consul系列一之基礎

一、consul原理

consul的功能

·服務發現

·健康檢查

·支持多數據中心

·key/value存儲

consul的使用場景

·docker實例的註冊與配置共享

·coreos實例的註冊與配置共享

consul的優勢

·使用 Raft 算法來保證一致性, 比複雜的 Paxos 算法更直接. 相比較而言, zookeeper 採用的是 Paxos, 而 etcd 使用的則是 Raft

·支持多數據中心,內外網的服務採用不同的端口進行監聽。 多數據中心集羣可以避免單數據中心的單點故障,而其部署則需要考慮網絡延遲, 分片等情況等. zookeeper 和 etcd 均不提供多數據中心功能的支持

·支持健康檢查. etcd 不提供此功能

·官方提供web管理界面, etcd 無此功能

·支持 http 和 dns 協議接口. zookeeper 的集成較爲複雜, etcd 只支持 http 協議

consul發現機制

當一個Consul代理啓動後,它並不知道其它節點的存在,它是一個孤立的 單節點集羣,如果想感知到其它節點的存在,它必須加入到一個現存的集羣,要加入到一個現存的集羣,它只用加入集羣中任意一個現存的成員,當加入一個現存的成員後,會通過成員間的通訊很快發現集羣中的其它成員,一個Consul代理可以加入任意一個代理,而不僅僅是服務節點

consul角色

client:註冊服務、健康檢查並將數據發送到服務端

server:保存配置信息, 高可用集羣, 在局域網內與本地客戶端通訊, 通過廣域網與其他數據中心通訊

二、構建consul

2.1 安裝consul

[root@consul-1 ~]# unzip consul_0.6.3_linux_amd64.zip
[root@consul-1 ~]# mv consul   /usr/locao/bin/.

2.2 單節點啓動consul

[root@consul-1 ~]# consul agent -dev  -bind=192.168.0.149
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
         Node name: 'consul-1'
        Datacenter: 'dc1'
            Server: true (bootstrap: false)
       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
      Cluster Addr: 192.168.0.149 (LAN: 8301, WAN: 8302)
    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
             Atlas: <disabled>

==> Log data will now stream in as it occurs:

    2016/03/23 15:17:27 [INFO] serf: EventMemberJoin: consul-1 192.168.0.149
    2016/03/23 15:17:27 [INFO] serf: EventMemberJoin: consul-1.dc1 192.168.0.149
    2016/03/23 15:17:27 [INFO] raft: Node at 192.168.0.149:8300 [Follower] entering Follower state
    2016/03/23 15:17:27 [INFO] consul: adding LAN server consul-1 (Addr: 192.168.0.149:8300) (DC: dc1)
    2016/03/23 15:17:27 [INFO] consul: adding WAN server consul-1.dc1 (Addr: 192.168.0.149:8300) (DC: dc1)
    2016/03/23 15:17:27 [ERR] agent: failed to sync remote state: No cluster leader
    2016/03/23 15:17:29 [WARN] raft: Heartbeat timeout reached, starting election
    2016/03/23 15:17:29 [INFO] raft: Node at 192.168.0.149:8300 [Candidate] entering Candidate state
    2016/03/23 15:17:29 [DEBUG] raft: Votes needed: 1
    2016/03/23 15:17:29 [DEBUG] raft: Vote granted from 192.168.0.149:8300. Tally: 1
    2016/03/23 15:17:29 [INFO] raft: Election won. Tally: 1
    2016/03/23 15:17:29 [INFO] raft: Node at 192.168.0.149:8300 [Leader] entering Leader state
    2016/03/23 15:17:29 [INFO] raft: Disabling EnableSingleNode (bootstrap)
    2016/03/23 15:17:29 [DEBUG] raft: Node 192.168.0.149:8300 updated peer set (2): [192.168.0.149:8300]
    2016/03/23 15:17:29 [INFO] consul: cluster leadership acquired
    2016/03/23 15:17:29 [DEBUG] consul: reset tombstone GC to index 2
    2016/03/23 15:17:29 [INFO] consul: member 'consul-1' joined, marking health alive
    2016/03/23 15:17:29 [INFO] consul: New leader elected: consul-1
    2016/03/23 15:17:29 [INFO] agent: Synced service 'consul'
    2016/03/23 15:18:46 [DEBUG] agent: Service 'consul' in sync

2.3 查看集羣中的成員

[root@consul-1 ~]# consul members
Node      Address             Status  Type    Build  Protocol  DC
consul-1  192.168.0.149:8301  alive   server  0.6.4  2         dc1

2.4 查詢節點

可以通過HTTP API和DNS API查詢節點

如果使用DNS查詢格式爲:name.node.consul或者name.node.datacenter.consul

[root@consul-1 ~]# dig @127.0.0.1 -p 8600  consul-1.node.consul

; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.3 <<>> @127.0.0.1 -p 8600 consul-1.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6969
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;consul-1.node.consul.		IN	A

;; ANSWER SECTION:
consul-1.node.consul.	0	IN	A	192.168.0.149

;; Query time: 1 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Mar 23 15:50:19 CST 2016
;; MSG SIZE  rcvd: 74

2.5 脫離節點:可以使用ctrl+c來平滑退出,也可以使用kill退出,區別是主動告知其他節點自己離開,和被其他節點標記爲失效,被發現離開

三、服務註冊

服務註冊有兩種方式

·服務定義:是服務註冊最常用的方法

·HTTP API:通過HTTP API方式註冊

3.1 創建配置文件目錄

[root@consul-1 ~]# mkdir /etc/consul.d/

NOTE:consul.d是配置文件目錄,表示裏面有若干個配置文件,這是命名規範

3.2 編寫服務定義配置文件

[root@consul-1 ~]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' \
 > /etc/consul.d/web.json

配置文件內容解釋:有個名稱爲web的服務運行在端口80,另外給他一個標籤作爲額外的方法查詢服務

3.3 重啓代理並加載配置文件

[root@consul-1 ~]# consul agent -dev  -bind=192.168.0.149  -config-dir=/etc/consul.d/
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
         Node name: 'consul-1'
        Datacenter: 'dc1'
            Server: true (bootstrap: false)
       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
      Cluster Addr: 192.168.0.149 (LAN: 8301, WAN: 8302)
    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
             Atlas: <disabled>

==> Log data will now stream in as it occurs:

    2016/03/23 16:18:57 [INFO] serf: EventMemberJoin: consul-1 192.168.0.149
    2016/03/23 16:18:57 [INFO] serf: EventMemberJoin: consul-1.dc1 192.168.0.149
    2016/03/23 16:18:57 [INFO] raft: Node at 192.168.0.149:8300 [Follower] entering Follower state
    2016/03/23 16:18:57 [INFO] consul: adding LAN server consul-1 (Addr: 192.168.0.149:8300) (DC: dc1)
    2016/03/23 16:18:57 [INFO] consul: adding WAN server consul-1.dc1 (Addr: 192.168.0.149:8300) (DC: dc1)
    2016/03/23 16:18:57 [ERR] agent: failed to sync remote state: No cluster leader
    2016/03/23 16:18:58 [WARN] raft: Heartbeat timeout reached, starting election
    2016/03/23 16:18:58 [INFO] raft: Node at 192.168.0.149:8300 [Candidate] entering Candidate state
    2016/03/23 16:18:58 [DEBUG] raft: Votes needed: 1
    2016/03/23 16:18:58 [DEBUG] raft: Vote granted from 192.168.0.149:8300. Tally: 1
    2016/03/23 16:18:58 [INFO] raft: Election won. Tally: 1
    2016/03/23 16:18:58 [INFO] raft: Node at 192.168.0.149:8300 [Leader] entering Leader state
    2016/03/23 16:18:58 [INFO] raft: Disabling EnableSingleNode (bootstrap)
    2016/03/23 16:18:58 [DEBUG] raft: Node 192.168.0.149:8300 updated peer set (2): [192.168.0.149:8300]
    2016/03/23 16:18:58 [INFO] consul: cluster leadership acquired
    2016/03/23 16:18:58 [DEBUG] consul: reset tombstone GC to index 2
    2016/03/23 16:18:58 [INFO] consul: member 'consul-1' joined, marking health alive
    2016/03/23 16:18:58 [INFO] consul: New leader elected: consul-1
    2016/03/23 16:19:01 [INFO] agent: Synced service 'consul'
    2016/03/23 16:19:01 [INFO] agent: Synced service 'web'

輸出中表示Synced service‘web’服務註冊成功,如果想註冊多個服務,可以創建多個配置文件

3.4 通過HTTP API註冊服務

curl -X PUT http://127.0.0.1:8500/v1/agent/service/register -i -H "Content-Type:application/json" -H "Accept:application/json" -d '{"ID":"web","Name" :"etcd","Tags":["2.2.2","cn-north-1","develop"],"Address":"10.10.10.1","Port":8080}'

3.5 使用DNS API查詢服務

可以使用DNS API的方式查看服務的IP,這樣只能看到服務的IP,而不能看到Port

dig  @127.0.0.1 -p  8600 web.service.consul

; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.3 <<>> @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18524
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;web.service.consul.		IN	A

;; ANSWER SECTION:
web.service.consul.	0	IN	A	192.168.0.149

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Mar 23 16:24:48 CST 2016
;; MSG SIZE  rcvd: 70

如果要看到服務的IP和Port,主需要加上SRV

[root@consul-1 ~]# dig  @127.0.0.1 -p  8600 web.service.consul SRV
; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.3 <<>> @127.0.0.1 -p 8600 web.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2987
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;web.service.consul.		IN	SRV

;; ANSWER SECTION:
web.service.consul.	0	IN	SRV	1 1 80 consul-1.node.dc1.consul.

;; ADDITIONAL SECTION:
consul-1.node.dc1.consul. 0	IN	A	192.168.0.149

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Mar 23 16:27:17 CST 2016
;; MSG SIZE  rcvd: 138

使用DNS API標籤過濾服務

[root@consul-1 ~]# dig  @127.0.0.1 -p  8600 rails.web.service.consul
; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.3 <<>> @127.0.0.1 -p 8600 rails.web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51120
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;rails.web.service.consul.	IN	A

;; ANSWER SECTION:
rails.web.service.consul. 0	IN	A	192.168.0.149

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Mar 23 16:29:21 CST 2016
;; MSG SIZE  rcvd: 82

3.6使用HTTP API查詢服務

[root@consul-1 ~]# curl http://localhost:8500/v1/catalog/service/web
[{"Node":"consul-1","Address":"192.168.0.149","ServiceID":"web","ServiceName":"web","ServiceTags":["rails"],"ServiceAddress":"","ServicePort":80,"ServiceEnableTagOverride":false,"CreateIndex":5,"ModifyIndex":5}]

3.7查詢服務的健康狀態

[root@consul-1 ~]# curl 'http://localhost:8500/v1/health/service/web?passing'
[{"Node":{"Node":"consul-1","Address":"192.168.0.149","TaggedAddresses":{"wan":"192.168.0.149"},"CreateIndex":3,"ModifyIndex":5},"Service":{"ID":"web","Service":"web","Tags":["rails"],"Address":"","Port":80,"EnableTagOverride":false,"CreateIndex":5,"ModifyIndex":5},"Checks":[{"Node":"consul-1","CheckID":"serfHealth","Name":"Serf Health Status","Status":"passing","Notes":"","Output":"Agent alive and reachable","ServiceID":"","ServiceName":"","CreateIndex":3,"ModifyIndex":3}]}]



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章