您如何通过 API 从 Kubernetes 中的节点检索 cpu 使用情况?

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

我想使用 Kubernetes API 在我自己的 Web 应用程序中计算并显示节点特定 cpu 使用百分比。

我需要与 Kube UI 和 Cadvisor 显示相同的信息,但我想使用 Kubernetes API。

我在 node-ip:10255/stats 下发现了一些 cpu 指标,其中包含时间戳、cpu 用法:总计、用户和系统,这些都是我不明白的奇怪数字。 CPU-Limit 也报告为 1024。

Kube UI 如何计算 cpu 使用量,是否可以通过 API 来计算?

因此 CPU 在 Kubernetes 中通常收集使用指标的方式是使用 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

如果您使用 Kubernetes v1.2,则会有一个新的、更清晰的指标摘要 API。来自发行说明:

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

您可以通过 <node-ip>:10255/stats/summarydetailed API objects is here 访问端点。