参数解释
使用 Prometheus 配置 kubernetes 环境中 Container 的 CPU 使用率时,会经常遇到 CPU 使用超出 100%,下面就来解释一下:
1.container_spec_cpu_period
当对容器进行 CPU 限制时,CFS 调度的时间窗口,又称容器 CPU 的时钟周期通常是 100,000 微秒
2.container_spec_cpu_quota
是指容器的使用 CPU 时间周期总量,如果 quota 设置的是 700,000,就代表该容器可用的 CPU 时间是 7*100,000 微秒,通常对应 kubernetes 的 resource.cpu.limits 的值
3.container_spec_cpu_share
是指 container 使用分配主机 CPU 相对值,比如 share 设置的是 500m,代表窗口启动时向主机节点申请 0.5 个 CPU,也就是 50,000 微秒,通常对应 kubernetes 的 resource.cpu.requests 的值
4.container_cpu_usage_seconds_total
统计容器的 CPU 在一秒内消耗使用率,应注意的是该 container 所有的 CORE
5.container_cpu_system_seconds_total
统计容器内核态在一秒时间内消耗的 CPU
6.container_cpu_user_seconds_total
统计容器用户态在一秒时间内消耗的 CPU
参考官方地址 https://docs.signalfx.com/en/latest/integrations/agent/monitors/cadvisor.html https://github.com/google/cadvisor/blob/master/docs/storage/prometheus.md
具体公式
1.默认如果直接使用 container_cpu_usage_seconds_total 的话,如下
sum(irate(container_cpu_usage_seconds_total{container="$Container",instance="$Node",pod="$Pod"}[5m])*100)by(pod)
默认统计的数据是该容器所有的 CORE 的平均使用率
2.如果要精确计算每个容器的 CPU 使用率,使用 % 呈现的形式,如下
sum(irate(container_cpu_usage_seconds_total{container="$Container",instance="$Node",pod="$Pod"}[5m])*100)by(pod)/sum(container_spec_cpu_quota{container="$Container",instance="$Node",pod="$Pod"}/container_spec_cpu