TL;DR
没有直接的方法可以通过 deployment-name 查询 prometheus。
您可以使用部署的标签查询特定部署的内存使用情况。
使用过的查询:
sum(
kube_pod_labels{label_app=~"ubuntu.*"} * on (pod) group_right(label_app) container_memory_usage_bytes{namespace="memory-testing", container=""}
)
by (label_app)
有一篇很棒的文章解释了这个查询背后的概念。我鼓励你阅读它:
我在下面的示例中包含了一个解释。
问题中提到的选择器:
container_memory_usage_bytes{pod_name=~"foo-.+"}
.+ - 匹配任何字符串,但不匹配空字符串
使用 pod 如下:
-
foo-12345678-abcde - 将匹配(部署foo)
-
foo-deployment-98765432-zyxzy - 将匹配(部署foo-deployment)
如上所示,它将匹配两个 pod 和两个部署。
更多参考:
如前所述,您可以使用部署中的标签来查明特定部署使用的资源。
假设:
-
memory-testing 命名空间中有 2 个部署:
-
ubuntu 3 个副本
-
ubuntu-additional 3 个副本
- 以上部署的标签与其名称相同(它们可以不同):
app: ubuntu
app: ubuntu-additional
- Kubernetes集群版本
1.18.X
为什么要指定 Kubernetes 版本?
Kubernetes 1.16 将从 cAdvisor 指标中删除重复的 pod_name 和 container_name 指标标签。对于 1.14 和 1.15 版本,所有 pod、pod_name、container 和 container_name 都可以作为宽限期使用。
这意味着您需要替换以下参数:
-
pod 与 pod_name
-
container 与 container_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,默认情况下它将显示名称和 memory-testing 命名空间中具有 ubuntu 的所有 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
此时,您需要选择要使用的指标。在这个例子中,我使用了第一个。如需深入了解,请查看这篇文章:
如果我们将这些指标与sum (QUERY) by (pod) 汇总在一起,实际上我们报告的已用资源会增加一倍。
剖析主要查询:
container_memory_usage_bytes{namespace="memory-testing", container=""}
上面的查询将输出每个 pod 的已用内存指标的记录。 container=""参数用于只获取一条没有container参数的记录(前面提到过)。
kube_pod_labels{label_app=~"ubuntu.*"}
以上查询将输出带有 pod 的记录,其标签的正则表达式为 ubuntu.*
kube_pod_labels{label_app=~"ubuntu.*"} * on (pod) group_right(label_app) container_memory_usage_bytes{namespace="memory-testing", container=""}
上面的查询会将kube_pod_labels中的pod与container_memory_usage_bytes中的pod相匹配,并将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 对记录求和。
之后,您应该能够获得将通过标签(实际上是部署)汇总已用内存的查询。
至于:
我想通过指标实现相同的目标
kube_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() 来获取所有“部署”的平均值。
作为上述解决方案的解决方法,您可以尝试使用如下正则表达式:
container_memory_usage_bytes{pod=~"^ubuntu-.{6,10}-.{5}"}