静态文件服务发现

Prometheus是如何通过静态文件进行服务发现的

当使用 prometheus_sd_method == ‘static’ 的静态文件服务发现模式时,Prometheus会使用静态文件进行服务发现。

Prometheus FHS

配置文件地址默认为 /etc/prometheus/targets/ 目录,根据所属模块又分为几个子目录:infra, nodes, pgsql, redis, ….

#------------------------------------------------------------------------------
# Config FHS
#------------------------------------------------------------------------------
# /etc/prometheus/
#  ^-----prometheus.yml              # prometheus main config file
#  ^-----alertmanager.yml            # alertmanger main config file
#  ^-----@bin                        # util scripts: check,reload,status,new
#  ^-----@rules                      # record & alerting rules definition
#            ^-----@infra            # infrastructure rules & alert
#            ^-----@nodes            # nodes rules & alert
#            ^-----@pgsql            # pgsql rules & alert
#            ^-----@redis            # redis rules & alert
#            ^-----@..........       # etc...
#  ^-----@targets                    # file based service discovery targets definition
#            ^-----@infra            # infra static targets definition
#            ^-----@nodes            # nodes static targets definition
#            ^-----@pgsql            # pgsql static targets definition
#            ^-----@redis            # redis static targets definition
#            ^-----@.....            # other targets
#------------------------------------------------------------------------------

监控对象文件

/etc/prometheus/targets
├── nodes
│   ├── 10.10.10.13.yml
│   ├── 10.10.10.10.yml
│   ├── 10.10.10.11.yml
│   ├── 10.10.10.12.yml
├── pgsql
│   ├── pg-meta-1.yml
│   ├── pg-test-2.yml
│   ├── pg-test-3.yml
│   ├── pg-test-1.yml
├── infra
│   ├── 10.10.10.10.yml
├── redis
│   ├── redis-common-1.yml
│   ├── redis-test-2.yml
│   ├── redis-meta-1.yml
│   ├── redis-test-1.yml

INFRA监控对象

INFRA监控对象以元节点的IP地址作为文件名,内容如下:

---
#------------------------------------------------------------------------------
# Prometheus (9090)
#------------------------------------------------------------------------------
- labels: { ip: 10.10.10.10, type: prometheus }
  targets:
    - 10.10.10.10:9090

#------------------------------------------------------------------------------
# AlertManager (9093)
#------------------------------------------------------------------------------
- labels: { ip: 10.10.10.10, type: alertmanager }
  targets:
    - 10.10.10.10:9093

#------------------------------------------------------------------------------
# Grafana (3000)
#------------------------------------------------------------------------------
- labels: { ip: 10.10.10.10, type: grafana }
  targets:
    - 10.10.10.10:3000

#------------------------------------------------------------------------------
# Loki (3100)
#------------------------------------------------------------------------------
- labels: { ip: 10.10.10.10, type: loki }
  targets:
    - 10.10.10.10:3100

#------------------------------------------------------------------------------
# Nginx (Exporter @ 9113)
#------------------------------------------------------------------------------
- labels: { ip: 10.10.10.10, type: nginx }
  targets:
    - 10.10.10.10:9113

DCS监控对象

DCS监控对象直接定于 /etc/prometheus/prometheus.yml 主配置文件中:

  - job_name: consul
    metrics_path: /v1/agent/metrics
    params:
      format: ['prometheus']
    static_configs: [ { targets: [ 127.0.0.1:8500 ] , labels: { ip: 10.10.10.10, type: consul , job: infra } } ]

  - job_name: etcd
    metrics_path: /metrics
    static_configs:
      - { targets: [ 10.10.10.11:2379 ] , labels: { ip: 10.10.10.11, ins: meta-2, type: etcd , job: infra } }
      - { targets: [ 10.10.10.12:2379 ] , labels: { ip: 10.10.10.12, ins: meta-3, type: etcd , job: infra } }
      - { targets: [ 10.10.10.10:2379 ] , labels: { ip: 10.10.10.10, ins: meta-1, type: etcd , job: infra } }

NODES监控对象

NODES监控对象以IP地址作为监控对象文件名,内容如下所示:

$ cat nodes/10.10.10.10.yml
# 10.10.10.10
- labels: { ip: 10.10.10.10 , ins: pg-meta-1 , cls: pg-meta }
  targets:
    - 10.10.10.10:9100   # node_exporter
    - 10.10.10.10:9323   # docker
    - 10.10.10.10:9080   # promtail 

PGSQL监控对象

PGSQL监控对象以实例名作为监控对象文件名,内容如下所示:

$ cat pgsql/pg-meta-1.yml
# pg-meta-1 [primary] @ 10.10.10.10
- labels: { cls: pg-meta, ins: pg-meta-1, ip: 10.10.10.10 }
  targets:
    - 10.10.10.10:9630  # postgres
    - 10.10.10.10:9631  # pgbouncer
    - 10.10.10.10:8008  # patroni
    - 10.10.10.10:9101  # haproxy

REDIS监控对象

REDIS监控对象以Redis节点名作为监控对象文件名,内容如下所示:

$ cat pgsql/redis-common-1.yml
# redis-common-1 @ 10.10.10.13

- labels: { cls: redis-common, ins: redis-common-1-6501, instance: 10.10.10.13:6501 }
  targets: [ redis://10.10.10.13:6501 ]

- labels: { cls: redis-common, ins: redis-common-1-6502, instance: 10.10.10.13:6502 }
  targets: [ redis://10.10.10.13:6502 ]

- labels: { cls: redis-common, ins: redis-common-1-6503, instance: 10.10.10.13:6503 }
  targets: [ redis://10.10.10.13:6503 ]

最后修改 2022-05-27: init commit (1e3e284)