6
votes

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?

2

2 Answers

4
votes

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.

3
votes

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.