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

huangapple go评论100阅读模式
英文:

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

问题

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

我已经完成了以下工作:

  • 我有一个Go应用程序,按照这篇文章中的描述,在localhost:8080/metrics上公开指标:

    https://godoc.org/github.com/prometheus/client_golang/prometheus

  • 我有一个运行Prometheus、Grafana和AlertManager的Kubernetes minikube,使用这篇文章中的操作员运行:

    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的目标。我该如何配置我的pod,以便它们在Prometheus中显示,以便我可以报告我的自定义指标?

感谢提供的任何帮助!

英文:

I am trying to get some custom application metrics captured in golang using the prometheus client library to show up in Prometheus.

I have the following working:

> 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

  • If I connect to my pod using:

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

then do wget on "localhost:8080/metrics", I can see my metrics

So far so good, here is where I am hitting a wall. I could have multiple pods running this same image. I want to expose all the images to prometheus as targets. How do I configure my pods so that they show up in prometheus so I can report on my custom metrics?

Thanks for any help offered!

答案1

得分: 2

kubernetes_sd_config指令可用于发现具有特定标签的所有Pod。您的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'且其值由正则表达式捕获的Pod(在本例中匹配'python-app')。

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

希望对您有所帮助。您可以在此处查看博客文章以获取更多详细信息。

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

英文:

The kubernetes_sd_config directive can be used to discover all pods with a given tag. Your Prometheus.yml config file should have something like so:

- job_name: 'some-app'
  kubernetes_sd_configs:
    - role: pod
  relabel_configs:
    - source_labels: [__meta_kubernetes_pod_label_app]
      regex: python-app
      action: keep

The source label [__meta_kubernetes_pod_label_app] is basically using the Kubernetes api to look at pods that have a label of 'app' and whose value is captured by the regex expression, given on the line below (in this case, matching 'python-app').

Once you've done this Prometheus will automatically discover the pods you want and start scraping the metrics from your app.

Hope that helps. You can follow blog post here for more detail.

Note: it is worth mentioning that at the time of writing, kubernetes_sd_config is still in beta. Thus breaking changes to configuration may occur in future releases.

答案2

得分: 0

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

英文:

Can you share the prometheus config that you are using to scrape the metrics. The config will control what all sources to scrape the metrics from. Here are a few links that you can refer to : https://groups.google.com/forum/#!searchin/prometheus-users/Application$20metrics$20monitoring$20of$20Kubernetes$20Pods%7Csort:relevance/prometheus-users/uNPl4nJX9yk/cSKEBqJlBwAJ

答案3

得分: 0

你需要两样东西:

  • 一个用于Prometheus Operator的ServiceMonitor,它指定了将被抓取指标的服务
  • 一个与ServiceMonitor匹配并指向你的Pod的Service

文档中有一个示例,可以在这里找到:https://coreos.com/operators/prometheus/docs/latest/user-guides/running-exporters.html

英文:

You need 2 things:

  • a ServiceMonitor for the Prometheus Operator, which specifies which services will be scraped for metrics
  • a Service which matches the ServiceMonitor and points to your pods

There is an example in the docs over here: https://coreos.com/operators/prometheus/docs/latest/user-guides/running-exporters.html

huangapple
  • 本文由 发表于 2017年8月26日 06:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/45889970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定