集羣中的每一個節點必須有位置名稱,默認狀況下consul使用主機名做爲名稱,咱們也可使用-node命令指
node
1、環境web
192.168.0.149 | server01 |
192.168.0.161 | server02 |
192.192.0.174 | server03 |
192.168.0.239 | client01 |
2、構建集羣json
啓動第一個節點以server模式運行bootstrap
[root@consul-1 ~]# consul agent -ui-dir /root/consul_ui -server -bootstrap-expect 3 -data-dir /tmp/consul -bind=192.168.0.149 -node=server01 -client 0.0.0.0
啓動第二個節點以server模式運行curl
[root@consul-2 ~]# consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -bind=192.168.0.161 -node=server02
啓動第三個節點以server模式運行ide
[root@consul-3 ~]# consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -bind=192.168.0.174 -node=server03
啓動第四個節點以client模式運行ui
[root@slave-1 ~]# consul agent -data-dir /tmp/consul -bind=192.168.0.239 -node=client01
server端加入集羣
google
NOTE:按照上面的啓動方式-bootstrap-expect設置爲3,若是server端要選舉leader必須,集羣中必須得有三個節點,如今他們還互相不識別,因此須要找一個節點加入另外兩個節點url
[root@consul-3 ~]# consul join 192.168.0.149 192.168.0.161
client加入集羣spa
NOTE:server端互相識別並選取leader以後client也要加入集羣
[root@slave-1 ~]# consul join 192.168.0.149
查看集羣
[root@consul-3 ~]# consul members Node Address Status Type Build Protocol DC server01 192.168.0.149:8301 alive server 0.6.4 2 dc1 server02 192.168.0.161:8301 alive server 0.6.4 2 dc1 server03 192.168.0.174:8301 alive server 0.6.4 2 dc1 client01 192.168.0.239:8301 alive client 0.6.4 2 dc1
-server |
以服務端模式運行 |
-bootstrap-expect | 指按期望加入的節點數,只有達到這個數字纔會觸發選舉 |
-data-dir | 指定數據存放位置 |
-node | hiding節點名 |
-bind | 指定綁定的IP |
-config-dir | 指定配置目錄 |
-client | 以客戶端模式運行 |
NOTE:只有達到指按期望加入的節點數後纔會出發選舉
三、基本操做
脫離集羣
可使用ctrl+c來平滑退出,也可使用kill退出,區別是主動告知其餘節點本身離開,和被其餘節點標記爲失效,被發現離開
健康檢查
健康檢查對於避免將請求發送給運行不正常的服務是一個至關關鍵的機制
和服務同樣,有兩種方式來定義檢查
·經過配置文件
·使用HTTP API
定義檢查
在第一個節點的配置文件目錄中建立兩個文件
[root@consul-1 ~]# echo '{"check": {"name": "ping", \ > "script": "ping -c1 google.com >/dev/null", "interval": "30s"}}' \ > >/etc/consul.d/ping.json [root@consul-1 ~]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80, \ > "check": {"script": "curl localhost >/dev/null 2>&1", "interval": "10s"}}}' \ > >/etc/consul.d/web.json
以上兩個檢查若是文件退出狀態碼非0就標記爲不健康
重載配置
經過給consul SIGHUP的信號來使配置重載
echo {"check": {"name": "ping","script": "ping -c1 soft.dog >/dev/null", "interval": "30s"}} > /etc/consul.d/ping.json echo {"service": {"name": "web", "tags": ["rails"], "port": 80,"check": {"script": "curl localhost >/dev/null 2>&1", "interval": "10s"}}} > /etc/consul.d/web.json cat /etc/consul.d/ping.json {"check": {"name": "ping","script": "ping -c1 soft.dog >/dev/null", "interval": "30s"}} cat /etc/consul.d/web.json {"service": {"name": "web", "tags": ["rails"], "port": 80,"check": {"script": "curl localhost >/dev/null 2>&1", "interval": "10s"}}}
此時能夠觀察consul-2日誌的輸出
2016/03/23 19:26:39 [INFO] agent: Synced service 'web' 2016/03/23 19:26:40 [INFO] agent: Synced check 'ping' 2016/03/23 19:26:42 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:26:53 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:27:03 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:27:10 [INFO] agent: Synced check 'ping' 2016/03/23 19:27:13 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:27:23 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:27:33 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:27:43 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:27:50 [WARN] agent: Check 'ping' is now warning 2016/03/23 19:27:50 [INFO] agent: Synced check 'ping' 2016/03/23 19:27:54 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:28:04 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:28:14 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:28:20 [INFO] agent: Synced check 'ping' 2016/03/23 19:28:24 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:28:34 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:28:44 [WARN] agent: Check 'service:web' is now critical 2016/03/23 19:28:54 [WARN] agent: Check 'service:web' is now critical
從新加載配置後,兩個檢查腳本都成功載入了
ping腳本檢查正常,百度是能ping通的,同時因爲咱們並無真正在本地啓web服務,80端口不存在,也不提供內容,因此檢查結果是狀態不正常
查看狀態
可使用HTTP API來檢查配置
[root@consul-2 ~]# curl http://localhost:8500/v1/health/state/critical [{"Node":"agent-two","CheckID":"service:web","Name":"Service 'web' check","Status":"critical","Notes":"","Output":"","ServiceID":"web","ServiceName":"web","CreateIndex":1206,"ModifyIndex":1206}]
能夠在任意一個節點上進行檢查
和服務同樣,健康也可使用HTTP API來動態的進行添加、刪除和修改