【问题标题】:Monitor custom kubernetes pod metrics using Prometheus使用 Prometheus 监控自定义 kubernetes pod 指标
【发布时间】:2018-11-18 20:36:47
【问题描述】:

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

【问题讨论】:

  • 如何在上述端点公开 pod 级别的指标?我们需要在应用程序的 kubernetes 部署文件中添加一些特殊的东西吗?

标签: kubernetes containers prometheus prometheus-node-exporter


【解决方案1】:

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

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

它将如何工作?

查看config-map.yamlkubernetes-pods 作业,您用于配置prometheus,

- 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

检查这三个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__

这里,__metrics_path__port 以及是否从该 pod 中废弃指标正在从 pod 注释中读取。

有关如何配置 Prometheus 的更多详细信息,请参阅here

【讨论】:

    【解决方案2】:

    问题中提供的link 指的是prometheus 配置的this ConfigMap。如果使用了 ConfigMap 那么 prometheus 已经配置为scrape pods

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多