Environmental restrictions
- A key-value storage service, such as consul, must be installed
- The host has installed docker engine
- The hostname of the host must be different
- Kernel greater than 3.16
Environment preparation and role assignment
- Two ubuntu servers
server1 | 192.168.1.75 | 4.2.0-27-generic | server1(centOS7) | 1.10.2 | server |
server2 | 192.168.1.76 | 4.2.0-27-generic | server2(centOS7) | 1.10.2 | client |
- Experimental goal: two CentOS7 containers server1, server2 network interoperability, pay attention to hanxt@server1 and hanxt@server2 in this article
Download the distributed discovery service coordination software: consul
- Download the consul software and unzip it, it is actually a binary file and put it under $PATH
hanxt@server1:~$ wget https://releases.hashicorp.com/consul/0.6.3/consul_0.6.3_linux_amd64.zip
hanxt@server1:~$ unzip consul_0.6.3_linux_amd64.zip;
hanxt@server1:~$ sudo mv consul /bin
- start consul
hanxt@server1:~$ nohup sudo consul agent -server -bootstrap -data-dir /home/hanxt/workspace/data/consul -bind=192.168.1.75 &
hanxt@server2:~$ nohup sudo consul agent -data-dir /home/hanxt/workspace/data/consul -bind=192.168.1.76 &
hanxt@server2:~$ consul join 192.168.1.75
You can start a server and multiple agents (here is one), and then let the agents join the consul cluster
Configure Docker and restart
- Do the following configuration on each docker host and restart docker
sudo vi /etc/default/docker
DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store consul://localhost:8500 --cluster-advertise eth0:2375"
sudo service docker restart
- Cluster configuration
The --cluster-store= parameter points to the address of the key value service used by the docker daemon (in this case, the service address of consul) --cluster-advertise= The parameter determines the network card used and the port information of the docker daemon
- host configuration
The above -H parameters specify the address and protocol of the docker demon service respectively
Create overlay network
- create
hanxt@server1:~$ sudo docker network create -d overlay multihost
- verify
The multihost network created on server1 will be synchronized to server2 through the consul service
hanxt@server2:~$ docker network ls
NETWORK ID NAME DRIVER
4956314037af multihost overlay
e17295058b38 bridge bridge
e4ef9ba16838 none null
f3312e582310 host host
Create a container
docker run -d --net=multihost --name=host1 hanxt/centos:7
docker run -d --net=multihost --name=host2 hanxt/centos:7
Verify network connectivity
hanxt@server2:~$ docker exec -it host2 bash
[root@2516560c337f /]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 10.0.0.3 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::42:aff:fe00:3 prefixlen 64 scopeid 0x20<link>
ether 02:42:0a:00:00:03 txqueuelen 0 (Ethernet)
RX packets 29 bytes 2522 (2.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22 bytes 1964 (1.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@2516560c337f /]#
[root@2516560c337f /]# ping 192.168.1.75
PING 192.168.1.75 (192.168.1.75) 56(84) bytes of data.
64 bytes from 192.168.1.75: icmp_seq=1 ttl=63 time=0.790 ms
64 bytes from 192.168.1.75: icmp_seq=2 ttl=63 time=0.825 ms
64 bytes from 192.168.1.75: icmp_seq=3 ttl=63 time=0.907 ms
^C
--- 192.168.1.75 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.790/0.840/0.907/0.059 ms
[root@2516560c337f /]#
[root@2516560c337f /]# ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=1.13 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=1.03 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=1.06 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=1.09 ms
^C
--- 10.0.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 1.031/1.080/1.130/0.043 ms
[root@2516560c337f /]#
- Verification conclusion, the ip=10.0.0.3 of the server2 container host2 can ping server1, and the ip=10.0.0.2 of the container host1 on server1 can be pinged
如何使用静态ip
- 以上的实验步骤。container的ip都是自动分配的,如果需要静态的固定ip,怎么办?
- 在创建网络的过程中有区别
sudo docker network create -d overlay --ip-range=192.168.2.0/24 --gateway=192.168.2.1 --subnet=192.168.2.0/24 multihost
docker run -d --name host1 --net=multihost --ip=192.168.2.2 hanxt/centos:7
docker run-d --name host2 --net=multihost --ip=192.168.2.3 hanxt/centos:7
http://www.tuicool.com/articles/AVZB32v