17.3.1 服务发现模式概述与适用场景

本节概述 Prometheus 的服务发现模式与适用场景,并通过可执行示例展示如何快速落地与排错。Prometheus 采用 Pull 方式抓取指标,服务发现的核心是“动态获取目标端点 + 统一标签管理 + 目标生命周期可追踪”。

文章图片

一、模式概览与适用场景(含示例)#

1) 静态发现(Static Config)
场景:目标少、变更不频繁。
示例:直接在 prometheus.yml 中写死目标。

# /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "node_static"
    static_configs:
      - targets: ["10.0.0.11:9100","10.0.0.12:9100"]
        labels:
          env: "prod"

2) 文件发现(File SD)
场景:目标由 CMDB/脚本/平台生成,支持热更新。
示例:Prometheus 监控文件夹中的 JSON。

# /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: "node_file_sd"
    file_sd_configs:
      - files:
          - /etc/prometheus/targets/node_*.json
        refresh_interval: 30s
// /etc/prometheus/targets/node_prod.json
[
  {
    "targets": ["10.0.0.21:9100","10.0.0.22:9100"],
    "labels": { "env": "prod", "team": "ops" }
  }
]

3) 注册中心发现(Consul/Nacos/ZooKeeper)
场景:微服务数量大、弹性伸缩频繁。
示例:Consul 发现服务。

# /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: "consul_sd"
    consul_sd_configs:
      - server: "10.0.0.31:8500"
        services: ["node-exporter","web-api"]
    relabel_configs:
      - source_labels: [__meta_consul_service]
        target_label: service

4) 编排平台发现(Kubernetes)
场景:云原生环境,按标签发现 Pod/Service。
示例:通过 K8s API 发现节点指标。

# /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: "kubernetes-nodes"
    kubernetes_sd_configs:
      - role: node
    scheme: https
    tls_config:
      insecure_skip_verify: true
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)

5) 云厂商 API 发现(EC2/GCE)
场景:云资源数量大,生命周期由云平台管理。
示例:EC2 实例发现(需配置访问密钥与区域)。

# /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: "ec2_sd"
    ec2_sd_configs:
      - region: "ap-southeast-1"
        port: 9100

二、安装与运行(最小可用示例)#

以 Linux 服务器为例快速启动 Prometheus,并验证服务发现生效。

# 1) 安装 Prometheus(示例:二进制方式)
useradd -r -s /sbin/nologin prometheus
cd /opt
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.50.0/prometheus-2.50.0.linux-amd64.tar.gz
tar zxvf prometheus-2.50.0.linux-amd64.tar.gz
ln -s /opt/prometheus-2.50.0.linux-amd64 /opt/prometheus
mkdir -p /etc/prometheus /var/lib/prometheus
cp /opt/prometheus/prometheus.yml /etc/prometheus/prometheus.yml

# 2) 启动(前台)
/opt/prometheus/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus

# 3) 验证 Web UI
# 浏览器访问 http://<server_ip>:9090/targets

三、关键命令与解释#

# 热更新配置(不重启)
curl -X POST http://127.0.0.1:9090/-/reload
# 说明:Prometheus 会重新加载服务发现配置

# 查看当前目标状态(REST API)
curl -s http://127.0.0.1:9090/api/v1/targets | jq '.data.activeTargets[].labels'
# 说明:查看抓取目标标签是否符合预期

四、排错要点(含命令)#

1) 目标未出现在 Targets
- 检查配置语法:

/opt/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --check-config
  • 检查 File SD 文件路径与权限:
ls -l /etc/prometheus/targets
cat /etc/prometheus/targets/node_prod.json

2) 目标状态为 DOWN
- 检查网络与端口:

curl -s http://10.0.0.21:9100/metrics | head
  • 检查防火墙/安全组:
ss -lntp | grep 9100

3) K8s 发现无目标
- 检查 RBAC 与 ServiceAccount:

kubectl auth can-i list nodes --as=system:serviceaccount:monitoring:prometheus

五、练习(动手验证)#

1) 使用 Static Config 添加 2 个 node_exporter 目标,并在 /targets 页面确认为 UP。
2) 将目标改为 File SD 方式,要求更新 JSON 后 30 秒内自动生效。
3) 添加 relabel 规则,把 __meta_kubernetes_node_label_role 重写为 role 标签,并在 UI 验证标签是否存在。

通过以上练习,掌握服务发现方式的选择、配置热更新与标签管理。