安裝consul
官網(wǎng)下載合適的版本即可
windows下直接下載解壓即可
linux下可以使用wget
命令下載
//1.下載壓縮包
wget https://releases.hashicorp.com/consul/1.4.2/consul_1.4.2_linux_amd64.zip
//2.解壓
unzip consul_1.4.2_linux_amd64.zip
//3.檢查安裝
./consul -v
Consul v1.4.2
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
啟動(dòng)consul
開發(fā)服務(wù)器模式-dev
consul支持開發(fā)服務(wù)器模式來快速啟動(dòng)單節(jié)點(diǎn)consul:
consul agent -dev
這種模式下啟動(dòng)的是內(nèi)存服務(wù)器,所有數(shù)據(jù)不會(huì)持久化. 僅用于開發(fā).
瀏覽器中訪問http://localhost:8500
即可看到consul管理界面,如下圖所示:
集群模式
consul使用raft協(xié)議實(shí)現(xiàn)一致性,集群至少部署三個(gè)節(jié)點(diǎn).
集群部署方式,agent先各自啟動(dòng),通過將某個(gè)agent加入cluster來實(shí)現(xiàn)集群部署.
集群規(guī)劃:
consul server1:172.16.22.2
consul server2:172.16.22.51
consul server3:10.45.82.76
啟動(dòng)consul服務(wù)器
172.16.22.2
[test@node1 consul]$ nohup ./consul agent -server -bind=172.16.22.2 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/test/consul/data -node=server-xh-1 -ui &
172.16.22.51
[test@node2 consul]$nohup ./consul agent -server -bind=172.16.22.51 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/test/consul/data -node=server-xh-2 -ui &
10.45.82.76
[test@node3 consul]$nohup ./consul agent -server -bind=10.45.82.76 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/test/consul/data -node=server-xh-3 -ui &
啟動(dòng)命令參數(shù)可參考Command-line Options
這個(gè)時(shí)候查看下consul agent,會(huì)發(fā)現(xiàn)每個(gè)agent下都只有各自自身,即每個(gè)agent都是相互獨(dú)立的.
172.16.22.2
[test@node1 consul]$ ./consul members
Node Address Status Type Build Protocol DC Segment
server-xh-1 172.16.22.2:8301 alive server 1.4.2 2 dc1 <all>
172.16.22.51
[test@node2 consul]$./consul members
Node Address Status Type Build Protocol DC Segment
server-xh-2 172.16.22.51:8301 alive server 1.4.2 2 dc1 <all>
10.45.82.76
[test@node3 consul]$./consul members
Node Address Status Type Build Protocol DC Segment
server-xh-3 10.45.82.76:8301 alive server 1.4.2 2 dc1 <all>
通過將172.16.22.2和172.16.22.51加入到10.45.82.76來實(shí)現(xiàn)集群部署
[test@node1 consul]$./consul join 10.45.82.76
Successfully joined cluster by contacting 1 nodes.
[test@node1 consul]$./consul members
Node Address Status Type Build Protocol DC Segment
server-01 172.16.22.2:8301 alive server 1.4.2 2 dc1 <all>
server-03 10.45.82.76:8301 alive server 1.4.2 2 dc1 <all>
[test@node2 consul]$./consul join 10.45.82.76
Successfully joined cluster by contacting 1 nodes.
[test@node2 consul]$./consul members
Node Address Status Type Build Protocol DC Segment
server-01 172.16.22.2:8301 alive server 1.4.2 2 dc1 <all>
server-02 172.16.22.51:8301 alive server 1.4.2 2 dc1 <all>
server-03 10.45.82.76:8301 alive server 1.4.2 2 dc1 <all>
至此,包含三個(gè)節(jié)點(diǎn)的consul cluster搭建好了
注意:集群部署的時(shí)候可以看下啟動(dòng)日志,能比較清楚的看到Leader選舉的流程等
啟動(dòng)consul時(shí)打印的一些重要信息
以下是開發(fā)服務(wù)器模式啟動(dòng)的部分日志:
==> Starting Consul agent...
==> Consul agent running!
//consul版本號(hào)
Version: 'v1.4.2'
//agent節(jié)點(diǎn)的ID,隨機(jī)生成的唯一ID
Node ID: '4e02ef52-9690-5fc6-b16c-de9724422f84'
//agent節(jié)點(diǎn)的唯一名稱,默認(rèn)情況下是主機(jī)名,可以使用-node指定節(jié)點(diǎn)名稱
Node name: 'localhost.localdomain'
//數(shù)據(jù)中心.consul支持多數(shù)據(jù)中心. 每個(gè)agent節(jié)點(diǎn)都需要指定其使用的數(shù)據(jù)中心,默認(rèn)為dc1.可以通過-datacenter指定使用的數(shù)據(jù)中心.
Datacenter: 'dc1' (Segment: '<all>')
//服務(wù)器.true表示當(dāng)前agent是以服務(wù)器模式運(yùn)行.false表示客戶端模式.
//Bootstrap引導(dǎo)模式,帶上啟動(dòng)參數(shù)-bootstrap可以開啟引導(dǎo)模式.一個(gè)數(shù)據(jù)中心下最多只有一個(gè)agent節(jié)點(diǎn)可以設(shè)置為引導(dǎo)模式,因?yàn)樵谶@種模式下agent節(jié)點(diǎn)可以自選為Raft leader,多個(gè)會(huì)導(dǎo)致不一致性.
Server: true (Bootstrap: false)
//客戶端連接agent節(jié)點(diǎn)的IP地址及端口號(hào).默認(rèn)情況下只綁定到本機(jī).
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
//集群中consul agent節(jié)點(diǎn)之間通信的IP地址和端口
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:
2019/02/18 18:49:41 [DEBUG] agent: Using random ID "4e02ef52-9690-5fc6-b16c-de9724422f84" as node ID
2019/02/18 18:49:41 [WARN] agent: Node name "localhost.localdomain" will not be discoverable via DNS due to invalid characters. Valid characters include all alpha-numerics and dashes.
2019/02/18 18:49:41 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:4e02ef52-9690-5fc6-b16c-de9724422f84 Address:127.0.0.1:8300}]
2019/02/18 18:49:41 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2019/02/18 18:49:41 [INFO] serf: EventMemberJoin: localhost.localdomain.dc1 127.0.0.1
2019/02/18 18:49:41 [INFO] serf: EventMemberJoin: localhost.localdomain 127.0.0.1
2019/02/18 18:49:41 [INFO] consul: Handled member-join event for server "localhost.localdomain.dc1" in area "wan"
2019/02/18 18:49:41 [DEBUG] agent/proxy: managed Connect proxy manager started
2019/02/18 18:49:41 [INFO] consul: Adding LAN server localhost.localdomain (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2019/02/18 18:49:41 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2019/02/18 18:49:41 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2019/02/18 18:49:41 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2019/02/18 18:49:41 [INFO] agent: started state syncer
2019/02/18 18:49:41 [INFO] agent: Started gRPC server on 127.0.0.1:8502 (tcp)
2019/02/18 18:49:41 [WARN] raft: Heartbeat timeout from "" reached, starting election
2019/02/18 18:49:41 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2019/02/18 18:49:41 [DEBUG] raft: Votes needed: 1
2019/02/18 18:49:41 [DEBUG] raft: Vote granted from 4e02ef52-9690-5fc6-b16c-de9724422f84 in term 2. Tally: 1
2019/02/18 18:49:41 [INFO] raft: Election won. Tally: 1
2019/02/18 18:49:41 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2019/02/18 18:49:41 [INFO] consul: cluster leadership acquired
2019/02/18 18:49:41 [INFO] consul: New leader elected: localhost.localdomain
2019/02/18 18:49:41 [INFO] connect: initialized primary datacenter CA with provider "consul"
2019/02/18 18:49:41 [DEBUG] consul: Skipping self join check for "localhost.localdomain" since the cluster is too small
2019/02/18 18:49:41 [INFO] consul: member 'localhost.localdomain' joined, marking health alive
2019/02/18 18:49:42 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2019/02/18 18:49:42 [INFO] agent: Synced node info
2019/02/18 18:49:42 [DEBUG] agent: Node info in sync
2019/02/18 18:49:44 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2019/02/18 18:49:44 [DEBUG] agent: Node info in sync
通過啟動(dòng)日志可以發(fā)現(xiàn):
1.建議使用啟動(dòng)參數(shù)-node
指定agent節(jié)點(diǎn)名稱,因?yàn)槟J(rèn)的主機(jī)名可能包含一些無效字符.節(jié)點(diǎn)名稱有效字符包括所有字母數(shù)字和破折號(hào).
2.一致性協(xié)議使用Raft協(xié)議
consul web ui
我們可以通過web UI查看集群信息
注意,啟動(dòng)agent的時(shí)候帶參數(shù)-ui
即可啟動(dòng)consul自帶的web管理界面,默認(rèn)端口號(hào)8500
http://10.45.82.76:8500
consul command
查看下consul命令支持的功能:
[test@node3 consul]$./consul --help
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
//將consul server加入consul cluster ./consul join 172.16.22.2
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
//優(yōu)雅的停掉consul server ./consul leave
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
//查看當(dāng)前consul下的成員 ./consul members
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
可以通過./consul command --help
查看具體某個(gè)指令支持的參數(shù).比如./consul agent --help