Prometheus+Consul服務自動發現監控

2019.11.14晚21:00直播《任務管理系統》

一、簡介

在上一篇Prometheus+Node_exporter+Grafana+Alertmanager 監控部署裏,已經有了 promethues 的一套監控環境。但是這裏有個問題,就是在 prometheus.yml 裏配置需要監聽的服務時,是按服務名寫死的,如果後面增加了節點或者組件信息,就得手動修改此配置,並重啓 promethues;那麼能否動態的監聽微服務呢?Prometheus 提供了多種動態服務發現的功能,這裏以 consul 爲例。

二、引入 consul 的好處

在沒有使用 consul 服務自動發現的時候,我們需要頻繁對 Prometheus 配置文件進行修改,無疑給運維人員帶來很大的負擔,還有可能直接變成一個"配置達人",即使是配置達人也會存在人爲失誤的情況。

三、Prometheus 支持的多種服務發現機制

#Prometheus數據源的配置主要分爲靜態配置和動態發現, 常用的爲以下幾類:
static_configs: #靜態服務發現
file_sd_configs: #文件服務發現
dns_sd_configs: DNS #服務發現
kubernetes_sd_configs: #Kubernetes 服務發現
consul_sd_configs: Consul #服務發現
...

#在監控kubernetes的應用場景中,頻繁更新的pod,svc,等等資源配置應該是最能體現Prometheus監控目標自動發現服務的好處

四、基於 docker 的 consul 集羣

這裏使用 docker-compose 方式部署 consul 集羣

cat > /data0/consul/docker-compose.yaml << \EOF
version: '2'
networks:
  byfn:

services:
  consul1:
    image: consul
    container_name: node1
    volumes: 
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    networks:
      - byfn

  consul2:
    image: consul
    container_name: node2
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    ports:
       - 8500:8500
    depends_on:
        - consul1
    networks:
      - byfn

  consul3:
    image: consul
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    depends_on:
        - consul1
    networks:
      - byfn

  consul4:
    image: consul
    container_name: node4
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8501:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn

  consul5:
    image: consul
    container_name: node5
    volumes:
      - /data0/consul/conf_without_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe5 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8502:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn
EOF

cd /data0/consul/
docker-compose up -d

Prometheus+Consul服務自動發現監控

五、使用接口註冊服務

# 註冊服務
curl -X PUT -d '{"id": "test1","name": "test1","address": "192.168.56.12","port": 9100,"tags": ["service"],"checks": [{"http": "http://192.168.56.12:9100/","interval": "5s"}]}' http://192.168.56.12:8502/v1/agent/service/register

# 查詢指定節點以及指定的服務信息
root># curl http://192.168.56.12:8500/v1/catalog/service/test1

[{"ID":"62c9ea24-a464-ee3f-a7ac-44b608b2a9fc","Node":"ndoe5","Address":"172.18.0.6","Datacenter":"dc1","TaggedAddresses":{"lan":"172.18.0.6","wan":"172.18.0.6"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"test1","ServiceName":"test1","ServiceTags":["service"],"ServiceAddress":"192.168.56.12","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":9100,"ServiceEnableTagOverride":false,"ServiceProxy":{"MeshGateway":{}},"ServiceConnect":{},"CreateIndex":261,"ModifyIndex":261}]

Prometheus+Consul服務自動發現監控

六、修改 prometheus 使用 consul 服務發現

cat > /home/prometheus/prometheus.yml <<\EOF
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
   - job_name: 'prometheus'
     static_configs:
      - targets: ['localhost:9090']

   # 匹配service關鍵字
   - job_name: 'consul-prometheus'
     consul_sd_configs:
       - server: '192.168.56.12:8502'
         services: []
EOF

#重啓prometheus
docker restart prometheus

七、結合 grafana 最終展示

Prometheus+Consul服務自動發現監控

參考文檔:

https://kuboard.cn/ (強烈推薦一位大佬新品,快速在 Kubernetes 上落地微服務)

https://www.jianshu.com/p/d4b85d404f6d Consul常用接口使用

作者:Lancger

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章