Prometheus queries to get the cpu and memory request of only pods which are in running state

3/29/2019

In my kubernetes cluster I have some running pods and a bunch of more pods in "completed" state. I use the query, for eg., kube_pod_container_resource_requests_cpu_cores{namespace="default"} to get the cpu request of the pods in the default namespace. This gives me the cpu request off ALL pods. However, what I want is ONLY the cpu request of the pods in "Running" state. Any idea how to achieve this? Thanks

-- Mulugeta Ayalew Tamiru
kubernetes
prometheus

1 Answer

6/3/2019

Please try with following query:

kube_pod_container_resource_requests_cpu_cores{job="kube-state-metrics"} * on (endpoint, instance, job, namespace, pod, service) group_left(phase) (kube_pod_status_phase{phase=~"^(Pending|Running)
quot;
} == 1)

which makes use of Prometheus matching operator to select labels that regex-match - here only Pods with Running or Pending state.

-- Nepomucen
Source: StackOverflow