Prometheus查询获取Kubernetes Pod中的CPU和内存使用情况

13

我需要使用Prometheus查询获取Kubernetes中Pod的CPU和内存使用情况,有人可以帮忙吗?


1
请提供更多关于您当前情况的信息。Prometheus是否正在运行,但您不知道如何查询指标?您是否在集群中遇到了Prometheus运行问题? - Monkeyanator
是的,它已经启动并运行。我想要获取有关Pod的CPU和内存使用情况的警报。为此,我需要拥有Prometheus查询。请问您正在使用什么? - GihanS
我想要类似于这样的内容:"sum(container_memory_usage_bytes{namespace="$namespace", pod_name="$pod", container_name!="POD"}) by (container_name)",由于查询中存在变量,因此我无法发送警报。 - GihanS
请编辑您的问题,并附上您尝试过的任何查询。 - Dev
4个回答

14

对于 CPU 使用百分比:

avg((sum (rate (container_cpu_usage_seconds_total {container_name!="" ,pod="<Pod name>" } [5m])) by (namespace , pod, container ) / on (container , pod , namespace) ((kube_pod_container_resource_limits_cpu_cores >0)*300))*100)

内存百分比

avg((avg (container_memory_working_set_bytes{pod="<pod name>"}) by (container_name , pod ))/ on (container_name , pod)(avg (container_spec_memory_limit_bytes>0 ) by (container_name, pod))*100)

您可以在查询中使用上面的PromQL和Pod名称。


7
为什么要乘以300? - chaosguru

4
以下查询应返回每个 Pod 使用的 CPU 核心数:
sum(rate(container_cpu_usage_seconds_total{container_name!="POD",pod_name!=""}[5m])) without (container_name)

以下查询应返回每个 pod 的 RSS 内存使用情况
sum(container_memory_working_set_bytes{container_name!="POD",pod_name!=""}) without (container_name)

如果您需要在Kubernetes集群中汇总所有Pod的CPU和内存使用情况,则只需从上面的查询中删除without (container_name)后缀即可。

0
你是否使用 prometheus-operator 从 kubernetes 收集数据? 如果是,可以使用类似以下的东西: sum(container_memory_usage_bytes) sum(container_cpu_usage_seconds_total) 仅供举例。

我想要类似于这样的内容:"sum(container_memory_usage_bytes{namespace="$namespace", pod_name="$pod", container_name!="POD"}) by (container_name)",由于查询中存在变量,因此我无法发送警报。 - GihanS

0
为了更好地监控 Pod/容器的内存使用情况,除了使用 container_memory_working_set_bytes 之外,还应该使用 container_memory_max_usage_bytes 进行监控。

我们过去通过监控 pod 的内存使用情况

avg(container_memory_working_set_bytes{container!="POD"}) by (pod)  / (avg(kube_pod_container_resource_requests_memory_bytes{container!="POD"} > 0) by (pod)) *100 

然而,我们遇到了 Pod 的 OOMKilled,并且从上述指标中未能发现任何异常。


通过 container_memory_max_usage_bytes 我们可以找到 Pod 的异常内存使用情况。

avg(container_memory_max_usage_bytes{ container!="POD"}) by (pod)  / (avg(kube_pod_container_resource_requests_memory_bytes{ container!="POD"} > 0) by (pod)) *100 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接