我正在尝试使用 prometheus 客户端库在 golang 中捕获一些自定义应用程序指标,以显示在 Prometheus 中。
我有以下工作:
我有一个 go 应用程序,它在 localhost:8080/metrics 上公开指标,如本文所述:
https://godoc.org/github.com/prometheus/client_golang/prometheus
我有一个正在运行的 kubernates minikube,它使用本文中的运算符运行 Prometheus、Grafana 和 AlertManager:
https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus
我为我的 go 应用程序创建了一个 docker 镜像,当我运行它并转到 localhost:8080/metrics 时,我可以看到浏览器中显示的 prometheus 指标。
我使用以下 pod.yaml 将我的 docker 镜像部署到 k8s 中的 pod
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
- 如果我使用以下方式连接到我的 pod:
kubectl exec -it my-app-pod -- /bin/bash
然后在“localhost:8080/metrics”上执行 wget,我可以看到我的指标
到目前为止一切顺利,这就是我碰壁的地方。我可以让多个 pod 运行同一个图像。我想将所有图像作为目标公开给普罗米修斯。如何配置我的 pod 以便它们显示在 prometheus 中,以便我可以报告我的自定义指标?
感谢您提供的任何帮助!
最佳答案
kubernetes_sd_config 指令可用于发现具有给定标签的所有 pod。你的 Prometheus.yml 配置文件应该是这样的:
- job_name: 'some-app'
kubernetes_sd_configs:
- 角色:pod
重新标记配置:
- source_labels:[__meta_kubernetes_pod_label_app]
正则表达式:python-app
Action :保持
源标签 [__meta_kubernetes_pod_label_app] 基本上是使用 Kubernetes api 查看标签为“app”且其值由正则表达式捕获的 pod,在下面的行中给出(在本例中,匹配“python -app').
完成此操作后,Prometheus 将自动发现您想要的 pod 并开始从您的应用中抓取指标。
希望对您有所帮助。可以关注博文here了解更多详情。
注意:值得一提的是,在撰写本文时,kubernetes_sd_config 仍处于测试阶段。因此,在未来的版本中可能会对配置进行重大更改。
关于go - 如何在 Prometheus 中显示使用 golang 客户端库从 Kubernetes 中运行的所有 pod 捕获的自定义应用程序指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889970/