Prometheus Kubernetes SD Config - 存在 pod 注释

Rak*_*h N 2 prometheus

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

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

一位同事建议——

- job_name: 'k8sPodScrape'
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: ['__meta_kubernetes_pod_annotationpresent_my_custom_annotation']
        action: 'keep'
        regex: 'true'
Run Code Online (Sandbox Code Playgroud)

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

Ble*_*ess 6

您可以使用 实现相同的效果__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
Run Code Online (Sandbox Code Playgroud)

action: keep并确保仅当注释为 时regex: true才会从 Pod 中删除指标。prometheus.io/scrapetrue

应将此注释添加到您的 pod 定义中:

annotations:
  prometheus.io/scrape: 'true'
Run Code Online (Sandbox Code Playgroud)

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