Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you retrieve cpu usage from Node in Kubernetes via API?

I want to calculate and show node specific cpu usage in percent in my own web application using Kubernetes API.

I need the same information as Kube UI and Cadvisor displays but I want to use the Kubernetes API.

I have found some cpu metrics under node-ip:10255/stats which contains timestamp, cpu usage: total, user and system in big weird numbers which I do not understand. Also the CPU-Limit is reported as 1024.

How does Kube UI calculate cpu usage and is it possible to do the same via the API?

like image 705
Martin Svensson Avatar asked Mar 31 '16 18:03

Martin Svensson


2 Answers

If you use Kubernetes v1.2, there is a new, cleaner metrics summary API. From the release note:

Kubelet exposes a new Alpha metrics API - /stats/summary in a user friendly format with reduced system overhead.

You can access the endpoint through <node-ip>:10255/stats/summary and detailed API objects is here.

like image 165
Yu-Ju Hong Avatar answered Oct 10 '22 06:10

Yu-Ju Hong


So the way CPU usage metrics are usually collected in Kubernetes is using cAdvisor https://github.com/google/cadvisor which looks at the cgroups to get metircs, so mostly CPU and some memory metrics. cAdvisor then can put its data into a metrics DB like heapster, influxDB or prometheus. Kubernetes does not directly deal with metrics, so therefore does not expose it through the API, however you can use the metrics DB instead. Additionally you can use an additional container in your pod to collect metrics and place that into your metrics DB. Additionally, you can get resource quotas through the API, but not usage. This proposal may be of some interest for you as well https://github.com/kubernetes/kubernetes/blob/release-1.2/docs/proposals/metrics-plumbing.md.

like image 41
Christian Grabowski Avatar answered Oct 10 '22 08:10

Christian Grabowski