如何在 Prometheus 中显示使用来自 Kubernetes 中所有 pods 运行 的 golang 客户端库捕获的自定义应用程序指标

How to show custom application metrics in Prometheus captured using the golang client library from all pods running in Kubernetes

我正在尝试使用 prometheus 客户端库获取在 golang 中捕获的一些自定义应用程序指标,以显示在 Prometheus 中。

我有以下工作:

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
  labels:
    zone: prod
    version: v1
  annotations:
   prometheus.io/scrape: 'true'
   prometheus.io/port: '8080'

spec:
   containers:
    - name: my-container
      image: name/my-app:latest
      imagePullPolicy: IfNotPresent
      ports:
      - containerPort: 8080

kubectl exec -it my-app-pod -- /bin/bash

然后在 "localhost:8080/metrics" 上执行 wget,我可以看到我的指标

到目前为止一切顺利,这就是我碰壁的地方。我可以有多个 pods 运行 同一张图片。我想将所有图像作为目标公开给普罗米修斯。如何配置我的 pods 以便它们出现在 prometheus 中,以便我可以报告我的自定义指标?

感谢您提供的任何帮助!

你能分享一下你用来抓取指标的普罗米修斯配置吗?该配置将控制从哪些来源抓取指标。以下是一些您可以参考的链接:https://groups.google.com/forum/#!searchin/prometheus-users/Application$20metrics$20monitoring$20of$20Kubernetes$20Pods%7Csort:relevance/prometheus-users/uNPl4nJX9yk/cSKEBqJlBwAJ

你需要两样东西:

  • Prometheus Operator 的 ServiceMonitor,它指定将针对指标抓取哪些服务
  • 与 ServiceMonitor 匹配并指向您的 pods
  • 的服务

这里的文档中有一个示例:https://coreos.com/operators/prometheus/docs/latest/user-guides/running-exporters.html

kubernetes_sd_config 指令可用于发现所有具有给定标签的 pods。你的 Prometheus.yml 配置文件应该是这样的: - job_name: 'some-app' kubernetes_sd_configs: - role: pod relabel_configs: - source_labels: [__meta_kubernetes_pod_label_app] regex: python-app action: keep

源标签 [__meta_kubernetes_pod_label_app] 基本上是使用 Kubernetes api 查看标签为 'app' 且其值由正则表达式,在下面的行中给出(在本例中,匹配 'python-app')。

完成此操作后,Prometheus 将自动发现您想要的 pods 并开始从您的应用程序中扫描aping 指标。

希望对您有所帮助。您可以关注博客 post here 了解更多详情。

注意:值得一提的是,在撰写本文时,kubernetes_sd_config仍处于测试阶段。因此,在未来的版本中可能会对配置进行重大更改。