I want to calculate the cpu usage of all pods in a kubernetes cluster. I found two metrics in prometheus may be useful:
container_cpu_usage_seconds_total: Cumulative cpu time consumed per cpu in seconds. process_cpu_seconds_total: Total user and system CPU time spent in seconds. Cpu Usage of all pods = increment per second of sum(container_cpu_usage_seconds_total{id="/"})/increment per second of sum(process_cpu_seconds_total)
However, I found every second's increment of container_cpu_usage{id="/"}
larger than the increment of sum(process_cpu_seconds_total)
. So the usage may be larger than 1...
Resource units in Kubernetes Limits and requests for CPU resources are measured in cpu units. In Kubernetes, 1 CPU unit is equivalent to 1 physical CPU core, or 1 virtual core, depending on whether the node is a physical host or a virtual machine running inside a physical machine.
I found today that the prometheus consumes lots of memory(avg 1.75GB) and CPU (avg 24.28%). There are two prometheus instances, one is the local prometheus, the other is the remote prometheus instance.
you can use above promql with pod name in a query. The following query should return per-pod number of used CPU cores: The following query should return per-pod RSS memory usage: 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.
These four characteristics made Prometheus the de-facto standard for Kubernetes monitoring: 1 Multi-dimensional data model: The model is based on key-value pairs, similar to how Kubernetes organizes infrastructure... 2 Accessible format and protocols: Exposing prometheus metrics is a pretty straightforward task. Metrics are human... More ...
Our product lives in a Kubernetes cluster on our server. It is not in production yet, so there are multiple instances running in the cluster for different purposes, each in its own namespace. I need to run some load tests on one of the namespaces and I need to monitor CPU usage meanwhile. We have Prometheus and Grafana for monitoring.
Prometheus has made monitoring of Kubernetes nodes and clusters very easy. It provides various types of metrics like counter, graphs, summary, gauge, etc. You can monitor your Kubernetes services or cluster using Prometheus.
This I'm using to get CPU usage at cluster level:
sum (rate (container_cpu_usage_seconds_total{id="/"}[1m])) / sum (machine_cpu_cores) * 100
I also track the CPU usage for each pod.
sum (rate (container_cpu_usage_seconds_total{image!=""}[1m])) by (pod_name)
I have a complete kubernetes-prometheus solution on GitHub, maybe can help you with more metrics: https://github.com/camilb/prometheus-kubernetes
I created my own prometheus exporter (https://github.com/google-cloud-tools/kube-eagle), primarily to get a better overview of my resource utilization on a per node basis. But it also offers a more intuitive way monitoring your CPU and RAM resources. The query to get the cluster wide CPU usage would look like this:
sum(eagle_pod_container_resource_usage_cpu_cores)
But you can also easily get the CPU usage by namespace, node or nodepool.
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