Kubernetes pod cpu usage percentage

I try to calculate how many percent cpu of limits pod use. I try to use this expression:

sum(irate(container_cpu_usage_seconds_total{cluster!="production",namespace="devops"}[2m])) by (pod_name) / kube_pod_container_resource_limits_cpu_cores{cluster!="production",namespace="devops"}

But it show 0 series. Left and right sided expressions return scalar values.

Upvotes: 1

Views: 2942

Answers (2)

Darshan Deshmukh
Darshan Deshmukh

Reputation: 353

You can use below prometheus expression to give percentage CPU usage -

sum(rate(container_cpu_usage_seconds_total{container_name="abc"}[5m])) by (container_name) /sum(container_spec_cpu_quota{container_name="abc"}/container_spec_cpu_period{container_name="abc"}) by (container_name) *100 

You can create your own custom dashboard in Grafana to get the data for last x number of days.

Upvotes: 1

mibrl12
mibrl12

Reputation: 484

I suppose the problem is that not every pod container has resource limits of CPU cores. Therefore you get two vectors of different length and expression fails without an error message.

You can verify my guess the following way:

count(sum(irate(container_cpu_usage_seconds_total{cluster!="production",namespace="devops"}[2m])) by (pod_name))

and:

count(kube_pod_container_resource_limits_cpu_cores{cluster!="production",namespace="devops"})

These two values should be the same, if not - here is the reason your query doesn't work.

Upvotes: 0

Related Questions