简体   繁体   English

使用Prometheus监控自定义kubernetes pod指标

[英]Monitor custom kubernetes pod metrics using Prometheus

I am using Prometheus to monitor my Kubernetes cluster. 我正在使用Prometheus来监控我的Kubernetes集群。 I have set up Prometheus in a separate namespace. 我在一个单独的命名空间中设置了Prometheus。 I have multiple namespaces and multiple pods are running. 我有多个名称空间和多个pod正在运行。 Each pod container exposes a custom metrics at this end point, :80/data/metrics . 每个pod容器在此端点公开自定义指标:80/data/metrics I am getting the Pods CPU, memory metrics etc, but how to configure Prometheus to pull data from :80/data/metrics in each available pod ? 我正在获取Pods CPU,内存指标等,但是如何配置Prometheus从每个可用pod中提取数据:80/data/metrics I have used this tutorial to set up Prometheus, Link 我使用本教程设置了Prometheus, Link

You have to add this three annotation to your pods: 您必须将这三个注释添加到您的pod:

prometheus.io/scrape: 'true'
prometheus.io/path: '/data/metrics'
prometheus.io/port: '80'

How it will work? 它会如何工作?

Look at the kubernetes-pods job of config-map.yaml you are using to configure prometheus, 看看你用来配置prometheus的config-map.yamlkubernetes-pods工作,

- job_name: 'kubernetes-pods'

        kubernetes_sd_configs:
        - role: pod

        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
          action: replace
          target_label: __metrics_path__
          regex: (.+)
        - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
          action: replace
          regex: ([^:]+)(?::\d+)?;(\d+)
          replacement: $1:$2
          target_label: __address__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - source_labels: [__meta_kubernetes_namespace]
          action: replace
          target_label: kubernetes_namespace
        - source_labels: [__meta_kubernetes_pod_name]
          action: replace
          target_label: kubernetes_pod_name

Check this three relabel configuration 检查这三个relabel配置

- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
    action: keep
    regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
    action: replace
    target_label: __metrics_path__
    regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
    action: replace
    regex: ([^:]+)(?::\d+)?;(\d+)
    replacement: $1:$2
    target_label: __address__

Here, __metrics_path__ and port and whether to scrap metrics from this pod are being read from pod annotations. 在这里,正在从pod注释中读取__metrics_path__port以及是否从此pod中__metrics_path__度量标准。

For, more details on how to configure Prometheus see here . 有关如何配置Prometheus的更多详细信息,请参见此处

The link provided in the question refers to this ConfigMap for the prometheus configuration. 问题中提供的链接指的是prometheus配置的ConfigMap It that ConfigMap is used then prometheus is already configured to scrape pods . 使用ConfigMap然后prometheus已经配置为刮掉pods

For that configuration (see relabel_configs ) to have prometheus scrape the custom metrics exposed by pods at :80/data/metrics , add these annotations to the pods deployment configurations: 对于该配置(请参阅relabel_configs ),让prometheus抓取pod所公开的自定义指标:80/data/metrics ,将这些注释添加到pods部署配置中:

metadata:
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/path: '/data/metrics'
    prometheus.io/port: '80'

See the configuration options for Kubernetes discovery in the prometheus docs (scroll down) for settings related to scraping over https and more. 有关刮取https等的设置,请参阅prometheus文档中的Kubernetes发现配置选项 (向下滚动)。

Edit: I saw Emruz Hossain's answer only after I posted mine. 编辑:只有在我发布之后才看到Emruz Hossain的回答。 His answer currently lacks the prometheus.io/scrape: 'true' annotation and specified = instead of : as annotations' name/value separator which is invalid in yaml or json. 他的回答目前缺少prometheus.io/scrape: 'true'注释并指定=而不是:作为注释的名称/值分隔符在yaml或json中无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM