Overlay网络是叠加网络,叠加的意思是在二层网络中,可以使用三层网络进行通信。docker默认是二层网络,所以在主机上启动的container利用Docker Networking可以ping通对方,但是不同主机上的container是没有办法ping通对方的。
flannel是叠加网络,docker可以使用flannel来实现这个目标
首先
先准备三台vm,分别命名为branmaster, brannode1, brannode2
我的vm是centos7的,第一步是disable掉三台vm的防火墙
systemctl stop firewalld.service; systemctl disable firewalld.service
第二步;在三台VM上安装软件
yum -y install docker flannel etcd python
安装完软件后enable docker
systemctl enabledocker.service;
systemctl start docker.service
第三步(branmaster主机上):修改branmaster主机上的etcd.conf文件
ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
ETCD_ADVERTISE_CLIENT_URLS=http://0.0.0.0:2379
重启etcd
systemctl enableetcd; systemctl start etcd; systemctl status etcd
新建flanneld-config.json文件
{
"Network":"172.15.0.0/16",
"SubnetLen": 24,
"Backend": {
"Type": "vxlan"
}
}
将文件内容写入etcd
curl -Lhttp://localhost:2379/v2/keys/branmaster/network/config -XPUT --data-urlencodevalue@flanneld-config.json | python -m json.tool
查看是否写入
curl -L http://localhost:2379/v2/keys/branmaster/network/config | python -m json.tool
编辑flanneld配置文件
/etc/sysconfig/flannel
FLANNEL_ETCD="http://branmaster:2379"
FLANNEL_ETCD_KEY="/branmaster/network"
重启flanneld:
systemctl enable flanneld;
systemctl start flanneld;
systemctl status flannel
ip addr可以看到多了flanneld.1网络接口
自动生成了subnet.env文件
cat/run/flannel/subnet.env
第四步(在node节点上):
编辑flannel配置文件
vim /etc/sysconfig/flanneld
FLANNEL_ETCD="http://branmaster:2379"
FLANNEL_ETCD_KEY="/branmaster/network"
重启
systemctl enable flanneld;
systemctl start flanneld;
systemctl status flanneld
修改docker配置文件
vim/usr/lib/systemd/system/docker.service
把之前的网络配置删掉
改成flannel的
EnvironmentFile=-/run/flannel/subnet.env
--bip=${FLANNEL_SUBNET}
--mtu=${FLANNEL_MTU}
重启docker
systemctldaemon-reload;
systemctl restart docker;
systemctl enable docker;
systemctlstatus docker
在另外一条node上重复上述步骤
结果展示:
brannode1上的container的IP:172.15.86.2
brannode2上的container的IP:172.15.12.2
可以ping同brannode1主机上的container的IP:172.15.86.2