Recently in the study of Docker,docker network configuration is a more troublesome part, especially the inter-host communication between the container, many solutions are more complex, here, I only use virtual bridge to achieve Docker cross-host access, share out, I hope that Docker learning to have some inspiration.
Basic idea:
Because Docker containers implement IP address assignment and access to containers across the same host through the Docker0 Bridge, the simplest way to make Docker cross-host access is to set the Docker0 of different hosts to the same network segment.
So how do you implement cross-host? I will also connect the local network card through the bridge, then the overall topology structure is this:
However, with this bridging, all network cards must be in one network segment, so limit the allocation of IP to each Docker daemon
Below, we will implement this structure :
My two Ubuntu 14.04 virtual machine IPs:
host1:10.211.55.3 Network card: eth0
host2:10.211.55.5 Nic Eth1
Gateway: 10.211.55.1
Partitioning of the container IP:
Host1:10.211.55.64/26
Address range: 10.211.55.65~10.211.55.126
Host2:10.211.55.128/26
Address range: 10.211.55.129~10.211.55.190
Required actions:
The following, take Host1 as an example, Host2 on similar operation, but the network card name is not the same, I am here, not using the default Docker0 bridge, but a new virtual bridge
1. Establish the virtual bridge on the Docker host, respectively:
Host1: $ sudo brctl addbr br0
2. Assigning a same network segment IP to the bridge
Host1: $ sudo ifconfig br0 10.211.55.10 netmask 255.255.255.0
Host2: $ sudo ifconfig br0 10.211.55.20 netmask 255.255.255.0
3. Bridge the local network card:
Host1: $ sudo brctl addif br0 eth0
Here, we're ready. Bridge settings
Let's modify the Docker configuration and use our new bridge instead of Docker0:
1. Modify the/etc/default/docker file
$sudo Vim/etc/default/docker
2. Add the daemon's startup options:
Host1: docker_opts= "-b=br0--fixed-cidr= ' 10.211.55.64/26 '"
Host2: docker_opts= "-b=br1--fixed-cidr= ' 10.211.55.128/26 '"
Here,-B is used to specify the name of the bridge to which the container is connected
--fixed-cidr used to limit the range of IP addresses assigned to a container
3. Save the file and restart the Docker service
$ sudo service docker restart
Below, it is possible to verify:
1. Start a container on two hosts, respectively
$ docker run-it Ubuntu/bin/bash
2. Run the ping command in the container to view the connection status
Using virtual Bridge to implement cross-host access for Docker containers