Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prometheus queries to get CPU and Memory usage in kubernetes pods

I need to get CPU and Memory usage in kubernetes pods with prometheus queries. Can someone plz help?

like image 475
GihanS Avatar asked Mar 13 '19 13:03

GihanS


2 Answers

For CPU percentage

avg((sum (rate (container_cpu_usage_seconds_total {container_name!="" ,pod="<Pod name>" } [5m])) by (namespace , pod, container ) / on (container , pod , namespace) ((kube_pod_container_resource_limits_cpu_cores >0)*300))*100)

For Memory percentage

avg((avg (container_memory_working_set_bytes{pod="<pod name>"}) by (container_name , pod ))/ on (container_name , pod)(avg (container_spec_memory_limit_bytes>0 ) by (container_name, pod))*100)

you can use above promql with pod name in a query.

like image 169
Tejas Avatar answered Oct 08 '22 19:10

Tejas


The following query should return per-pod number of used CPU cores:

sum(rate(container_cpu_usage_seconds_total{container_name!="POD",pod_name!=""}[5m])) without (container_name)

The following query should return per-pod RSS memory usage:

sum(container_memory_working_set_bytes{container_name!="POD",pod_name!=""}) without (container_name)

If you need summary CPU and memory usage across all the pods in Kubernetes cluster, then just remove without (container_name) suffix from queries above.

like image 2
valyala Avatar answered Oct 08 '22 21:10

valyala