2019.11.14晚21:00直播《任務管理系統》。node
在上一篇Prometheus+Node_exporter+Grafana+Alertmanager 監控部署裏,已經有了 promethues 的一套監控環境。可是這裏有個問題,就是在 prometheus.yml 裏配置須要監聽的服務時,是按服務名寫死的,若是後面增長了節點或者組件信息,就得手動修改此配置,並重啓 promethues;那麼可否動態的監聽微服務呢?Prometheus 提供了多種動態服務發現的功能,這裏以 consul 爲例。docker
在沒有使用 consul 服務自動發現的時候,咱們須要頻繁對 Prometheus 配置文件進行修改,無疑給運維人員帶來很大的負擔,還有可能直接變成一個"配置達人",即便是配置達人也會存在人爲失誤的狀況。bootstrap
#Prometheus數據源的配置主要分爲靜態配置和動態發現, 經常使用的爲如下幾類: static_configs: #靜態服務發現 file_sd_configs: #文件服務發現 dns_sd_configs: DNS #服務發現 kubernetes_sd_configs: #Kubernetes 服務發現 consul_sd_configs: Consul #服務發現 ... #在監控kubernetes的應用場景中,頻繁更新的pod,svc,等等資源配置應該是最能體現Prometheus監控目標自動發現服務的好處
這裏使用 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
# 註冊服務 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}]
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
參考文檔:curl
https://kuboard.cn/ (強烈推薦一位大佬新品,快速在 Kubernetes 上落地微服務)ide
https://www.jianshu.com/p/d4b85d404f6d Consul經常使用接口使用微服務
做者:Lancgerui