Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate containers' cpu usage in kubernetes with prometheus as monitoring?

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...

like image 323
Haoyuan Ge Avatar asked Oct 30 '16 07:10

Haoyuan Ge


People also ask

How is CPU measured in Kubernetes?

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.

How much CPU and memory does Prometheus use?

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.

How to get CPU and memory usage across all pods in Kubernetes?

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.

What makes Prometheus the standard for Kubernetes monitoring?

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 ...

Does your product live in a Kubernetes cluster?

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.

How to monitor Kubernetes nodes and clusters?

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.


2 Answers

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

enter image description here

enter image description here

like image 162
Camil Avatar answered Sep 21 '22 13:09

Camil


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.

like image 40
kentor Avatar answered Sep 22 '22 13:09

kentor