[CONSUL] 使用docker建立一個Consul集群

使用docker 建立一個Consul集群

 

1. docker pull consul

2.  可以看到 consul 的image

3. 加入第一個Node, 設定為server : docker run -d --name=consul_node1 -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt":true}' consul agent -server -node=node1 -bootstrap-expect=2

skip_leave_on_interrupt This is similar to leave_on_terminate but only affects interrupt handling. When Consul receives an interrupt signal (such as hitting Control-C in a terminal), Consul will gracefully leave the cluster. Setting this to true disables that behavior. The default behavior for this feature varies based on whether or not the agent is running as a client or a server (prior to Consul 0.7 the default value was unconditionally set to false). On agents in client-mode, this defaults to false and for agents in server-mode, this defaults to true (i.e. Ctrl-C on a server will keep the server in the cluster and therefore quorum, and Ctrl-C on a client will gracefully leave).

bootstrap-expect  This flag provides the number of expected servers in the datacenter. Either this value should not be provided or the value must agree with other servers in the cluster. When provided, Consul waits until the specified number of servers are available and then bootstraps the cluster. This allows an initial leader to be elected automatically. This cannot be used in conjunction with the legacy -bootstrap flag. This flag requires -server mode.

4. 查看consul server的IPdocker inspect -f "{{.NetworkSettings.IPAdress}}" consul_node1

5. 加入第二個Node, 設定為server : docker run -d --name=consul_node2 -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt":true}' consul agent -server -node=node2 -bootstrap-expect=2 -join 172.17.0.2

6. 加入第3個Node , 設定為client, 並加入ui : docker run -d --name=consul_node3 -p 8500:8500 -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt":true}' consul agent -ui -node=node3 -client=0.0.0.0 -join 172.17.0.2

可以看到目前的node已經有3個了, consul_node1 consul_node2 都是server , consul_node3是client , 且有UI

如果在建立docker時忘了加入集群, 可以在事後加入

EX: consul_node3忘了加到集群, 可以用docker exec consul_node3 consul join 172.17.0.2

若要查看節點, 可以用 docker exec consul_node1 consul member

7. 進到UI介面看看, http://127.0.0.1:8500

[參考資料]

https://www.consul.io/docs/agent/options.html

https://blog.csdn.net/qq_28119741/article/details/96741692