-
Docker pulls consul image
docker pull consul Copy the code
-
Start The server. Before starting the server, create the /data/ Consul folder to save data on Consul
mkdir -p /data/consul Copy the code
-
Start the server using Docker Run
docker run -d -p 8500:8500 -v /data/consul:/consul/data -e CONSUL_BIND_INTERFACE='eth0' --name=consul1 consul agent -server -bootstrap -ui -client='0.0.0.0' Copy the code
agent
: Starts the Agent processserver
: Indicates that Consul is in server modeclient
: Indicates that Consul is in client modebootstrap
: indicates that the node is the Server Leaderui
: Starts the Web UI. Default port is 8500node
: Specifies the node name. The node name is unique in the clusterclient
: Binds the client interface address. 0.0.0.0 indicates that all addresses can be accessed
The IP address of the first container is 172.17.0.2. You can run the following command to query the IP address of the container:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' consul1 Copy the code
-
Insert other nodes into the cluster
docker run -d --name=consul2 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true- client = 0.0.0.0 - join 172.17.0.2; docker run-d --name=consul3 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true- client = 0.0.0.0 - join 172.17.0.2; docker run-d --name=consul4 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=false- client = 0.0.0.0 - join 172.17.0.2;Copy the code
join
: Indicates that the vm is added to the specified cluster
-
View the nodes under the cluster
docker exec -it consul1 consul members Copy the code
-
Consul has been deployed.
-
Only DC1 has been built above. Now we start to build DC2 and associate DC1 with DC2
docker run -d --name=consul5 -e CONSUL_BIND_INTERFACE='eth0' consul agent -server -bootstrap-expect 3 -datacenter=dc2 Copy the code
-
Add nodes to DC2
docker run -d --name=consul6 -e CONSUL_BIND_INTERFACE=eth0 consul agent --datacenter=dc2 --server=true- client = 0.0.0.0 - join 172.17.0.6; docker run-d --name=consul7 -e CONSUL_BIND_INTERFACE=eth0 consul agent --datacenter=dc2 --server=true- client = 0.0.0.0 - join 172.17.0.6; docker run-d --name=consul8 -e CONSUL_BIND_INTERFACE=eth0 consul agent --datacenter=dc2 --server=false- client = 0.0.0.0 - join 172.17.0.6;Copy the code
-
Associate DC1 with DC2
docker exec- It consul6 consul join -wan 172.17.0.2Copy the code
-
So far you can see dC1 and DC2 hot in the Web UI!