請參考:
CentOS7.5 Prometheus2.5+Grafana5.4監控部署node
wget https://releases.hashicorp.com/consul/1.2.4/consul_1.2.4_linux_amd64.zip unzip consul_1.2.4_linux_amd64.zip -d /app/prometheus/bin/ cd /app/prometheus/bin/ chown -R prometheus.prometheus consul
# vim /usr/lib/systemd/system/consul.service [Unit] Description=consul Documentation=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus ExecStart=/app/prometheus/bin/consul agent \ -server -bootstrap-expect 1 \ -bind=0.0.0.0 \ -client=172.16.9.201 \ -data-dir=/app/prometheus/consuld/data/consul \ -node=172.17.9.201 \ -config-dir=/app/prometheus/consuld/conf \ -ui Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
systemctl daemon-reload systemctl start consul.service systemctl enable consul.service
# systemctl status consul.service ● consul.service - consul Loaded: loaded (/usr/lib/systemd/system/consul.service; enabled; vendor preset: disabled) Active: active (running) since 二 2018-12-11 15:23:12 CST; 20min ago Docs: https://prometheus.io/ Main PID: 5721 (consul) CGroup: /system.slice/consul.service └─5721 /app/prometheus/bin/consul agent -server -bootstrap-expect 1 -bind=0.0.0.0 -client=172.16.9.201 -data-dir=/app/prometheus/consuld/data/consul -node=172.17.9.201 -config... 12月 11 15:23:18 prometheus-node1 consul[5721]: 2018/12/11 15:23:18 [INFO] raft: Election won. Tally: 1 12月 11 15:23:18 prometheus-node1 consul[5721]: 2018/12/11 15:23:18 [INFO] raft: Node at 172.16.9.201:8300 [Leader] entering Leader state 12月 11 15:23:18 prometheus-node1 consul[5721]: 2018/12/11 15:23:18 [INFO] consul: cluster leadership acquired 12月 11 15:23:18 prometheus-node1 consul[5721]: 2018/12/11 15:23:18 [INFO] consul: New leader elected: 172.17.9.201 12月 11 15:23:18 prometheus-node1 consul[5721]: 2018/12/11 15:23:18 [INFO] consul: member '172.17.9.201' joined, marking health alive 12月 11 15:23:18 prometheus-node1 consul[5721]: 2018/12/11 15:23:18 [INFO] agent: Synced node info 12月 11 15:23:25 prometheus-node1 consul[5721]: ==> Newer Consul version available: 1.4.0 (currently running: 1.2.4) 12月 11 15:40:21 prometheus-node1 consul[5721]: 2018/12/11 15:40:21 [WARN] agent: Service name "node_exporter" will not be discoverable via DNS due to invalid characters. Val...and dashes. 12月 11 15:40:21 prometheus-node1 consul[5721]: 2018/12/11 15:40:21 [INFO] agent: Synced service "node_exporter" 12月 11 15:42:08 prometheus-node1 consul[5721]: 2018/12/11 15:42:08 [INFO] agent: Synced service "node_exporter" Hint: Some lines were ellipsized, use -l to show in full.
http://172.16.9.201:8500/ui/
linux
yum -y install jq
服務查詢算法
# curl -s http://172.16.9.201:8500/v1/catalog/services|jq { "consul": [] }
使用HTTP接口服務註冊:docker
# curl -X PUT -d '{"ID": "node_exporter", "Name": "node_exporter", "Address": "172.16.9.201", "Port": 9100, "Tags": ["lock"], "EnableTagOverride": false}' http://172.16.9.201:8500/v1/agent/service/register # curl -s http://172.16.9.201:8500/v1/catalog/services|jq { "consul": [], "node_exporter": [ "lock" ] }
# vim prometheus.yml global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: - 172.16.9.201:9093 - 172.16.9.202:9093 rule_files: - /app/prometheus/cfg/rule.yml scrape_configs: - job_name: 'prometheus' metrics_path: /metrics honor_labels: false static_configs: - targets: ['localhost:9090'] labels: group: 'node' service: 'prometheus' - job_name: 'prod_discover' metrics_path: /metrics honor_labels: false consul_sd_configs: - server: '172.16.9.201:8500' services: ['node_exporter'] tag_separator: '' relabel_configs: - source_labels: ['__meta_consul_tags'] target_label: 'product' - source_labels: ['__meta_consul_dc'] target_label: 'idc' - source_labels: ['__meta_consul_service'] target_label: 'service' - source_labels: ['job'] target_label: 'environment' regex: '(.*)_discover' replacement: '${1}'
http://172.16.9.201:9090/targetsbootstrap