通过 Prometheus 从多个 Kubernetes pods 抓取自定义统计信息

Scraping custom stats from multiple Kubernetes pods via Prometheus

我们是 Kubernetes 下云上的 运行 Prometheus,能够获取内存的统计信息,CPU 节点的使用情况等。现在我们希望能够从云上的 pods 运行 抓取我们自己的自定义时间序列。据我了解,这需要一份具有 kubernetes_sd_config 角色 'pod' 的工作(以及将 select 仅 pods 具有给定名称的重新标记)。到目前为止,一切都很好。但是,从我阅读的文档中:

The pod role discovers all pods and exposes their containers as targets. For each declared port of a container, a single target is generated. If a container has no specified ports, a port-free target per container is created for manually adding a port via relabeling.

所以每个 pod 都必须声明一个端口(大概是在它的 k8s yaml 中?),否则它会变成无端口的。在这种情况下,我如何确保每个 pod 都有唯一的端口号? (如果我不这样做,如果两个 pods 在同一个节点上启动,它可能会失败)

and a relabel that will select only pods with a given name

我想这是可能的,但在我们的配置中,我们只是省略了抓取注释(下面讨论)并且 Prom 不联系那些 Pods;我们已经几个月没有触及 Prom 配置本身,但更频繁地更改 Pod 注释。

So each pod must declare a port

their example config 中,可以看到从 Pod 中提取注释的位置,并用于将 Prom 抓取器指向该特定 Pod 的端口和路径(当然,包括是否完全抓取它).

metadata:
    annotation:
        prometheus.io/scrape: true
        prometheus.io/path: /custom-prom-metrics
        prometheus.io/port: 9145

该片段可以进入您的 ReplicationController、Deployment、ReplicaSet、Pod(如果您部署 "bare" Pods),或者使用 Kubernetes post-facto 附加到 Pod API。据我所知,可以随意应用和删除这些注释,Prom 将在下一次抓取时将其拾取。

how would I ensure that each pod has a unique port number? (and if I didn't, it would presumably fail if two pods were started on the same node)

假设你有一个合理的 Kubernetes 安装,一个软件定义的网络,那么每个 Pod 都有自己的 IP 地址,因此有自己的端口命名空间。如果您想使用 9145 对 每个 Pod 进行标准化,那就太好了。