Docker + Consul 多数据中心模拟

1. dc1搭建

docker run -d --name node1 -h node1 progrium/consul -server -bootstrap-expect 3
JOIN_IP="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' node1)"
docker run -d --name node2 -h node2 progrium/consul -server -join $JOIN_IP
docker run -d --name node3 -h node3 progrium/consul -server -join $JOIN_IP
docker run -d -p 8400:8400 -p 8500:8500 -p 8600:53/udp --name node4 -h node4 progrium/consul -join $JOIN_IP

进入node1容器,执行命令 consul members,命令行展示结果如下:

e31c5329d0f6:/bin # ./consul members
Node Address Status Type Build Protocol DC
dc1-dc2 172.17.0.9:8301 alive server 0.7.1 2 dc1
node1 172.17.0.2:8301 alive server 0.5.2 2 dc1
node2 172.17.0.3:8301 alive server 0.5.2 2 dc1
node3 172.17.0.4:8301 alive server 0.5.2 2 dc1
node4 172.17.0.5:8301 alive client 0.5.2 2 dc1

2. dc2 搭建

采用最新consul搭建(目前最新版为0.7.1)

./consul agent -server -bootstrap-expect 3 -node=dragon-server-root -datacenter=dc2 -data-dir=/data
./consul agent -server -node=dragon-server-two -datacenter=dc2 -data-dir=/data -join=172.17.0.6
./consul agent -server -node=dragon-server-three -datacenter=dc2 -data-dir=/data -join=172.17.0.6

进入node1容器,执行命令 consul members,命令行展示结果如下:

f1aff93c9d44:/bin # ./consul members
Node Address Status Type Build Protocol DC
dragon-server-root 172.17.0.6:8301 alive server 0.7.1 2 dc2
dragon-server-three 172.17.0.8:8301 alive server 0.7.1 2 dc2
dragon-server-two 172.17.0.7:8301 alive server 0.7.1 2 dc2

3. 多数据中心关联

前两节搭建了两个独立的数据中心,两个数据中心是独立的,相互之间并没有任何联系。

选取dragon-server-three 和 node1 为临界容器,进入dragon-server-three,执行命令:./consul join -wan 172.17.0.2。完成后,执行./consul members -wan

Node                     Address          Status  Type    Build  Protocol  DC
dc1-dc2.dc1 172.17.0.9:8302 alive server 0.7.1 2 dc1
dragon-server-root.dc2 172.17.0.7:8302 alive server 0.7.1 2 dc2
dragon-server-three.dc2 172.17.0.8:8302 alive server 0.7.1 2 dc2
dragon-server-two.dc2 172.17.0.7:8302 alive server 0.7.1 2 dc2
node1.dc1 172.17.0.2:8302 alive server 0.5.2 2 dc1

至此两个数据中心已经发生联系。访问urlhttp://192.168.1.100:8500/ 可以看到两个数据中心dc1,dc2

4. 附录(shell脚本如下:)

docker run -d --name node1 -h node1 progrium/consul -server -bootstrap-expect 3

JOIN_IP="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' node1)"
echo "node1:" $JOIN_IP
docker run -d --name node2 -h node2 progrium/consul -server -join $JOIN_IP echo "node2:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' node2) docker run -d --name node3 -h node3 progrium/consul -server -join $JOIN_IP echo "node3:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' node3) docker run -d -p 8400:8400 -p 8500:8500 -p 8600:53/udp --name node4 -h node4 progrium/consul -join $JOIN_IP echo "node4:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' node4) docker run --name dc1-dc2 -d --entrypoint /bin/consul opensuse-consul agent -node=dc1-dc2 -data-dir=/data -join=$JOIN_IP echo "dc1-dc2:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' dc1-dc2) docker run --name dragon-server-root -d --entrypoint /bin/consul opensuse-consul agent -server -bootstrap-expect 3 -node=dragon-server-root -datacenter=dc2 -data-dir=/data JOIN_IP2="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' dragon-server-root)" echo "dragon-server-root:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' dragon-server-root) echo $JOIN_IP2 docker run --name dragon-server-two -d --entrypoint /bin/consul opensuse-consul agent -server -node=dragon-server-two -datacenter=dc2 -data-dir=/data -join=$JOIN_IP2 echo "dragon-server-two:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' dragon-server-two) docker run --name dragon-server-three -d --entrypoint /bin/consul opensuse-consul agent -server -node=dragon-server-three -datacenter=dc2 -data-dir=/data -join=$JOIN_IP2 echo "dragon-server-three:" $(docker inspect -f '{{.NetworkSettings.IPAddress}}' dragon-server-three) docker exec -it dragon-server-root /bin/consul join -wan $JOIN_IP
echo $?
docker exec -it dragon-server-two /bin/consul join -wan $JOIN_IP
echo $?
docker exec -it dragon-server-three /bin/consul join -wan $JOIN_IP
echo $?
05-11 16:11