gpt4 book ai didi

kubernetes - container_memory_usage_bytes 按部署名称

转载 作者:行者123 更新时间:2023-12-04 09:34:06 25 4
gpt4 key购买 nike

给定一个 kubernetes 集群:

  • prometheus
  • node-exporter
  • kube-state-metrics

  • 我喜欢使用度量 container_memory_usage_bytes但选择 deployment_name而不是 pod .
    选择器如 container_memory_usage_bytes{pod_name=~"foo-.+"}如果 deployment_name=foo只要没有部署 deployment_name=foo-bar 就很好.
    我想用指标 kube_pod_container_resource_limits_memory_bytes 实现同样的目标.
    有没有办法实现这一目标?

    最佳答案

    TL;DR
    没有通过 deployment-name 查询普罗米修斯的直接方法。
    您可以使用部署的标签查询特定部署的内存使用情况。
    使用的查询:

    sum(
    kube_pod_labels{label_app=~"ubuntu.*"} * on (pod) group_right(label_app) container_memory_usage_bytes{namespace="memory-testing", container=""}
    )
    by (label_app)
    有一篇很棒的文章解释了这个查询背后的概念。我鼓励你阅读它:
  • Medium.com: Amimahloof: Kubernetes promql prometheus cpu aggregation walkthrough

  • 我在下面的例子中包含了一个解释。

    问题中提到的选择器: container_memory_usage_bytes{pod_name=~"foo-.+"}

    .+ - match any string but not an empty string


    带有像这样的 pod :
  • foo-12345678-abcde - 将匹配 (部署 foo)
  • foo-deployment-98765432-zyxzy - 将匹配 (部署 foo-deployment)

  • 如上所示,它将匹配两个 Pod 和两个部署。
    更多引用:
  • Prometheus.io: Docs: Prometheus: Querying: Basics

  • 如前所述,您可以使用部署中的标签来查明特定部署使用的资源。
    假如说:
  • memory-testing 命名空间中有 2 个部署:
  • 带有 3 个副本的 ubuntu
  • 带有 3 个副本的 ubuntu-additional

  • 以上部署具有与其名称相同的标签(它们可以不同):
  • app: ubuntu
  • app: ubuntu-additional

  • Kubernetes 集群版本 1.18.X

  • Why do I specify Kubernetes version?

    Kubernetes 1.16 will remove the duplicate pod_name and container_name metric labels from cAdvisor metrics. For the 1.14 and 1.15 release all pod, pod_name, container and container_name were available as a grace period.


  • Github.com: Kubernetes: Metrics Overhaul

  • 这意味着您需要替换如下参数:
  • podpod_name
  • containercontainer_name

  • 部署 Prometheus 和其他工具来监控我使用的集群: Github.com: Coreos: Kube-prometheus ubuntu 部署中的 pod 被配置为生成人工负载( stress-ng )。这样做是为了展示如何避免所用资源翻倍的情况。 memory-testing 命名空间中 pod 使用的资源:
    $ kubectl top pod --namespace=memory-testing
    NAME CPU(cores) MEMORY(bytes)
    ubuntu-5b5d6c56f6-cfr9g 816m 280Mi
    ubuntu-5b5d6c56f6-g6vh9 834m 278Mi
    ubuntu-5b5d6c56f6-sxldj 918m 288Mi
    ubuntu-additional-84bdf9b7fb-b9pxm 0m 0Mi
    ubuntu-additional-84bdf9b7fb-dzt72 0m 0Mi
    ubuntu-additional-84bdf9b7fb-k5z6w 0m 0Mi
    如果您要使用以下查询查询 Prometheus:
    container_memory_usage_bytes{namespace="memory-testing", pod=~"ubuntu.*"}
    您将获得类似于下面的输出(出于示例目的,它被剪切为仅显示一个 pod,默认情况下,它将显示名称和 ubuntu 命名空间中带有 memory-testing 的所有 pod):
    container_memory_usage_bytes{endpoint="https-metrics",id="/kubepods/besteffort/podb96dea39-b388-471e-a789-8c74b1670c74",instance="192.168.0.117:10250",job="kubelet",metrics_path="/metrics/cadvisor",namespace="memory-testing",node="node1",pod="ubuntu-5b5d6c56f6-cfr9g",service="kubelet"} 308559872
    container_memory_usage_bytes{container="POD",endpoint="https-metrics",id="/kubepods/besteffort/podb96dea39-b388-471e-a789-8c74b1670c74/312980f90e6104d021c12c376e83fe2bfc524faa4d4cee6553182d0fa2e007a1",image="k8s.gcr.io/pause:3.2",instance="192.168.0.117:10250",job="kubelet",metrics_path="/metrics/cadvisor",name="k8s_POD_ubuntu-5b5d6c56f6-cfr9g_memory-testing_b96dea39-b388-471e-a789-8c74b1670c74_0",namespace="memory-testing",node="node1",pod="ubuntu-5b5d6c56f6-cfr9g",service="kubelet"} 782336
    container_memory_usage_bytes{container="ubuntu",endpoint="https-metrics",id="/kubepods/besteffort/podb96dea39-b388-471e-a789-8c74b1670c74/1b93889a3e7415ad3fa040daf89f3f6bc77e569d85069de518267666ede8e21c",image="ubuntu@sha256:55cd38b70425947db71112eb5dddfa3aa3e3ce307754a3df2269069d2278ce47",instance="192.168.0.117:10250",job="kubelet",metrics_path="/metrics/cadvisor",name="k8s_ubuntu_ubuntu-5b5d6c56f6-cfr9g_memory-testing_b96dea39-b388-471e-a789-8c74b1670c74_0",namespace="memory-testing",node="node1",pod="ubuntu-5b5d6c56f6-cfr9g",service="kubelet"} 307777536
    在这一点上,您需要选择将使用的指标。在这个例子中,我使用了第一个。如需深入了解,请查看以下文章:
  • Blog.freshtracks.io: A deep dive into kubernetes metrics part 3 container resource metrics
  • Ianlewis.org: Almighty pause container

  • 如果我们使用 sum (QUERY) by (pod) 汇总这些指标,我们实际上会将报告的已用资源翻倍。
    剖析主查询:
    container_memory_usage_bytes{namespace="memory-testing", container=""}
    以上查询将输出每个 pod 的已用内存指标记录。 container="" 参数用于仅获取一条没有 container 参数的记录(前面提到过)。
    kube_pod_labels{label_app=~"ubuntu.*"}
    上面的查询将输出带有 pods 的记录及其带有 ubuntu.* 正则表达式的标签
    kube_pod_labels{label_app=~"ubuntu.*"} * on (pod) group_right(label_app) container_memory_usage_bytes{namespace="memory-testing", container=""}
    以上查询将 pod 中的 kube_pod_labelspod 中的 container_memory_usage_bytes 进行匹配,并将 label_app 添加到每条记录中。
    sum (kube_pod_labels{label_app=~"ubuntu.*"} * on (pod) group_right(label_app) container_memory_usage_bytes{namespace="memory-testing", container=""}) by (label_app)
    上面的查询将通过 label_app 对记录求和。
    之后,您应该能够通过标签(实际上是部署)获取将使用的内存相加的查询。
    Grafana Prometheus query

    至于:

    The same I'd like to achieve with the metrickube_pod_container_resource_limits_memory_bytes.


    假设部署中的每个 pod 具有相同的限制,您可以使用以下查询获取标记有标签的部署的内存限制,如上例所示:
    kube_pod_labels{label_app="ubuntu-with-limits"} * on (pod) group_right(label_app) kube_pod_container_resource_limits_memory_bytes{namespace="memory-testing", pod=~".*"}
    您可以在此查询上应用 avg()mean()max() 等函数来获取将成为您的内存限制的单个数字:
    avg(kube_pod_labels{label_app="ubuntu-with-limits"} * on (pod) group_right(label_app) kube_pod_container_resource_limits_memory_bytes{namespace="memory-testing", pod=~".*"}) by (label_app)
    如果您使用 VPA ,您的内存限制可能会有所不同。在这种情况下,您可以同时显示所有这些或使用 avg() 来获得所有“部署”的平均值。
    Combined resources and limits

    作为上述解决方案的 解决方法 ,您可以尝试使用如下正则表达式:
    container_memory_usage_bytes{pod=~"^ubuntu-.{6,10}-.{5}"}

    关于kubernetes - container_memory_usage_bytes 按部署名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62677636/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com