Prometheus - Discovery

收集配置

静态方式:

Prometheus 通过 static_configs 来配置监控对象,这种情况下监控的目标 IP、Port、Metrics URI 都是固定的。

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['172.20.1.101:9100']

 

动态方式:

Prometheus 结合服务发现来实现,被监控对象先在注册中心进行服务注册,而后 Prometheus 直接去注册中心进行获取被监控信息。


基于文件的形式

scrape_configs:
- job_name: 'node_exporter'
  file_sd_configs:
  - files:
    - target/node_exporter.yml
    refresh_interval: 1m

cat target/node_exporter.yml
- targets:
  - 172.20.1.101:9100
  - 172.20.1.102:9100
  - 172.20.1.103:9100
  labels:
    exporter: node_exporter
    app: kubernetes


基于 Consul 的方式

scrape_configs:
- job_name: node_exporter
  consul_sd_configs:
  - server: consul-service:8500
    refresh_interval: 1m
    services: []
  relabel_configs:
  - source_labels: [__meta_consul_tags]
    regex: .*node-exporter.*
    action: keep


服务注册

curl -X PUT -d '{"id": "node-exporter","name": "node-exporter-172.20.31.195","address": "172.20.31.195","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://172.20.31.195:9100/metrics", "interval": "10s"}]}' http://consul:8500/v1/agent/service/register

服务注销

curl -X PUT http://consul:8500/v1/agent/service/deregister/node-exporter


基于 DNS 的方式

scrape_configs:
  - job_name: 'node-exporter-dns'
    dns_sd_configs:
    - names: ["node1.example.com"]
      type: A
      port: 9100

基于 Kubernetes 的方式

- job_name: 'kubernetes-nodes'
    tls_config:
      ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    kubernetes_sd_configs:
    - role: node
  - job_name: 'kubernetes-ingress'
    tls_config:
      ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    kubernetes_sd_configs:
    - role: ingress
  - job_name: 'kubernetes-endpoints'
    tls_config:
      ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    kubernetes_sd_configs:
    - role: endpoints
  - job_name: 'kubernetes-service'
    tls_config:
      ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    kubernetes_sd_configs:
    - role: service
  - job_name: 'kubernetes-pod'
    tls_config:
      ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    kubernetes_sd_configs:
    - role: pod


Prometheus - Discovery

但结果确不是预想中的,因为不是每一个被监控对象都支持被 Prometheus 采集 /metrics 。






上一篇:Kubernetes - 7.2 RBAC - RoleBinding


下一篇:Elasticsearch Word Segmentation