TaoTaoFu 2019-06-27
主要参考每天5分钟玩转docker容器技术博客。 记录下自己的实验过程。 如果想学习,请直接参考该博客http://www.cnblogs.com/CloudMan6/
跨主机网络方案:
方案通过libnetwork及cnm与docker集成在一起。
本文主要来实现overlay跨主机网络。
host1: 192.168.19.120
host2: 192.168.19.121
host3: 192.168.19.122
在host1上部署支持的组件,使用Consul。
在host2和host3上实践跨主机网络。
我们以容器方式运行Consul:
root@host1:~# docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap root@host1:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f9429634f2d3 progrium/consul "/bin/start -server ?? 28 seconds ago Up 27 seconds 53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp consul
在virtualbox上将8500端口转发至20003端口
浏览器打开127.0.0.1:20003
virtualbox我不是很熟,选择nat网络后使用的端口转发将8500转发至20003,可能有其他做法,暂时未深究
修改host2和host3的docker daemon的配置文件/etc/systemd/system/docker.service.d/10-machine.conf
在ExecStart选项中加上
--cluster-store=consul://192.168.19.120:8500 --cluster-advertise=enp0s3:2376
--cluster-store 指定 consul 的地址。
--cluster-advertise 告知 consul 自己的连接地址。
之后重启docker
systemctl daemon-reload systemctl restart docker.service
之后查看consul控制台,发现host2与host3自动注册到了Consul数据库中。
root@host2:~# docker network ls NETWORK ID NAME DRIVER SCOPE 4504cf2b3c21 bridge bridge local edf7614b9aef host host local 7441ee592779 none null local root@host2:~# docker network create -d overlay ov_net1 472b8d3a3b9353328ad08a5bbf456599ffc3f43892806810bbfd2fd6a49b5159 root@host2:~# docker network ls NETWORK ID NAME DRIVER SCOPE 4504cf2b3c21 bridge bridge local edf7614b9aef host host local 7441ee592779 none null local 472b8d3a3b93 ov_net1 overlay global
-d overlay: 指定driver为overlay
可以注意到ov_net1的scope为global
root@host3:~# docker network ls NETWORK ID NAME DRIVER SCOPE 3f19c8a4ae83 bridge bridge local c0cc6418f84f host host local 55a505077100 none null local 472b8d3a3b93 ov_net1 overlay global
在host3中也看到了ov_net1网络
是由于host2将信息存入了consul中,host3从consul中获取到了该网络。
使用docker network inspect命令
root@host3:~# docker network inspect ov_net1 [ { "Name": "ov_net1", "Id": "472b8d3a3b9353328ad08a5bbf456599ffc3f43892806810bbfd2fd6a49b5159", "Created": "2018-05-12T23:46:20.332476055+08:00", "Scope": "global", "Driver": "overlay", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "10.0.0.0/24", "Gateway": "10.0.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]
在host2上创建容器
root@host2:~# docker run -itd --name bbox1 --network ov_net1 busybox afc025f1cab1d0bf9ac674e42c8b02a6066ccae23fa904d4b984827483b0de4b root@host2:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES afc025f1cab1 busybox "sh" 4 seconds ago Up 2 seconds bbox1
也在host3上创建容器
root@host3:~# docker run -itd --name bbox2 --network ov_net1 busybox 63a900ce909bfd53127693efa00739b77f437b5cf91b87c7d55279222ee782b5 root@host3:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63a900ce909b busybox "sh" About a minute ago Up About a minute
发现两台机器上的这两个容器可以互相ping通,且可以直接通过主机名访问通。也可以访问互联网。
root@host2:~# docker exec -it bbox1 ping -c 2 bbox2 PING bbox2 (10.0.0.3): 56 data bytes 64 bytes from 10.0.0.3: seq=0 ttl=64 time=3.092 ms 64 bytes from 10.0.0.3: seq=1 ttl=64 time=1.215 ms --- bbox2 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 1.215/2.153/3.092 ms root@host2:~# docker exec -it bbox1 ping -c 4 www.baidu.com PING www.baidu.com (115.239.211.112): 56 data bytes 64 bytes from 115.239.211.112: seq=0 ttl=53 time=12.352 ms 64 bytes from 115.239.211.112: seq=1 ttl=53 time=15.298 ms 64 bytes from 115.239.211.112: seq=2 ttl=53 time=14.399 ms 64 bytes from 115.239.211.112: seq=3 ttl=53 time=20.381 ms --- www.baidu.com ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max = 12.352/15.607/20.381 ms
查看当前的docker网络
root@host2:~# docker network ls NETWORK ID NAME DRIVER SCOPE 33088197519e bridge bridge local 4fd73b05e4d2 docker_gwbridge bridge local edf7614b9aef host host local 7441ee592779 none null local 472b8d3a3b93 ov_net1 overlay global