I need to get CPU and Memory usage in kubernetes pods with prometheus queries. Can someone plz help?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With