Consul 有不少組件,對於總體來講,它是一個服務發現和服務配置的工具,它提供了一下特性:node
如下是在 CentOS 系統上操做linux
下載web
wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip
查看版本shell
consul -v Consul v1.1.0 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
使用dev模式啓動。json
$ consul agent -dev ==> Starting Consul agent... ==> Consul agent running! Version: 'v1.1.0' Node ID: 'a94cdf4f-e36d-9bef-927c-61c1d14500cd' Node name: 'bogon' Datacenter: 'dc1' (Segment: '<all>') Server: true (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600) Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false ==> Log data will now stream in as it occurs: 2018/06/23 00:03:45 [DEBUG] agent: Using random ID "a94cdf4f-e36d-9bef-927c-61c1d14500cd" as node ID 2018/06/23 00:03:45 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:a94cdf4f-e36d-9bef-927c-61c1d14500cd Address:127.0.0.1:8300}] 2018/06/23 00:03:45 [INFO] serf: EventMemberJoin: bogon.dc1 127.0.0.1 2018/06/23 00:03:45 [INFO] serf: EventMemberJoin: bogon 127.0.0.1 2018/06/23 00:03:45 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp) 2018/06/23 00:03:45 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "") 2018/06/23 00:03:45 [INFO] consul: Adding LAN server bogon (Addr: tcp/127.0.0.1:8300) (DC: dc1) 2018/06/23 00:03:45 [INFO] consul: Handled member-join event for server "bogon.dc1" in area "wan" 2018/06/23 00:03:45 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp) 2018/06/23 00:03:45 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp) 2018/06/23 00:03:45 [INFO] agent: started state syncer 2018/06/23 00:03:45 [WARN] raft: Heartbeat timeout from "" reached, starting election 2018/06/23 00:03:45 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2 2018/06/23 00:03:45 [DEBUG] raft: Votes needed: 1 2018/06/23 00:03:45 [DEBUG] raft: Vote granted from a94cdf4f-e36d-9bef-927c-61c1d14500cd in term 2. Tally: 1 2018/06/23 00:03:45 [INFO] raft: Election won. Tally: 1 2018/06/23 00:03:45 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state 2018/06/23 00:03:45 [INFO] consul: cluster leadership acquired 2018/06/23 00:03:45 [DEBUG] consul: Skipping self join check for "bogon" since the cluster is too small 2018/06/23 00:03:45 [INFO] consul: member 'bogon' joined, marking health alive 2018/06/23 00:03:45 [INFO] consul: New leader elected: bogon 2018/06/23 00:03:45 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically 2018/06/23 00:03:45 [INFO] agent: Synced node info 2018/06/23 00:03:46 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically 2018/06/23 00:03:46 [DEBUG] agent: Node info in sync 2018/06/23 00:03:46 [DEBUG] agent: Node info in sync
在另一個終端使用 consul members
, 能夠查看集羣成員信息。dom
$ consul members Node Address Status Type Build Protocol DC Segment bogon 127.0.0.1:8301 alive server 1.1.0 2 dc1 <all>
也能夠經過HTTP API 來查看Consul成員信息curl
$ curl localhost:8500/v1/catalog/nodes [ { "ID": "a94cdf4f-e36d-9bef-927c-61c1d14500cd", "Node": "bogon", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "Meta": { "consul-network-segment": "" }, "CreateIndex": 5, "ModifyIndex": 6 } ]
定義一個服務步驟tcp
$ sudo mkdir /etc/consul.d
定義一個服務名爲web, 運行端口爲80ide
$ echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' \ | sudo tee /etc/consul.d/web.json
重啓agent,並制定配置目錄工具
$ consul agent -dev -config-dir=/etc/consul.d ==> Starting Consul agent... ... [INFO] agent: Synced service 'web' ...
下面經過HTTP API的方式來查詢服務
$ curl http://localhost:8500/v1/catalog/service/web [ { "ID": "68530a9c-2434-9a80-32ac-e4e455086655", "Node": "bogon", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "NodeMeta": { "consul-network-segment": "" }, "ServiceID": "web", "ServiceName": "web", "ServiceTags": [ "rails" ], "ServiceAddress": "", "ServiceMeta": {}, "ServicePort": 80, "ServiceEnableTagOverride": false, "CreateIndex": 6, "ModifyIndex": 6 } ]
同時也能夠經過下面的HTTP API 來查詢服務的健康情況
$ curl 'http://localhost:8500/v1/health/service/web?passing' [ { "Node": { "ID": "68530a9c-2434-9a80-32ac-e4e455086655", "Node": "bogon", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "Meta": { "consul-network-segment": "" }, "CreateIndex": 5, "ModifyIndex": 6 }, "Service": { "ID": "web", "Service": "web", "Tags": [ "rails" ], "Address": "", "Meta": null, "Port": 80, "EnableTagOverride": false, "CreateIndex": 6, "ModifyIndex": 6 }, "Checks": [ { "Node": "bogon", "CheckID": "serfHealth", "Name": "Serf Health Status", "Status": "passing", "Notes": "", "Output": "Agent alive and reachable", "ServiceID": "", "ServiceName": "", "ServiceTags": [], "Definition": {}, "CreateIndex": 5, "ModifyIndex": 5 } ] } ]
能夠經過修改配置文件和發送SIGHUP
到agent, 更新服務的信息。 另外,也能夠經過HTTP API 的方式動態添加或者移除服務信息。