docker跨主机联通
当Docker容器进程启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上。虚拟网桥的工作方式和物理交换机类似,利用overlay网络,这样主机上的所有容器就通过交换机连在了一个二层网络中。
Server1(Server) 192.168.81.58 |内核版本 3.10.0-123.el7.x86_64 |Docker版本 1.12.6
Server2(Agent) 192.168.81.59 3.10.0-123.el7.x86_64 1.12.6
目标:实现两个CentOS7容器server1,server2网络互通
上传docker_consul包consul_0.7.2_linux_amd64.zip
解压consul并移动到$PATH中
unzip consul_0.7.2_linux_amd64.zip -d /usr/local/bin
Server1:
mkdir /opt/d_c
nohup consul agent -server -bootstrap -data-dir /opt/d_c -bind=192.168.81.58 &
显示结果:nohup: ignoring input and appending output to ‘nohup.out’
Server2:
mkdir /opt/d_c
nohup consul agent -data-dir /opt/d_c -bind=192.168.81.59 &
显示结果:nohup: ignoring input and appending output to ‘nohup.out’
让agent加入到server中
consul join 192.168.81.58
显示结果:Successfully joined cluster by contacting 1 nodes.
vim /etc/sysconfig/docker-network
DOCKER_NETWORK_OPTIONS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store consul://localhost:8500 --cluster-advertise eno16777736:2375"
service docker restart
docker network ls
显示结果包含:multihost overlay global
查看Docker网络内的成员
consul members list
Server1:
docker run -d --net=multihost --name=web1 nginx
docker exec -it web1 /bin/bash
Server2:
docker run -d --net=multihost --name=web2 nginx
docker exec -it web2 /bin/bash
ping web1
ping web2
创建指定静态IP的network
docker network create -d overlay --ip-range=192.168.2.0/24 --gateway=192.168.2.1 --subnet=192.168.2.0/24 multihost2