Consul是由HashiCorp開發的一個支援多資料中心的分散式服務發現和鍵值對儲存服務的開源軟體,被大量應用於基於微服務的軟體架構當中。
Consul使用可以透過Consul官網https://www.consul.io/downloads.html下載對應作業系統版本的軟體包。Consul與Prometheus同樣使用Go語言進行開發,因此安裝和部署的方式也極為簡單,解壓並將命令列工具放到系統PATH路徑下即可。
Consul安裝
# wget https://releases.hashicorp.com/consul/1.9.0/consul_1.9.0_linux_amd64.zip# unzip consul_1.9.0_linux_amd64.zip# cp consul /usr/local/bin/
在本地可以使用開發者模式在本地快速啟動一個單節點的Consul環境:
# consul agent -dev==> Starting Consul agent... Version: '1.9.0' Node ID: 'f8baf8b0-4bb3-4985-e854-793668506963' Node name: 'node1' Datacenter: 'dc1' (Segment: '<all>') Server: true (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600) Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false==> Log data will now stream in as it occurs: 2021-01-27T01:28:02.423Z [INFO] agent.server.raft: initial configuration: index=1 servers="[{Suffrage:Voter ID:f8baf8b0-4bb3-4985-e854-793668506963 Address:127.0.0.1:8300}]" 2021-01-27T01:28:02.424Z [INFO] agent.server.serf.wan: serf: EventMemberJoin: node1.dc1 127.0.0.1
在啟動成功後,在一個新的terminal視窗中執行consul members可以檢視當前叢集中的所有節點:
# consul membersNode Address Status Type Build Protocol DC Segmentnode1 127.0.0.1:8301 alive server 1.9.0 2 dc1 <all> 說明:Node: 節點名稱Address: 繫結IP和埠DC: 預設dc1,可以指定
還可以透過HTTP API的方式檢視當前叢集中的節點資訊:
# curl localhost:8500/v1/catalog/nodes[ { "ID": "f8baf8b0-4bb3-4985-e854-793668506963", "Node": "node1", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "lan_ipv4": "127.0.0.1", "wan": "127.0.0.1", "wan_ipv4": "127.0.0.1" }, "Meta": { "consul-network-segment": "" }, "CreateIndex": 11, "ModifyIndex": 12 }]
Consul還提供了內建的DNS服務,可以透過Consul的DNS服務的方式訪問其中的節點:
# dig @127.0.0.1 -p 8600 localhost.node.consul; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.2 <<>> @127.0.0.1 -p 8600 localhost.node.consul; (1 server found);; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 19687;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1;; WARNING: recursion requested but not available;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 4096;; QUESTION SECTION:;localhost.node.consul. IN A;; AUTHORITY SECTION:consul. 0 IN SOA ns.consul. hostmaster.consul. 1609149345 3600 600 86400 0;; Query time: 4 msec;; SERVER: 127.0.0.1#8600(127.0.0.1);; WHEN: Mon Dec 28 04:55:45 EST 2020;; MSG SIZE rcvd: 100
在Consul當中服務可以透過服務定義檔案或者是HTTP API的方式進行註冊。
1.使用服務定義檔案的方式將本地執行的node_exporter透過服務的方式註冊到Consul當中。
建立node_exporter.json檔案# sudo mkdir /etc/consul.d# echo '{"service": {"name": "node_exporter", "tags": ["exporter"], "port": 9100}}' | sudo tee /etc/consul.d/node_exporter.json重新啟動Consul服務,並且宣告服務定義檔案所在目錄:# consul agent -dev -config-dir=/etc/consul.d -client=192.168.0.1072020-12-28T04:58:38.146-0500 [DEBUG] agent: Node info in sync2020-12-28T04:58:38.146-0500 [DEBUG] agent: Service in sync: service=node_exporter
2.服務註冊成功之後就可以透過DNS或HTTP API的方式查詢服務資訊。預設情況下,所有的服務都可以使用NAME.service.consul域名的方式進行訪問。
例如,可以使用node_exporter.service.consul域名查詢node_exporter服務的資訊:
# dig @192.168.0.107 -p 8600 node_exporter.service.consul; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.3 <<>> @192.168.0.107 -p 8600 node_exporter.service.consul; (1 server found);; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3507;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; WARNING: recursion requested but not available;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 4096;; QUESTION SECTION:;node_exporter.service.consul. IN A;; ANSWER SECTION:node_exporter.service.consul. 0 IN A 127.0.0.1;; Query time: 44 msec;; SERVER: 192.168.0.107#8600(192.168.0.107);; WHEN: Wed Jan 27 01:56:36 GMT 2021;; MSG SIZE rcvd: 73
如上所示DNS記錄會返回當前可用的node_exporter服務例項的IP地址資訊。
除了使用DNS的方式以外,Consul還支援使用者使用HTTP API的形式獲取服務列表:
# curl http://192.168.0.107:8500/v1/catalog/service/node_exporter[ { "ID": "6f3e888c-d4ce-422b-6208-e90701a1174a", "Node": "node1", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "lan_ipv4": "127.0.0.1", "wan": "127.0.0.1", "wan_ipv4": "127.0.0.1" }, "NodeMeta": { "consul-network-segment": "" }, "ServiceKind": "", "ServiceID": "node_exporter", "ServiceName": "node_exporter", "ServiceTags": [ "exporter" ], "ServiceAddress": "", "ServiceWeights": { "Passing": 1, "Warning": 1 }, "ServiceMeta": {}, "ServicePort": 9100, "ServiceEnableTagOverride": false, "ServiceProxy": { "MeshGateway": {}, "Expose": {} }, "ServiceConnect": {}, "CreateIndex": 14, "ModifyIndex": 14 }]
Consul也提供了一個Web UI可以檢視Consul中所有服務以及節點的狀態:
訪問頁面 http://192.168.0.107:8500/ui
consul UI
Consul整合Prometheus1.Consul服務檔案方式
name表示services名字, ID表示instance 我們可以實現,相同的service下面有多個instance。# echo '{"service": {"name": "node_exporter","id": "node_exporter_192.168.0.107","address": "192.168.0.107","tags": ["consul-exporter"], "port": 9100}}' | sudo tee /etc/consul.d/node_exporter.json# echo '{"service": {"name": "node_exporter","id": "node_exporter_192.168.0.108","address": "192.168.0.108","tags": ["consul-exporter"], "port": 9100}}' | sudo tee /etc/consul.d/node_exporter2.json需要重啟consul# consul agent -dev -config-dir=/etc/consul.d -client=192.168.0.107
檢視Consul UI http://192.168.0.107:8500/ui/dc1/services/node_exporter/instances
1個Service下多個instance
這就表示一個Service下面我們可以新增多個同類型的機器資訊。
2.Consul HTTP API方式
註冊服務, 一個name下面可以新增多個id,表示不同的機器。# curl -X PUT -d '{"name": "node-exporter-http","id": "node-exporter-192.168.0.107","address": "192.168.0.107","port": 9100,"tags": ["testenv"],"checks": [{"http": "http://192.168.0.107:9100/metrics", "interval": "5s"}]}' http://192.168.0.107:8500/v1/agent/service/register# curl -X PUT -d '{"name": "node-exporter-http","id": "node-exporter-192.168.0.108","address": "192.168.0.108","port": 9100,"tags": ["testenv"],"checks": [{"http": "http://192.168.0.108:9100/metrics", "interval": "5s"}]}' http://192.168.0.107:8500/v1/agent/service/register執行完畢後,重新整理一下 Consul Web 控制檯頁面,可以看到成功註冊到 Consul 中。登出掉某個服務,這裡的node-exporter,是ID# curl -X PUT http://192.168.0.107:8500/v1/agent/service/deregister/node-exporter 不需要重啟Consul
檢視Consul UI
http://192.168.0.107:8500/ui/dc1/services/node-exporter-http/instances
node-exporter-http
可以看到新新增的兩個Instance,表示HTTP API新增成功。
Consul與Prometheus整合
修改/etc/prometheus/prometheus.yml
- job_name: 'consul_node_exporter' metrics_path: /metrics scheme: http consul_sd_configs: - server: '192.168.0.107:8500' services: - node_exporter - job_name: 'consul_node_exporter_http' metrics_path: /metrics scheme: http consul_sd_configs: - server: '192.168.0.107:8500' services: - node-exporter-http注意: 這裡透過 node-exporter 來過濾consul裡面的 services。如果不過濾,下面配置 - job_name: 'consul_node_exporter' metrics_path: /metrics scheme: http consul_sd_configs: - server: '9.98.12.85:8500' services: []重啟Prometheus,讓配置生效# systemctl restart prometheus
檢視Prometheus Target http://192.168.0.107:9090/targets
Prometheus整合Consul
上圖可以看到,Prometheus從consul中獲取了node資訊,Consul服務檔案和HTTP API方式都成功了。圖中訪問失敗,是因為我沒有192.168.0.108這個服務,可以忽略。
至此,Prometheus基於Consul服務發現功能搭建完成。