NO IMAGE

Consul是一個支援多資料中心分散式高可用的服務發現配置共享的服務軟體,由HashiCorp公司用Go語言開發,基於Mozilla Public License 2.0的協議進行開源。Consul支援健康檢查,並允許HTTP和DNS協議呼叫 API
儲存鍵值對

 

1   如何獲取

目前最新版本是V0.6.4。

原始碼地址:

https://github.com/hashicorp/consul

可執行檔案地址:

https://www.consul.io/downloads.html

MailList:

https://groups.google.com/group/consul-tool/

官方地址:

https://www.consul.io/

官方演示:

http://demo.consul.io/ui/

 

2   搭建CONSUL環境

2.1 網路規劃

主機名稱

IP

角色

資料中心

node0

192.168.192.120

Server

DataCenter1

node1

192.168.192.121

Server

DataCenter1

node2

192.168.192.122

Server

DataCenter1

node3

192.168.192.123

Client

DataCenter1

 

2.2 軟體環境

CentOS:7.2.1511

Consul:V0.6.4

 

2.3 構建

1. 安裝可執行檔案consul

   從上述所屬地址中下載(linux類別X64),然後執行

[[email protected] cousul]$sudo consul /usr/bin/

[[email protected] ~]# chmod755 consul

檢視是否安裝成功

[[email protected] cousul]$consul version

Consul v0.6.4

Consul Protocol: 3 (Understands back to: 1)

[[email protected] cousul]$

 

檢視幫助

[[email protected] consul]$consul –help

usage: consul[–version] [–help] <command> [<args>]

 

Available commandsare:

    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 foroperators

    join           Tell Consul agent to join cluster

    keygen         Generates a new encryption key

    keyring        Manages gossip layer encryption keys

    leave          Gracefully leaves the Consul clusterand shuts down

    lock           Execute a command holding a lock

    maint          Controls node or service maintenancemode

    members        Lists the members of a Consul cluster

    monitor        Stream logs from a Consul agent

    reload         Triggers the agent to reloadconfiguration files

    rtt            Estimates network round trip timebetween nodes

    version        Prints the Consul version

    watch          Watch for changes in Consul

 

Consul有兩種方式搭建方式:一是bootstrap模式,二是非bootstrap模式。

 

2.3.1  bootstrap模式

 

1. 在啟動agent

在第一臺節點上啟動agent,以server模式執行,指定server角色的節點數目,指定節點名(在datacenter內節點名唯一),同時提供監聽地址。

命令如下:

[[email protected] cousul]$ consul agent -server-bootstrap-expect=3 -data-dir=/tmp/consul -node=node0 -bind=192.168.192.120-dc=dc1

==> WARNING: Expect Mode enabled,expecting 3 servers

==> Starting Consul agent…

==> Starting Consul agent RPC…

==> Consul agent running!

         Node name: ‘node0’

        Datacenter: ‘dc1’

            Server: true (bootstrap: false)

       Client Addr: 127.0.0.1 (HTTP: 8500,HTTPS: -1, DNS: 8600, RPC: 8400)

     Cluster Addr: 192.168.192.120 (LAN: 8301, WAN: 8302)

   Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

             Atlas: <disabled>

 

==> Log data will now stream inas it occurs:

 

   2016/07/05 20:52:06 [INFO] serf: EventMemberJoin: node0 192.168.192.120

   2016/07/05 20:52:06 [INFO] serf: EventMemberJoin: node0.dc1192.168.192.120

   2016/07/05 20:52:06 [INFO] raft: Node at 192.168.192.120:8300 [Follower]entering Follower state

   2016/07/05 20:52:06 [INFO] consul: adding LAN server node0 (Addr:192.168.192.120:8300) (DC: dc1)

   2016/07/05 20:52:06 [INFO] consul: adding WAN server node0.dc1 (Addr:192.168.192.120:8300) (DC: dc1)

   2016/07/05 20:52:06 [ERR] agent: failed to sync remote state: No clusterleader

   2016/07/05 20:52:08 [WARN] raft: EnableSingleNode disabled, and no knownpeers. Aborting election.

之所以失敗,原因在於當前的Datacenter沒有 leader server

依次在另外兩臺機器部署agent作為server

節點node1

[[email protected] consul]$ consul agent-server -bootstrap-expect=3 -data-dir=/tmp/consul -node=node1-bind=192.168.192.121 -dc=dc1

節點node2

[[email protected] consul]$ consul agent-server -bootstrap-expect=3 -data-dir=/tmp/consul -node=node2-bind=192.168.192.122 -dc=dc1

 

目前,三個節點均不知道其他Server節點的存在,以node0為例

[[email protected] consul]$ consul members

Node   Address               Status  Type   Build  Protocol  DC

node0  192.168.192.120:8301  alive  server  0.6.4  2        dc1

[[email protected] consul]$

檢視consul叢集資訊

[[email protected] consul]$ consul info

agent:

    check_monitors= 0

    check_ttls= 0

    checks= 0

    services= 1

build:

    prerelease=

    revision= 26a0ef8c

    version= 0.6.4

consul:

    bootstrap = false

    known_datacenters= 1

    leader = false

    server = true

……

當前節點為follow節點。

2. 觸發選舉leader

   因為consul一般需要3~5個Server,因此,在節點node0上新增node1和node2。

[[email protected] consul]$ consul join192.168.192.121

Successfully joined cluster bycontacting 1 nodes.

[[email protected] consul]$ consul join192.168.192.122

Successfully joined cluster bycontacting 1 nodes.

[[email protected] consul]$  

觀察三個節點consul日誌:

Node0:

   2016/07/05 21:10:55 [INFO] agent: (LAN) joining: [192.168.192.122]

   2016/07/05 21:10:55 [INFO] serf: EventMemberJoin: node2 192.168.192.122

   2016/07/05 21:10:55 [INFO] agent: (LAN) joined: 1 Err: <nil>

   2016/07/05 21:10:55 [INFO] consul: adding LAN server node2 (Addr:192.168.192.122:8300) (DC: dc1)

   2016/07/05 21:10:55 [INFO] consul: Attempting bootstrap with nodes:[192.168.192.120:8300 192.168.192.121:8300 192.168.192.122:8300]

   2016/07/05 21:10:55 [INFO] consul: New leader elected: node2

   2016/07/05 21:10:56 [INFO] agent: Synced service ‘consul’

Node1

  2016/07/05 21:10:55 [INFO] serf:EventMemberJoin: node2 192.168.192.122

   2016/07/05 21:10:55 [INFO] consul: adding LAN server node2 (Addr:192.168.192.122:8300) (DC: dc1)

   2016/07/05 21:10:55 [INFO] consul: Attempting bootstrap with nodes:[192.168.192.121:8300 192.168.192.120:8300 192.168.192.122:8300]

   2016/07/05 21:10:56 [INFO] consul: New leader elected: node2

2016/07/05 21:10:57 [INFO] agent:Synced service ‘consul’

Node2

   2016/07/05 21:10:55 [INFO] serf: EventMemberJoin: node0 192.168.192.120

   2016/07/05 21:10:55 [INFO] serf: EventMemberJoin: node1 192.168.192.121

   2016/07/05 21:10:55 [INFO] consul: adding LAN server node0 (Addr:192.168.192.120:8300) (DC: dc1)

   2016/07/05 21:10:55 [INFO]consul: Attempting bootstrap with nodes: [192.168.192.122:8300192.168.192.120:8300 192.168.192.121:8300]

   2016/07/05 21:10:55 [INFO] consul: adding LAN server node1 (Addr:192.168.192.121:8300) (DC: dc1)

   2016/07/05 21:10:55 [WARN] raft: Heartbeat timeout reached, startingelection

   2016/07/05 21:10:55 [INFO] raft: Node at 192.168.192.122:8300[Candidate] entering Candidate state

   2016/07/05 21:10:55 [INFO] raft: Election won. Tally: 2

   2016/07/05 21:10:55 [INFO] raft: Node at 192.168.192.122:8300 [Leader]entering Leader state

   2016/07/05 21:10:55 [INFO] consul: cluster leadership acquired

   2016/07/05 21:10:55 [INFO] consul: New leader elected: node2

   2016/07/05 21:10:55 [INFO] raft: pipelining replication to peer192.168.192.121:8300

   2016/07/05 21:10:55 [INFO] raft: pipelining replication to peer192.168.192.120:8300

   2016/07/05 21:10:55 [INFO] consul: member ‘node2’ joined, marking healthalive

   2016/07/05 21:10:55 [INFO] consul: member ‘node0’ joined, marking healthalive

   2016/07/05 21:10:55 [INFO] consul: member ‘node1’ joined, marking healthalive

   2016/07/05 21:10:58 [INFO] agent: Synced service ‘consul’

由日誌可知,舉出了leadernode2

在node0檢視members

[[email protected] consul]$ consul members

Node   Address               Status  Type   Build  Protocol  DC

node0  192.168.192.120:8301  alive  server  0.6.4  2        dc1

node1  192.168.192.121:8301  alive  server  0.6.4  2        dc1

node2  192.168.192.122:8301  alive  server  0.6.4  2         dc1

[[email protected] consul]$

檢視info資訊

[[email protected] consul]$ consul info

agent:

    check_monitors= 0

    check_ttls= 0

    checks= 0

    services= 1

build:

    prerelease=

    revision= 26a0ef8c

    version= 0.6.4

consul:

    bootstrap = false

    known_datacenters= 1

    leader = false

    server = true

……

節點node2上檢視consul資訊

[[email protected] consul]$ consul info

agent:

    check_monitors= 0

    check_ttls= 0

    checks= 0

    services= 1

build:

    prerelease=

    revision= 26a0ef8c

    version= 0.6.4

consul:

    bootstrap= false

    known_datacenters= 1

    leader = true

    server = true

 

3. 在node3上以client啟動agent

[[email protected] consul]$ consul agent-data-dir=/tmp/consul -node=node3 -bind=192.168.192.123 -dc=dc1

==> Starting Consul agent…

==> Starting Consul agent RPC…

==> Consul agent running!

         Node name: ‘node3’

        Datacenter: ‘dc1’

            Server: false (bootstrap: false)

       Client Addr: 127.0.0.1 (HTTP: 8500,HTTPS: -1, DNS: 8600, RPC: 8400)

     Cluster Addr: 192.168.192.123 (LAN: 8301, WAN: 8302)

   Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

             Atlas: <disabled>

 

==> Log data will now stream inas it occurs:

 

   2016/07/05 21:21:02 [INFO] serf: EventMemberJoin: node3 192.168.192.123

   2016/07/05 21:21:02 [ERR] agent: failed to sync remote state: No knownConsul servers

 

在節點node0上新增node3

[[email protected] consul]$ consul join192.168.192.123

Successfully joined cluster bycontacting 1 nodes.

[[email protected] consul]$ consul members

Node   Address               Status  Type   Build  Protocol  DC

node0  192.168.192.120:8301  alive   server  0.6.4 2         dc1

node1  192.168.192.121:8301  alive  server 
0.6.4  2        dc1

node2  192.168.192.122:8301  alive   server
 0.6.4 2         dc1

node3  192.168.192.123:8301  alive  client
 0.6.4 2         dc1

[[email protected] consul]$

節點node3的日誌如下:

   2016/07/05 21:21:57 [INFO] serf: EventMemberJoin: node0 192.168.192.120

   2016/07/05 21:21:57 [INFO] serf: EventMemberJoin: node2 192.168.192.122

   2016/07/05 21:21:57 [INFO] serf: EventMemberJoin: node1 192.168.192.121

   2016/07/05 21:21:57 [INFO] consul: adding server node0 (Addr:192.168.192.120:8300) (DC: dc1)

   2016/07/05 21:21:57 [INFO] consul: adding server node2 (Addr:192.168.192.122:8300) (DC: dc1)

   2016/07/05 21:21:57 [INFO] consul: adding server node1 (Addr:192.168.192.121:8300) (DC: dc1)

   2016/07/05 21:21:57 [INFO] consul: New leader elected: node2

   2016/07/05 21:21:57 [INFO] agent: Synced node info

 

3. 依次關閉node3 node2:

Node0和node1的日誌如下:

Node0

   2016/07/05 21:24:00 [INFO] serf: EventMemberLeave: node2 192.168.192.122

   2016/07/05 21:24:00 [INFO] consul: removing LAN server node2 (Addr:192.168.192.122:8300) (DC: dc1)

   2016/07/05 21:24:00 [WARN] raft: Heartbeat timeout reached, startingelection

   2016/07/05 21:24:00 [INFO] raft: Node at 192.168.192.120:8300[Candidate] entering Candidate state

   2016/07/05 21:24:01 [INFO] raft: Duplicate RequestVote for same term: 2

   2016/07/05 21:24:02 [WARN] raft: Election timeout reached, restartingelection

   2016/07/05 21:24:02 [INFO] raft: Node at 192.168.192.120:8300[Candidate] entering Candidate state

   2016/07/05 21:24:02 [INFO] raft: Election won. Tally: 2

   2016/07/05 21:24:02 [INFO] raft: Node at 192.168.192.120:8300 [Leader]entering Leader state

   2016/07/05 21:24:02 [INFO] consul: cluster leadership acquired

   2016/07/05 21:24:02 [INFO] consul: New leader elected: node0

   2016/07/05 21:24:02 [INFO] raft: pipelining replication to peer192.168.192.121:8300

   2016/07/05 21:24:02 [INFO] consul: member ‘node2’ left, deregistering

   2016/07/05 21:24:03 [INFO] agent.rpc: Accepted client: 127.0.0.1:35701

Node1

   2016/07/05 21:24:00 [INFO] consul: removing LAN server node2 (Addr:192.168.192.122:8300) (DC: dc1)

   2016/07/05 21:24:00 [WARN] raft: Rejecting vote request from192.168.192.120:8300 since we have a leader: 192.168.192.122:8300

   2016/07/05 21:24:01 [WARN] raft: Heartbeat timeout reached, startingelection

   2016/07/05 21:24:01 [INFO] raft: Node at 192.168.192.121:8300[Candidate] entering Candidate state

   2016/07/05 21:24:02 [INFO] raft: Node at 192.168.192.121:8300 [Follower]entering Follower state

   2016/07/05 21:24:02 [INFO]consul: New leader elected: node0

 

3   參考文獻

[01]http://www.cnblogs.com/yatingyang/articles/4495098.html