Prometheus监控容器内pod节点内存/CPU使用率

发布时间 2024-01-12 11:44:31作者: 呼长喜

内存使用率:

表达式1:sum (container_memory_working_set_bytes{container !="",container!="POD"}) by (container, pod) / sum(container_spec_memory_limit_bytes{container !="",container!="POD"}) by (container, pod) * 100 !=+Inf

表达式2:round(sum by(name, id, job, node) (container_memory_rss{image!=""}) / sum by(name, id, job, node) (container_spec_memory_limit_bytes{image!=""}) * 100 != +Inf) > 99

说明:

!= +Inf:
过滤正无穷的数据,如果容器没有定义限制内存,测container_spec_memory_limit_bytes值是0;
container_memory_rss:
RSS内存,即常驻内存集(Resident Set Size),是分配给进程使用实际物理内存,而不是磁盘上缓存的虚拟内存。RSS内存包括所有分配的栈内存和堆内存,以及加载到物理内存中的共享库占用的内存空间,但不包括进入交换分区的内存。
container_memory_working_set_bytes 容器使用内存 更能体现出mem usage,也是oom killer指标(建议使用)
container_spec_memory_limit_bytes:
容器的内存使用量限制,当k8s中的确认


CPU使用率:

表达式1:sum(irate(container_cpu_usage_seconds_total{container !="",container!="POD"}[2m])) by (container, pod) / (sum(container_spec_cpu_quota{container !="",container!="POD"}/100000) by (container, pod)) * 100

表达式2:sum by(name, namespace, job) (rate(container_cpu_usage_seconds_total{image!=""}[10m])) >= (sum by(name, namespace, job) (container_spec_cpu_quota{image!=""} / 100000))

说明:
公式 平均1s容器使用CPU的时间/配额CPU个数*1s

container_cpu_usage_seconds_total 该容器服务针对每个CPU累计消耗的CPU时间。如果有多个CPU,则总的CPU时间需要把各个CPU耗费的时间相加,可以求出平均1s容器使用的时间
container_spec_cpu_quota 是容器的CPU配额,它的值是:为容器指定的CPU个数*100000。故Pod在1s内CPU的总时间为:Pod的CPU核数 * 1s:
注意:
CPU配额可以通过container_spec_cpu_quota除以container_spec_cpu_period来得到,
或用CPU Limit值kube_pod_container_resource_limits_cpu_cores也可以

说明:
CPU资源的限制与内存不同。当容器使用的内存超过限制配额后,会被系统加到OOM-Killing候选中。当容器使用CPU资源到达申请配额时,容器不会被系统驱逐或怎么样,只是限制CPU使用。