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

Din*_*uja 7 containers kubernetes prometheus prometheus-node-exporter

我正在使用 Prometheus 来监控我的 Kubernetes 集群。我已经在单独的命名空间中设置了 Prometheus。我有多个命名空间并且多个 pod 正在运行。每个 pod 容器在这个端点公开一个自定义指标,:80/data/metrics. 我正在获取 Pods CPU、内存指标等,但如何配置 Prometheus 以从:80/data/metrics每个可用的 Pod 中提取数据?我已经使用本教程来设置 Prometheus,链接

api*_*sim 14

问题中提供的链接引用了普罗米修斯配置的此 ConfigMap 如果使用了 ConfigMap,那么 prometheus 就已经配置为抓取 pods了。

对于该配置(请参阅 参考资料relabel_configs),要让 prometheus 抓取 pod 在 处公开的自定义指标:80/data/metrics,请将这些注释添加到 pod 部署配置中:

metadata:
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/path: '/data/metrics'
    prometheus.io/port: '80'
Run Code Online (Sandbox Code Playgroud)

请参阅prometheus 文档(向下滚动)中的 Kubernetes 发现配置选项https,了解与抓取等相关的设置。

编辑:我在发布我的答案后才看到 Emruz Hossain 的答案。他的答案目前缺少prometheus.io/scrape: 'true'注释并指定=:注释的名称/值分隔符,这在 yaml 或 json 中无效。


Emr*_*ain 12

您必须将这三个注释添加到您的 pod 中:

prometheus.io/scrape: 'true'
prometheus.io/path: '/data/metrics'
prometheus.io/port: '80'
Run Code Online (Sandbox Code Playgroud)

它将如何运作?

看看kubernetes-pods的工作config-map.yaml您使用配置普罗米修斯,

- 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
Run Code Online (Sandbox Code Playgroud)

检查这三个重新标记配置

- 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__
Run Code Online (Sandbox Code Playgroud)

在这里,__metrics_path__以及port是否从这个 pod 中废弃指标是从 pod 注释中读取的。

有关如何配置 Prometheus 的更多详细信息,请参见此处

  • 注意:使用 prometheus-operator 时这不起作用!例如,在安装 kube-stack-prometheus 图表时,因为它使用 PodMonitors。请参阅 https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack#prometheusioscrape (4认同)