简体   繁体   中英

How to get number of pods running in prometheus

I am scraping the kubernetes metrics from prometheus and would need to extract the number of running pods.

I can see container_last_seen metrics but how should i get no of pods running. Can someone help on this?

If you need to get number of running pods, you can use a metric from the list of pods metrics https://github.com/kubernetes/kube-state-metrics/blob/master/docs/pod-metrics.md for that (To get the info purely on pods, it'd make sens to use pod-specific metrics). For example if you need to get the number of pods per namespace, it'll be: count(kube_pod_info{namespace="$namespace_name"}) by (namespace) To get the number of all pods running on the cluster, then just do: count(kube_pod_info)

Assuming you want to display that in Grafana according to your question tags, from this Kubernetes App Metrics dashboard for example:

count(count(container_memory_usage_bytes{container_name="$container", namespace="$namespace"}) by (pod_name))

You can just import the dashboard and play with the queries.

Depending on your configuration/deployment, you can adjust the variables container_name and namespace , grouping by (pod_name) and count 'ing it does the trick. Some other label than pod_name can be used as long as it's shared between the pods you want to count.

If you want to see only the number of "deployed" pods in some namespace, you can use the solutions in previous answers.

My use case was to see the current running pods in some namespace and below is my solution:

'min_over_time(sum(group(kube_pod_container_status_ready{namespace="BC_NAME"}) by (pod,uid)) [5m:1m]) OR on() vector(0)'

Please replace BC_NAME with your namespace name. The timespan provides you fine the data.

If no data found - no pod currently running it returns '0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM