Docker multi host network communication details

  • 2020-05-17 07:13:09
  • OfStack

The recent project is about the network communication of Docker, which requires multiple hosts to link communication. It is recorded here to facilitate the project development in the future. If you need it, you can also look at it and take fewer detours.

Docker multi-host network communication details

Docker supports multi-host network communication, which can be set up via the command line. This article USES the Docker machine and Consul service discovery tools to illustrate this point.

The premise is that you need to install the Docker toolkit first.

1. Docker Multi-Host Networking

As an example, we will create three Docker hosts on the VirtualBox virtual machine using docker machine. One of the Docker hosts runs the Consul service discovery tool, while the other two Docker hosts share network information through the Consul service discovery container on the first host.

Trust information for the Docker container network section is available at:
Consul details see: https: / / www consul. io /

Consul features:

1) service discovery

Consul makes service registration and service discovery easy

2) fault detection

Supports health checks on services to prevent requests from being routed to hosts where the service is not available

3) support multiple data centers

Consul supports multiple data centers without complex configuration

4) key and value storage

Consul USES a key-value store to support dynamic configuration and so on

2. Set up multi-host network

1) create the Docker host named "host1-Consul"

docker-machine create -d virtualbox host1-Consul

2) run the Consul container on the "host1-Consul" host

docker $(docker-machine config host1-Consul) run -d -p "8500:8500" -h"Consul" progrium/Consul -server -bootstrap

3) verify the running state of the above container

docker $(docker-machine config host1-Consul) ps

4) run the second Docker host and register to the previous Consul container

docker-machine create -d virtualbox --engine-opt="cluster-store=Consul://$(docker-machine ip host1-Consul):8500" --engine-opt="cluster-advertise=eth1:0" host2

5) run the third Docker host

docker-machine create -d virtualbox --engine-opt="cluster-store=Consul://$(docker-machine ip host1-Consul):8500" --engine-opt="cluster-advertise=eth1:0" host3

Now, the next two Docker hosts have default network configurations and can only be used for single-host communication.

6) to realize multi-host network communication, it is also necessary to create an overlay network on host 2

docker $(docker-machine config host2) network create -d overlay myapp

7) OK. At this point, if you check the network on host 3, you can see the overlay network created on host 2. This is because hosts 2 and 3 are registered to Consul and network information is Shared between all registered hosts.

docker $(docker-machine config host2) network ls
docker $(docker-machine config host3) network ls

If you are running containers on different hosts, you may need to use the container name to connect to them. We could do a test like this.
Run 1 Nginx container on host 2, 1 busybox container on host 3, and test the connection by downloading the Nginx container default page from the busybox container.

8) run the Nginx container on host 2 and specify the "myapp" network to be created

docker $(docker-machine config host2) run -itd --name=webfront --net=myapp nginx

9) verify the operation of Nginx container

docker $(docker-machine config host2) ps

10) run an busybox container on host 3 and download the default home page of the Nginx container on host 2 using the parameters.

docker $(docker-machine config host3) run -it --rm --net=myapp busybox wget -qO- http://webfront

If the result returns the output of the HTML content, that means the container can connect to the host using the overlaid network created earlier.

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: