Recently do project is about Docker network communication, need multiple host to carry on the link communication, here record, later easy project development, everybody need words also can look down, take a few detours.
Docker multi-Host network communication detailed
Docker supports multi-host network communication, and can establish a multiple host communication network through command line. This article uses the Docker machine and consul service discovery tools to explain this.
The prerequisite is that you need to install the Docker Toolbox first.
1, Docker Multi-host networking
As an example, we will create 3 Docker hosts using Docker machine on the VirtualBox virtual machine. One of the Docker hosts runs the Consul Service Discovery tool, and the other two Docker hosts share network information with the Consul Service discovery container of the first host.
The Docker container network part of the trust information can be viewed:
https://docs.docker.com/engine/userguide/networking/dockernetworks/
Consul for more information see: https://www.consul.io/
Characteristics of Consul:
1) Service discovery
Consul makes service registration and service discovery very simple
2) Fault Detection
Supports health checks on services to prevent requests from being routed to hosts on which the service is unavailable
3 Support Multiple Data centers
Consul support for multiple data centers without complex configuration
4) Key-value storage
Consul uses a key-value store to support dynamic configuration, etc.
2. Set up a multi-host network
1 Create a Docker host named "Host1-consul"
Docker-machine create-d VirtualBox Host1-consul
2 running 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 Operation status of the above container
Docker $ (docker-machine config Host1-consul) PS
4 Run the second Docker host and register to the front 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 Running a 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 a default network configuration and can only be used for communications from a single host.
6 to achieve multiple host network communications, you also need to create a cover network on host 2
Docker $ (docker-machine config host2) network create-d overlay MyApp
7 OK, now 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 among all registered hosts.
Docker $ (docker-machine config host2) network ls
Docker $ (docker-machine config host3) network ls
If you are running a container on a different host, you may need to connect them by using the container name. We can do a test like this.
Run a nginx container on host 2, run a BusyBox container on host 3, and download the Nginx container default page through the BusyBox container to test the connection.
8 Run the Nginx container on Host 2 and specify the created "MyApp" network
Docker $ (docker-machine config host2) run-itd--name=webfront--net=myapp Nginx
9) Verify the operation of the Nginx container
Docker $ (docker-machine config host2) PS
10 Run a BusyBox container on host 3 and use parameters to download the default home page for Host 2 's Nginx container.
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, it means that the container can connect to the host using the overlay network that was created earlier.
Thank you for reading, I hope to help you, thank you for your support for this site!