1. Docker pulls consul image

    docker pull consul
    Copy the code
  2. Start The server. Before starting the server, create the /data/ Consul folder to save data on Consul

    mkdir -p /data/consul
    Copy the code
  3. 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 process
    • server: Indicates that Consul is in server mode
    • client: Indicates that Consul is in client mode
    • bootstrap: indicates that the node is the Server Leader
    • ui: Starts the Web UI. Default port is 8500
    • node: Specifies the node name. The node name is unique in the cluster
    • client: 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
  4. 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
  5. View the nodes under the cluster

    docker exec -it consul1 consul members
    Copy the code
  6. Consul has been deployed.

  7. 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
  8. 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
  9. Associate DC1 with DC2

    docker exec- It consul6 consul join -wan 172.17.0.2Copy the code
  10. So far you can see dC1 and DC2 hot in the Web UI!