服务发现 #

一、服务发现概述 #

1.1 什么是服务发现 #

text
服务发现定义:

┌─────────────────────────────────────────────┐
│ 服务发现(Service Discovery)               │
├─────────────────────────────────────────────┤
│ • 自动发现监控目标                          │
│ • 动态更新目标列表                          │
│ • 支持多种发现机制                          │
│ • 无需手动配置每个目标                      │
└─────────────────────────────────────────────┘

优势:
├── 减少手动配置
├── 自动适应变化
├── 支持动态环境
└── 简化运维管理

1.2 支持的服务发现 #

text
支持的服务发现类型:

┌─────────────────────────────────────────────┐
│ 云平台                                      │
├─────────────────────────────────────────────┤
│ • AWS EC2                                   │
│ • Azure                                     │
│ • GCP GCE                                   │
│ • 阿里云                                    │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 容器编排                                    │
├─────────────────────────────────────────────┤
│ • Kubernetes                                │
│ • Docker Swarm                              │
│ • Nomad                                     │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 服务注册                                    │
├─────────────────────────────────────────────┤
│ • Consul                                    │
│ • Etcd                                      │
│ • Zookeeper                                 │
│ • Eureka                                    │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 其他                                        │
├─────────────────────────────────────────────┤
│ • DNS                                       │
│ • File                                      │
│ • HTTP                                      │
│ • Static                                    │
└─────────────────────────────────────────────┘

二、静态配置 #

2.1 基本静态配置 #

yaml
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets:
          - 'localhost:9090'
        labels:
          env: 'local'

2.2 多目标配置 #

yaml
scrape_configs:
  - job_name: 'node-exporter'
    static_configs:
      - targets:
          - 'node1:9100'
          - 'node2:9100'
          - 'node3:9100'
        labels:
          env: 'production'
      
      - targets:
          - 'node4:9100'
          - 'node5:9100'
        labels:
          env: 'staging'

三、文件服务发现 #

3.1 JSON格式 #

yaml
# prometheus.yml
scrape_configs:
  - job_name: 'file-sd'
    file_sd_configs:
      - files:
          - '/etc/prometheus/targets/*.json'
        refresh_interval: 5m
json
# targets/nodes.json
[
  {
    "targets": ["node1:9100", "node2:9100"],
    "labels": {
      "env": "production",
      "region": "cn-east"
    }
  },
  {
    "targets": ["node3:9100"],
    "labels": {
      "env": "staging",
      "region": "cn-west"
    }
  }
]

3.2 YAML格式 #

yaml
# targets/nodes.yml
- targets:
    - node1:9100
    - node2:9100
  labels:
    env: production
    region: cn-east

- targets:
    - node3:9100
  labels:
    env: staging
    region: cn-west

四、Kubernetes服务发现 #

4.1 基本配置 #

yaml
scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
        namespaces:
          names:
            - default
            - monitoring

4.2 发现角色 #

text
Kubernetes发现角色:

┌─────────────────────────────────────────────┐
│ node                                        │
├─────────────────────────────────────────────┤
│ • 发现集群节点                              │
│ • 每个节点一个目标                          │
│ • 地址:节点IP                              │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ pod                                         │
├─────────────────────────────────────────────┤
│ • 发现Pod                                   │
│ • 每个Pod一个目标                           │
│ • 地址:Pod IP                              │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ service                                     │
├─────────────────────────────────────────────┤
│ • 发现Service                               │
│ • 每个Service一个目标                       │
│ • 地址:Service ClusterIP                   │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ endpoints                                   │
├─────────────────────────────────────────────┤
│ • 发现Endpoints                             │
│ • 每个Endpoint一个目标                      │
│ • 地址:Pod IP                              │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ ingress                                     │
├─────────────────────────────────────────────┤
│ • 发现Ingress                               │
│ • 每个Ingress路径一个目标                   │
│ • 地址:Ingress主机                         │
└─────────────────────────────────────────────┘

4.3 Pod发现配置 #

yaml
scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
    
    relabel_configs:
      # 只采集有特定注解的Pod
      - 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_(.+)
      
      # 设置instance标签
      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        target_label: instance

4.4 常用元数据标签 #

text
Kubernetes元数据标签:

Pod相关:
├── __meta_kubernetes_pod_name
├── __meta_kubernetes_pod_namespace
├── __meta_kubernetes_pod_ip
├── __meta_kubernetes_pod_label_<labelname>
├── __meta_kubernetes_pod_annotation_<annotationname>
└── __meta_kubernetes_pod_container_name

Node相关:
├── __meta_kubernetes_node_name
├── __meta_kubernetes_node_label_<labelname>
└── __meta_kubernetes_node_address_<type>

Service相关:
├── __meta_kubernetes_service_name
├── __meta_kubernetes_service_namespace
├── __meta_kubernetes_service_label_<labelname>
└── __meta_kubernetes_service_cluster_ip

五、Consul服务发现 #

5.1 基本配置 #

yaml
scrape_configs:
  - job_name: 'consul-services'
    consul_sd_configs:
      - server: 'consul:8500'
        services: []
        tags: ['prometheus']
        refresh_interval: 30s

5.2 高级配置 #

yaml
scrape_configs:
  - job_name: 'consul-services'
    consul_sd_configs:
      - server: 'consul:8500'
        token: 'xxx'
        datacenter: 'dc1'
        services: ['api', 'web']
        tags: ['production']
        refresh_interval: 30s
    
    relabel_configs:
      - source_labels: [__meta_consul_tags]
        regex: '.*,production,.*'
        action: keep
      
      - source_labels: [__meta_consul_service]
        target_label: service
      
      - source_labels: [__meta_consul_node]
        target_label: instance

六、DNS服务发现 #

6.1 SRV记录发现 #

yaml
scrape_configs:
  - job_name: 'dns-srv'
    dns_sd_configs:
      - names:
          - '_prometheus._tcp.service.consul'
        refresh_interval: 30s

6.2 A记录发现 #

yaml
scrape_configs:
  - job_name: 'dns-a'
    dns_sd_configs:
      - names:
          - 'api.service.consul'
          - 'web.service.consul'
        type: 'A'
        port: 9090
        refresh_interval: 30s

七、HTTP服务发现 #

yaml
scrape_configs:
  - job_name: 'http-sd'
    http_sd_configs:
      - url: 'http://config-server/targets'
        refresh_interval: 5m
        basic_auth:
          username: admin
          password: password

八、总结 #

服务发现类型:

类型 用途
static 静态配置
file 文件动态发现
kubernetes K8s集群发现
consul Consul服务发现
dns DNS记录发现

选择建议:

场景 推荐
固定目标 static
动态目标 file/consul
Kubernetes kubernetes_sd
服务注册中心 consul/dns

下一步,让我们学习联邦集群!

最后更新:2026-03-27