【问题标题】:Prometheus Kubernetes SD Config - pod annotation presentPrometheus Kubernetes SD Config - 存在 pod 注释
【发布时间】:2020-08-31 14:33:19
【问题描述】:

我希望 Prometheus 只抓取我的 Kubernetes 集群中带有特定注释的 pod。因此,在作业配置中,我想按照此处的建议使用__meta_kubernetes_pod_annotationpresent_<annotationname> : true - 针对 Prometheus 的每个 Kubernetes SD 配置的 https://prometheus.io/docs/prometheus/latest/configuration/configuration/#pod。

问题是,我找不到任何相同的例子:(

一位同事建议-

- job_name: 'k8sPodScrape'
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: ['__meta_kubernetes_pod_annotationpresent_my_custom_annotation']
        action: 'keep'
        regex: 'true'

但我不确定它是否有效以及如何运作?任何指针都会有很大帮助。

【问题讨论】:

    标签: prometheus


    【解决方案1】:

    您可以使用__meta_kubernetes_pod_annotation_<annotationname> 实现相同的目的。例如,在prometheus.yml,你可以有这样的配置:

          - job_name: 'kubernetes-pods'
            kubernetes_sd_configs:
            - role: pod
            relabel_configs:
            - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
              action: keep
              regex: true
    

    action: keep 和 regex: true 确保仅当注释 prometheus.io/scrape 为 true 时才会从 pod 中抓取指标。

    这个注解应该添加到你的 pod 定义中:

    annotations:
      prometheus.io/scrape: 'true'
    

    所以现在 prometheus 仅在 prometheus.io/scrape 设置为 true 时才从 pod 中抓取指标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2021-01-30
      • 2021-12-23
      • 1970-01-01
      相关资源
      最近更新 更多