原文:
https://blog.csdn.net/u010046908/article/details/61916389
1 Consul簡介
Consul 是 HashiCorp 公司推出的開源工具谋右,用于實現(xiàn)分布式系統(tǒng)的服務發(fā)現(xiàn)與配置硬猫。與其他分布式服務注冊與發(fā)現(xiàn)的方案,Consul的方案更“一站式”改执,內(nèi)置了服務注冊與發(fā)現(xiàn)框 架啸蜜、分布一致性協(xié)議實現(xiàn)、健康檢查辈挂、Key/Value存儲衬横、多數(shù)據(jù)中心方案,不再需要依賴其他工具(比如ZooKeeper等)终蒂。使用起來也較 為簡單蜂林。Consul使用Go語言編寫,因此具有天然可移植性(支持Linux拇泣、windows和Mac OS X)噪叙;安裝包僅包含一個可執(zhí)行文件,方便部署霉翔,與Docker等輕量級容器可無縫配合 睁蕾。
2 Consul安裝
安裝環(huán)境:
mac:64bit(查看mac位數(shù):打開終端–>”uname -a”)
consul_0.6.4_darwin_amd64.zip和consul_0.6.4_web_ui.zip,從consul官網(wǎng)https://www.consul.io/downloads.html進行下載就好(選擇好OS和位數(shù))
1债朵、解壓consul_0.6.4_darwin_amd64.zip
2子眶、將解壓后的二進制文件consul(上邊畫紅框的部分拷貝到/usr/local/bin下)
sudo scp consul /usr/local/bin/
說明:使用sudo是因為權限問題。
3序芦、查看是否安裝成功,
直接在家目錄下執(zhí)行consul命令即可臭杰。出現(xiàn)如下結(jié)果,表示安裝成功谚中。
lidongdeMacBook-Pro:bin lidong$ consul
usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
configtest Validate config file
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
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
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
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
snapshot Saves, restores and inspects snapshots of Consul server state
version Prints the Consul version
watch Watch for changes in Consul
3 Consul 啟動
1渴杆、執(zhí)行命令
./consul agent -dev # -dev表示開發(fā)模式運行射窒,另外還有-server表示服務模式運行
查看顯示結(jié)果:
lidongdeMacBook-Pro:bin lidong$ consul agent -dev
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Version: 'v0.7.5'
Node ID: 'c67a8d03-deac-35b8-8f68-06ff7b687215'
Node name: 'lidongdeMacBook-Pro.local'
Datacenter: 'dc1'
Server: true (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream in as it occurs:
2017/03/13 12:44:31 [DEBUG] Using unique ID "c67a8d03-deac-35b8-8f68-06ff7b687215" from host as node ID
2017/03/13 12:44:31 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]
2017/03/13 12:44:31 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2017/03/13 12:44:31 [INFO] serf: EventMemberJoin: lidongdeMacBook-Pro.local 127.0.0.1
2017/03/13 12:44:31 [INFO] consul: Adding LAN server lidongdeMacBook-Pro.local (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/03/13 12:44:31 [INFO] serf: EventMemberJoin: lidongdeMacBook-Pro.local.dc1 127.0.0.1
2017/03/13 12:44:31 [INFO] consul: Adding WAN server lidongdeMacBook-Pro.local.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/03/13 12:44:36 [WARN] raft: Heartbeat timeout from "" reached, starting election
2017/03/13 12:44:36 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2017/03/13 12:44:36 [DEBUG] raft: Votes needed: 1
2017/03/13 12:44:36 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1
2017/03/13 12:44:36 [INFO] raft: Election won. Tally: 1
2017/03/13 12:44:36 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2017/03/13 12:44:36 [INFO] consul: cluster leadership acquired
2017/03/13 12:44:36 [INFO] consul: New leader elected: lidongdeMacBook-Pro.local
2017/03/13 12:44:36 [DEBUG] consul: reset tombstone GC to index 3
2017/03/13 12:44:36 [INFO] consul: member 'lidongdeMacBook-Pro.local' joined, marking health alive
2017/03/13 12:44:37 [INFO] agent: Synced service 'consul'
2017/03/13 12:44:37 [DEBUG] agent: Node info in sync
2017/03/13 12:44:39 [INFO] agent.rpc: Accepted client: 127.0.0.1:54095
2017/03/13 12:44:50 [DEBUG] http: Request GET /v1/catalog/datacenters (580.862μs) from=127.0.0.1:54114
2017/03/13 12:44:50 [DEBUG] http: Request GET /v1/catalog/datacenters (34.955μs) from=127.0.0.1:54114
2017/03/13 12:44:50 [DEBUG] http: Request GET /v1/internal/ui/nodes?dc=dc1&token=<hidden> (1.024476ms) from=127.0.0.1:54112
2017/03/13 12:44:50 [DEBUG] http: Request GET /v1/coordinate/nodes?dc=dc1&token=<hidden> (361.25μs) from=127.0.0.1:54111
2017/03/13 12:44:50 [DEBUG] http: Request GET /v1/internal/ui/services?dc=dc1&token=<hidden> (315.405μs) from=127.0.0.1:54111
2017/03/13 12:44:53 [DEBUG] http: Request GET /v1/internal/ui/nodes?dc=dc1&token=<hidden> (140.095μs) from=127.0.0.1:54111
2017/03/13 12:44:54 [DEBUG] http: Request GET /v1/kv/?keys&seperator=/&dc=dc1&token=<hidden> (1.368486ms) from=127.0.0.1:54111
2017/03/13 12:44:55 [DEBUG] http: Request GET /v1/acl/list?dc=dc1&token=<hidden> (9.253μs) from=127.0.0.1:54111
2017/03/13 12:44:57 [DEBUG] http: Request GET /v1/internal/ui/services?dc=dc1&token=<hidden> (109.196μs) from=127.0.0.1:54111
2017/03/13 12:44:59 [DEBUG] http: Request GET /v1/internal/ui/services?dc=dc1&token=<hidden> (98.48μs) from=127.0.0.1:54111
2017/03/13 12:45:58 [DEBUG] agent: Service 'consul' in sync
2017/03/13 12:45:58 [DEBUG] agent: Node info in sync
2017/03/13 12:47:55 [DEBUG] agent: Service 'consul' in sync
2017/03/13 12:47:55 [DEBUG] agent: Node info in sync
說明:
-dev(該節(jié)點的啟動不能用于生產(chǎn)環(huán)境,因為該模式下不會持久化任何狀態(tài))将塑,該啟動模式僅僅是為了快速便捷的啟動單節(jié)點consul
該節(jié)點處于server模式
該節(jié)點是leader
該節(jié)點是一個健康節(jié)點
2、查看consul cluster中的每一個consul節(jié)點的信息
lidongdeMacBook-Pro:~ lidong$ consul members
Node Address Status Type Build Protocol DC
lidongdeMacBook-Pro.local 127.0.0.1:8301 alive server 0.7.5 2 dc1
說明:
- Address:節(jié)點地址
- Status:alive表示節(jié)點健康
- Type:server運行狀態(tài)是server狀態(tài)
- DC:dc1表示該節(jié)點屬于DataCenter1
注意:
members命令的輸出是基于gossip協(xié)議的蝌麸,并且是最終一致的(也就是說点寥,某一個時刻你去運用該命令查到的consul節(jié)點的狀態(tài)信息可能是有誤的)
輸入http://127.0.0.1:8500/ui/ 訪問Consul,可查看到如下界面:
4 停止服務(優(yōu)雅退出)
命令:CTRL+C
^C==> Caught signal: interrupt
2017/03/13 12:50:42 [DEBUG] http: Shutting down http server (127.0.0.1:8500)
2017/03/13 12:50:42 [INFO] agent: requesting shutdown
2017/03/13 12:50:42 [INFO] consul: shutting down server
2017/03/13 12:50:42 [WARN] serf: Shutdown without a Leave
2017/03/13 12:50:42 [WARN] serf: Shutdown without a Leave
2017/03/13 12:50:42 [ERR] dns: error starting tcp server: accept tcp 127.0.0.1:8600: use of closed network connection
2017/03/13 12:50:42 [INFO] agent: shutdown complete
說明:
- 該節(jié)點離開后来吩,會通知cluster中的其他節(jié)點
注意:
- 安裝部分參考自:https://www.consul.io/intro/getting-started/install.html
- 啟動和停止服務部分參考自:https://www.consul.io/intro/getting-started/agent.html
5 Consul常用命令
命令 | 解釋 | 示例 |
---|---|---|
agent | 運行一個consul agent | consul agent -dev |
join | 將agent加入到consul集群 | consul join IP |
members | 列出consul cluster集群中的members | consul members |
leave | 將節(jié)點移除所在集群 | consul leave |
consul agent 命令詳解
輸入consul agent --help
敢辩,可以看到consul agent
的選項,如下:
-advertise=addr Sets the advertise address to use
-advertise-wan=addr Sets address to advertise on wan instead of advertise addr
-atlas=org/name Sets the Atlas infrastructure name, enables SCADA.
-atlas-join Enables auto-joining the Atlas cluster
-atlas-token=token Provides the Atlas API token
-atlas-endpoint=1.2.3.4 The address of the endpoint for Atlas integration.
-bootstrap Sets server to bootstrap mode
-bind=0.0.0.0 Sets the bind address for cluster communication
-http-port=8500 Sets the HTTP API port to listen on
-bootstrap-expect=0 Sets server to expect bootstrap mode.
-client=127.0.0.1 Sets the address to bind for client access.
This includes RPC, DNS, HTTP and HTTPS (if configured)
-config-file=foo Path to a JSON file to read configuration from.
This can be specified multiple times.
-config-dir=foo Path to a directory to read configuration files
from. This will read every file ending in ".json"
as configuration in this directory in alphabetical
order. This can be specified multiple times.
-data-dir=path Path to a data directory to store agent state
-dev Starts the agent in development mode.
-recursor=1.2.3.4 Address of an upstream DNS server.
Can be specified multiple times.
-dc=east-aws Datacenter of the agent (deprecated: use 'datacenter' instead).
-datacenter=east-aws Datacenter of the agent.
-encrypt=key Provides the gossip encryption key
-join=1.2.3.4 Address of an agent to join at start time.
Can be specified multiple times.
-join-wan=1.2.3.4 Address of an agent to join -wan at start time.
Can be specified multiple times.
-retry-join=1.2.3.4 Address of an agent to join at start time with
retries enabled. Can be specified multiple times.
-retry-interval=30s Time to wait between join attempts.
-retry-max=0 Maximum number of join attempts. Defaults to 0, which
will retry indefinitely.
-retry-join-wan=1.2.3.4 Address of an agent to join -wan at start time with
retries enabled. Can be specified multiple times.
-retry-interval-wan=30s Time to wait between join -wan attempts.
-retry-max-wan=0 Maximum number of join -wan attempts. Defaults to 0, which
will retry indefinitely.
-log-level=info Log level of the agent.
-node=hostname Name of this node. Must be unique in the cluster
-protocol=N Sets the protocol version. Defaults to latest.
-rejoin Ignores a previous leave and attempts to rejoin the cluster.
-server Switches agent to server mode.
-syslog Enables logging to syslog
-ui Enables the built-in static web UI server
-ui-dir=path Path to directory containing the Web UI resources
-pid-file=path Path to file to store agent PID
consul agent
命令的常用選項弟疆,如下:
-
-data-dir
- 作用:指定agent儲存狀態(tài)的數(shù)據(jù)目錄
- 這是所有agent都必須的
- 對于server尤其重要戚长,因為他們必須持久化集群的狀態(tài)
-
-config-dir
- 作用:指定service的配置文件和檢查定義所在的位置
- 通常會指定為”某一個路徑/consul.d”(通常情況下,.d表示一系列配置文件存放的目錄)
-
-config-file
- 作用:指定一個要裝載的配置文件
- 該選項可以配置多次怠苔,進而配置多個配置文件(后邊的會合并前邊的同廉,相同的值覆蓋)
-
-dev
- 作用:創(chuàng)建一個開發(fā)環(huán)境下的server節(jié)點
- 該參數(shù)配置下,不會有任何持久化操作柑司,即不會有任何數(shù)據(jù)寫入到磁盤
- 這種模式不能用于生產(chǎn)環(huán)境(因為第二條)
-
-bootstrap-expect
- 作用:該命令通知consul server我們現(xiàn)在準備加入的server節(jié)點個數(shù)迫肖,該參數(shù)是為了延遲日志復制的啟動直到我們指定數(shù)量的server節(jié)點成功的加入后啟動。
-
-node
- 作用:指定節(jié)點在集群中的名稱
- 該名稱在集群中必須是唯一的(默認采用機器的host)
- 推薦:直接采用機器的IP
-
-bind
- 作用:指明節(jié)點的IP地址
- 有時候不指定綁定IP攒驰,會報
Failed to get advertise address: Multiple private IPs found. Please configure one.
的異常
-
-server
- 作用:指定節(jié)點為server
- 每個數(shù)據(jù)中心(DC)的server數(shù)推薦至少為1蟆湖,至多為5
- 所有的server都采用raft一致性算法來確保事務的一致性和線性化,事務修改了集群的狀態(tài)玻粪,且集群的狀態(tài)保存在每一臺server上保證可用性
- server也是與其他DC交互的門面(gateway)
-
-client
- 作用:指定節(jié)點為client隅津,指定客戶端接口的綁定地址,包括:HTTP劲室、DNS伦仍、RPC
- 默認是127.0.0.1,只允許回環(huán)接口訪問
- 若不指定為-server痹籍,其實就是-client
-
-join
- 作用:將節(jié)點加入到集群
-
-datacenter(老版本叫-dc呢铆,-dc已經(jīng)失效)
- 作用:指定機器加入到哪一個數(shù)據(jù)中心中
使用-client 參數(shù)可指定允許客戶端使用什么ip去訪問,例如-client 192.168.11.143 表示可以使用http://192.168.11.143:8500/ui 去訪問蹲缠。
我們嘗試一下:
consul agent -dev -client 192.168.11.143
發(fā)現(xiàn)果然可以使用http://192.168.11.143:8500/ui 訪問了棺克。
6 Consul 的高可用
Consul Cluster集群架構圖如下:
這邊準備了三臺CentOS 7的虛擬機,主機規(guī)劃如下线定,供參考:
主機名稱 | IP | 作用 | 是否允許遠程訪問 |
---|---|---|---|
node0 | 192.168.11.143 | consul server | 是 |
node1 | 192.168.11.144 | consul client | 否 |
node2 | 192.168.11.145 | consul client | 是 |
6.1 搭建步驟:
- 啟動node0機器上的Consul(node0機器上執(zhí)行):
consul agent -data-dir /tmp/node0 -node=node0 -bind=192.168.11.143 -datacenter=dc1 -ui -client=192.168.11.143 -server -bootstrap-expect 1
- 啟動node1機器上的Consul(node1機器上執(zhí)行):
consul agent -data-dir /tmp/node1 -node=node1 -bind=192.168.11.144 -datacenter=dc1 -ui
- 啟動node2機器上的Consul(node2機器上執(zhí)行):
consul agent -data-dir /tmp/node2 -node=node2 -bind=192.168.11.145 -datacenter=dc1 -ui -client=192.168.11.145
- 將node1節(jié)點加入到node0上(node1機器上執(zhí)行):
consul join 192.168.11.143
- 將node2節(jié)點加入到node0上(node2機器上執(zhí)行):
consul join -rpc-addr=192.168.11.145:8400 192.168.11.143
- 這樣一個簡單的Consul集群就搭建完成了娜谊,在node1上查看當前集群節(jié)點:
consul members -rpc-addr=192.168.11.143:8400
結(jié)果如下:
Node Address Status Type Build Protocol DC
node0 192.168.11.143:8301 alive server 0.7.0 2 dc1
node1 192.168.11.144:8301 alive client 0.7.0 2 dc1
node2 192.168.11.145:8301 alive client 0.7.0 2 dc1
說明集群已經(jīng)搭建成功了。
我們分析一下斤讥,為什么第5步和第6步需要加-rpc-addr
選項纱皆,而第4步不需要加任何選項呢湾趾?原因是-client
指定了客戶端接口的綁定地址,包括:HTTP派草、DNS搀缠、RPC,而consul join
近迁、consul members
都是通過RPC與Consul交互的艺普。
6.2 訪問集群
如上,我們?nèi)齻€節(jié)點都加了-ui
參數(shù)啟動了內(nèi)建的界面鉴竭。我們可以通過:http://192.168.11.143:8500/ui/
或者http://192.168.11.145:8500/ui/
進行訪問歧譬,也可以在node1機器上通過http://127.0.0.1:8500/ui/
進行訪問,原因是node1沒有開啟遠程訪問