如何在Prometheus中查询容器内存限制

如何在Prometheus中查询容器内存限制

本文介绍了如何在Prometheus中查询容器内存限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Prometheus工具监视我的Kubernetes集群.

I am using Prometheus tool for monitoring my Kubernetes cluster.

我在部署中设置了资源限制(内存限制),需要配置一个面板来显示可用的总内存.请让我知道要在Prometheus中运行以获取可用于我的部署的总内存限制的查询.

I have set a resource limit(memory limit) in my deployments and need to configure a panel for showing the total memory available. Please let me know the query needed to run in Prometheus for getting the total memory limit available for my deployment.

推荐答案

可以使用度量kube_pod_container_resource_limits_memory_bytes(由 kube-state-metrics )和container_memory_usage_bytes(由kubelet/cAdvisor提供)

It is possible using metrics kube_pod_container_resource_limits_memory_bytes (provided by kube-state-metrics) and container_memory_usage_bytes (provided by kubelet/cAdvisor)

label_replace(
  label_replace(
    kube_pod_container_resource_limits_memory_bytes{},
    "pod_name",
    "$1",
    "pod",
    "(.+)"
  ),
  "container_name",
  "$1",
  "container",
  "(.+)"
)
-
on(pod_name,namespace,container_name)
avg(
      container_memory_usage_bytes{pod_name=~".+"}
)
by (pod_name,namespace,container_name)

对该查询的一些解释:这是对内存限制和实际使用量的减去.需要使用 label_replace 函数来匹配两个指标的标签名称,因为它们是从不同的目标获得的. avg 用于获取Pod重新启动之间的平均值,因为每次Pod重新启动都会创建一个新指标. {pod_name =〜.+"} 用于从 container_memory_usage_bytes 中过滤对这种情况无用的指标

A little explanation of the query: It is a subtraction of the memory limit and the actual usage. label_replace functions are needed to match the label names of both metrics, as they are obtained from different targets. avg is used to get the average between pod restarts, as every pod restart creates a new metric. {pod_name=~".+"} is used to filter metrics from container_memory_usage_bytes that are not useful for this case

这篇关于如何在Prometheus中查询容器内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:35