To visualize CPU usage per pod, create a Time Series panel in Grafana and use the following PromQL query:
What Does This Query Do?
container_cpu_usage_seconds_total
This metric is exposed by cAdvisor and represents the cumulative CPU time consumed by a container.
Since it is a counter, the value continuously increases over time as the container consumes CPU resources.
rate(...[5m])
The rate() function calculates how quickly the counter is increasing over the last five minutes.
Instead of showing the total CPU time consumed since the pod started, it returns the current CPU consumption rate.
namespace=~"^$namespace$"
This filter limits the query to the namespace selected in your Grafana dashboard variable.
If your dashboard variable contains production, Grafana effectively evaluates:
namespace=~"^production$"
This ensures only pods from the selected namespace are included.
pod!=""
This excludes metrics that do not contain a pod label.
Without this filter, you may end up graphing data that isn't associated with a Kubernetes pod.
Many Kubernetes pods contain multiple containers. The query sums CPU consumption across all containers belonging to the same pod, giving you a single CPU usage value per pod.
Understanding the Results
The query returns CPU usage in CPU cores.
For example:
| Pod | CPU Value |
|---|---|
| api-pod | 0.50 |
| worker-pod | 1.25 |
| frontend-pod | 0.15 |
This means:
0.50= Half of one CPU core1.00= One full CPU core1.25= One and a quarter CPU cores2.00= Two full CPU cores
Configure the chart unit as Percent (0–100). When your Grafana panel is configured to display percentages, Grafana automatically converts these values into a more human-friendly format.
For example, if the query returns a CPU value of 0.50 for a pod, Grafana displays it as 50%. Likewise, 0.15 becomes 15%, while 1.25 becomes 125%, indicating the pod is consuming the equivalent of 1.25 CPU cores.
| Pod | CPU Value | Displayed Percentage |
|---|---|---|
| api-pod | 0.50 | 50% |
| worker-pod | 1.25 | 125% |
| frontend-pod | 0.15 | 15% |
It's worth noting that percentages above 100% are possible because the underlying metric represents CPU cores consumed. A value of 1.25 means the pod is using one and a quarter CPU cores, not 125% of its configured CPU limit.
Apply the following graph settings as shown below:
When these settings are applied, the graph displays the current CPU usage for the namespace selected from your dashboard dropdown.
#grafana #prometheus #monitoring
0 Comments