To visualize CPU usage per pod, create a Time Series panel in Grafana and use the following PromQL query:
sum (rate (container_cpu_usage_seconds_total{pod!="",namespace=~"^$namespace$"}[5m])) by (pod)
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 5 minutes.
Instead of showing the total CPU time consumed since the pod started, it shows 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!=""
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 will automatically convert these values into a more human-friendly percentage output format.
For example, if the query returns a CPU value of 0.50 for a pod, Grafana will display it as 50% when the panel unit is set to Percent (0-100). 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:
#grafana #prometheus
0 Comments