overlay的优点:
虚拟出多个网卡给Docker容器使用,和局域网其它服务器处于同一个网段,并且能防止ip地址冲突
自动管理ip地址,所有容器之间内部dns互通
系统环境
1.安装Consul
#上传consul镜像包
#导入consul镜像包
[root@docker01 opt]# docker load -i docker_progrium_consul.tar.gz
#启动consul容器
[root@docker01 opt]# docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap
52becd6440b08f309c53bbc86e5f7350f2527b903a689f6b2bc2009ebaa1da38
[root@docker01 opt]#
2.docker01、02修改配置文件
#docker01配置 vim /etc/docker/daemon.json { "cluster-store": "consul://10.0.0.13:8500", "cluster-advertise": "10.0.0.11:2376" } #docker02配置 vim /etc/docker/daemon.json { "cluster-store": "consul://10.0.0.13:8500", "cluster-advertise": "10.0.0.12:2376" } #重启docker服务 systemctl restart docker
3.查看consul服务器端是否有两个节点信息
4.创建overlay网络
#创建overlay网络模式
[root@docker01 ~]# docker network create -d overlay --subnet 172.16.2.0/24 --gateway 172.16.2.254 ol1
[root@docker01 ~]# docker network create -d overlay --subnet 172.16.2.0/24 --gateway 172.16.2.254 ol1
ecbd715f153c59bd42a23769e9f3ddf2b0a21435ba74d70e3874351a08543597
#ol1网络模式为golbal全局模式(互相同步信息)
[root@docker01 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
3fba06b11250 bridge bridge local
4432d41fcf56 host host local
c8dad314488c macvlan_1 macvlan local
a2e63a998a4a none null local
ecbd715f153c ol1 overlay global
2b0f87210447 zabbix_default bridge local
#可以看到dockr02会自动同步网络模式
[root@docker02 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
7f4146a21aa0 bridge bridge local
a5f0d48f3378 harbor_harbor bridge local
1bfdb4760e94 host host local
98e6c0498a83 macvlan_1 macvlan local
369a03a283d0 none null local
ecbd715f153c ol1 overlay global
5.启动容器测试
#docker01容器测试docker02 [root@docker01 ~]# docker run -it --network ol1 --name test01 alpine:3.9 /bin/sh / # ping test02 PING test02 (172.16.2.2): 56 data bytes 64 bytes from 172.16.2.2: seq=0 ttl=64 time=0.556 ms 64 bytes from 172.16.2.2: seq=1 ttl=64 time=0.575 ms 64 bytes from 172.16.2.2: seq=2 ttl=64 time=0.542 ms ^C --- test02 ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.542/0.557/0.575 ms / # #docker02容器测试docker01 [root@docker02 ~]# docker run -it --network ol1 --name test02 alpine:3.9 /bin/sh / # ping test01 PING test01 (172.16.2.1): 56 data bytes 64 bytes from 172.16.2.1: seq=0 ttl=64 time=0.825 ms 64 bytes from 172.16.2.1: seq=1 ttl=64 time=0.509 ms 64 bytes from 172.16.2.1: seq=2 ttl=64 time=0.399 ms ^C --- test01 ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.399/0.577/0.825 ms / # PS:由此可见两个容器双向解析可以互相访问
每个容器有两块网卡,eth0实现容器间的通讯,eth1实现容器访问外网
赞赏微信赞赏
支付宝赞赏