如何计算每个节点上特定 pod CPU 使用率的百分比?

How to calculate percentage of specific pod CPU usage on each node?

我在具有 3 个节点的 k8s 集群上有一个 Fluentd daemonset。我想得到一个代表百分比的值,它给我一个关于每个节点上有多少 CPU(以 % 为单位)的信息,此时 fluentd pod 占用。

在 Prometheus 中如何实现?

谢谢。

您可能希望在 Prometheus 中使用 container_cpu_usage_seconds_total 查询。

像这样:

sum (rate (container_cpu_usage_seconds_total{}[5m])) by (container_name)

这将return系统中所有podsby容器名称的CPU用法。


您也可以应用一些过滤器来细化输出。例如:

sum (rate (container_cpu_usage_seconds_total{container_name=~"fluentd.*"}[5m])) by (container_name)

以上查询将 return CPU 用法与 pods 匹配以 fluentd

开头的容器名称

您可以将那些 pods 的使用量除以集群的总 cpu 个内核,以找出使用量的百分比,如下所示:

sum (rate (container_cpu_usage_seconds_total{container_name=~"fluentd.*"}[5m])) / sum (machine_cpu_cores{}) * 100

最后,为了获得特定 container_name 的特定节点上总 cpu 核心使用率的百分比,您将添加额外的过滤器:instance="INSTANCE_NAME":

sum (rate (container_cpu_usage_seconds_total{container_name=~"fluentd.*", instance="INSTANCE_NAME"}[5m])) / sum (machine_cpu_cores{}) * 100

N.B:根据 K8S 版本,由 container_cpu_usage_seconds_total 查询 return 的字段可能会有所不同。在某些系统上,容器的名称由 container_name 字段表示,而在某些系统上它是 container.

下面的查询应该 return per-node CPU pods 容器名称的百分比使用率,从 fluentd:

100 * (
  sum(rate(container_cpu_usage_seconds_total{container=~"fluentd.*"})) by (node)
    / on (node)
  kube_node_status_capacity{resource="cpu"}
)

container_cpu_usage_seconds_total 指标由 cadvisor - see these docs 导出。

kube_node_status_capacity 指标由 kube-state-metrics. See these docs 导出。