Chapter 04

配置与服务发现

读懂 prometheus.yml,配置 scrape 任务,用服务发现自动跟踪弹性实例,用 relabel 精细控制抓取与打标。

4.1 prometheus.yml 结构

global:
  scrape_interval: 15s       # 全局抓取周期
  scrape_timeout: 10s        # 单次抓取超时
  evaluation_interval: 15s   # 规则评估周期
  external_labels:            # 联邦/远程写时区分来源
    cluster: "prod-cn"

rule_files:                    # 告警/记录规则文件
  - "rules/*.yml"

alerting:                      # 指向 Alertmanager
  alertmanagers:
    - static_configs:
        - targets: ["alertmanager:9093"]

scrape_configs:               # 抓取任务(重点)
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
热加载配置

改完配置无需重启:启动时加 --web.enable-lifecycle,然后 curl -X POST http://localhost:9090/-/reload。也可用 promtool check config prometheus.yml 先校验语法。

4.2 scrape_config 详解

job_name
抓取任务名,会成为每条序列的 job 标签。
metrics_path
抓取路径,默认 /metrics
scheme
httphttps,默认 http。
scrape_interval / scrape_timeout
可覆盖全局值,为不同 job 设置不同抓取频率。
*_sd_configs
服务发现配置(kubernetes_sd_configs、file_sd_configs、dns_sd_configs、consul_sd_configs 等)。
relabel_configs
抓取对目标标签做重写、过滤;metric_relabel_configs 则在抓取作用于每条指标。
basic_auth / bearer_token / tls_config
抓取受保护端点时的认证与 TLS 配置。

4.3 静态配置

scrape_configs:
  - job_name: "node"
    scrape_interval: 30s
    static_configs:
      - targets:
          - "10.0.0.11:9100"
          - "10.0.0.12:9100"
        labels:
          env: "prod"
          team: "infra"

静态配置适合数量固定的目标。但在容器/云环境实例频繁增减,手动维护列表不现实——这就要靠服务发现。

4.4 服务发现

文件服务发现(最通用)

# prometheus.yml
  - job_name: "file-sd"
    file_sd_configs:
      - files: ["targets/*.json"]
        refresh_interval: 30s
// targets/api.json —— 由 CMDB/脚本动态生成
[
  {
    "targets": ["10.0.1.5:8080", "10.0.1.6:8080"],
    "labels": { "job": "api", "env": "prod" }
  }
]

DNS 服务发现

  - job_name: "dns"
    dns_sd_configs:
      - names: ["_prometheus._tcp.svc.local"]
        type: SRV
        refresh_interval: 30s

Kubernetes 服务发现

  - job_name: "k8s-pods"
    kubernetes_sd_configs:
      - role: pod          # 可选 node/service/endpoints/pod/ingress
    relabel_configs:
      # 只抓带 prometheus.io/scrape=true 注解的 Pod
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: "true"
      # 用注解里的 port 覆盖抓取地址
      - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
        action: replace
        regex: "([^:]+)(?::\d+)?;(\d+)"
        replacement: "$1:$2"
        target_label: __address__
      # 把 namespace / pod 名提升为普通标签
      - source_labels: [__meta_kubernetes_namespace]
        target_label: namespace
      - source_labels: [__meta_kubernetes_pod_name]
        target_label: pod

4.5 relabel_configs 重打标签

relabel 是 Prometheus 最强大也最烧脑的机制。它在抓取前对目标的元标签(__meta_*__address__ 等以 __ 开头的内部标签)做一系列变换。

action作用
keep只保留 regex 匹配 source_labels 的目标
drop丢弃 regex 匹配的目标
replace用 replacement 写入 target_label(默认 action)
labelmap按 regex 批量把匹配的 label 名映射成新名
labeldrop / labelkeep删除 / 保留匹配名字的标签
hashmod对 source 取哈希取模,用于抓取分片
relabel vs metric_relabel

relabel_configs 作用于抓取目标(决定抓不抓、给目标打什么标签);metric_relabel_configs 作用于抓回来的每条指标(常用于 drop 掉高基数或无用指标,省存储)。

# metric_relabel:丢弃某个高基数指标,省成本
    metric_relabel_configs:
      - source_labels: [__name__]
        regex: "go_gc_duration_seconds.*"
        action: drop

4.6 小结与下一步