部署Flannel,實現跨主機Docker容器通信
flannel(flannel is a virtual network that attaches IP addresses to containers)
兩個主機下的Docker容器之間是不互通的,通過部署flannel,對docker容器的ip進行規劃,就能實現跨主機容器之間的通信。
官方文檔:https://coreos.com/flannel/docs/latest/flannel-config.html
以下介紹Flannel的部署
機器配置如下
hostname | ip | 系統版本 |
---|---|---|
etcd1 | 172.17.0.2 | CentOS Linux release 7.4.1708 (Core) |
etcd2 | 172.17.0.3 | CentOS Linux release 7.4.1708 (Core) |
一、搭建etcd集群
參見之前的文章:http://www.cnblogs.com/cs-zh/p/7878019.html
二、添加網絡配置到etcd
etcdctl --endpoint="172.17.0.2:2379" set /coreos.com/network/config ‘{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}‘
這裏配置flannel網絡ip段為172.17.0.0
flannel默認加載etcd配置前綴為“/coreos.com/network”,可修改為其他的key
三、下載二進制包
wget https://github.com/coreos/flannel/releases/download/v0.9.1/flannel-v0.9.1-linux-amd64.tar.gz
tar -xf flannel-v0.9.1-linux-amd64.tar.gz
四、啟動flannel
nohup ./flanneld >> flannel.log 2>&1 &
註意:1.如果“第二步”中修改etcdctl set的key值,這裏需要加參數-etcd-prefix=".."
2.如果etcd不在部署flannel的節點,需要加參數-etcd-endpoints
具體./flanneld -h看參數說明
運行成功後,會生成文件/run/flannel/subnet.env,之後需要重啟dockerd,要帶上這幾個信息
FLANNEL_NETWORK=172.17.0.0/16
FLANNEL_SUBNET=172.17.80.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=false
五、重啟dockerd
1.修改docker.service
vim /usr/lib/systemd/system/docker.service
增加EnvironmentFile,並在ExecStart後面加上"$DOCKER_OPTS"
EnvironmentFile=/etc/default/docker
ExecStart=/usr/bin/dockerd $DOCKER_OPTS
2.重新加載配置
systemctl daemon-reload
3.重啟dockerd
systemctl restart docker
4.確認參數正確執行
ps -ef|grep docker
查看進行信息,--bip參數已經被正確帶上
root 4713 1 0 07:48 ? 00:00:00 /usr/bin/dockerd --bip=172.17.80.1/24 --ip-masq=true --mtu=1450
root 4721 4713 0 07:48 ? 00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
六、確認配置是否正確
1.查看網卡信息
ifconfig
此時多出個“flannel.1”,並且docker0網卡在該子網下
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.80.1 netmask 255.255.255.0 broadcast 0.0.0.0
ether 02:42:2d:68:34:d8 txqueuelen 0 (Ethernet)
RX packets 568 bytes 45301 (44.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 565 bytes 66158 (64.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 983380 bytes 612479051 (584.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 964071 bytes 89535273 (85.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.80.0 netmask 255.255.255.255 broadcast 0.0.0.0
ether 2e:3c:6e:38:36:ce txqueuelen 0 (Ethernet)
RX packets 19 bytes 1464 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 1593 (1.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.查看路由配置
route -n
確認有flannel.1的路由表
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.17.0.1 0.0.0.0 UG 0 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.17.80.0 0.0.0.0 255.255.255.0 U 0 0 0 docker0
172.17.87.0 172.17.87.0 255.255.255.0 UG 0 0 0 flannel.1
假如這裏沒有flannel.1的路由表,則跨主機容器之間還是ping不同,因為數據包沒有被正確的轉發。看看是不是flannel的版本過低,這個問題在0.8.0出現,換成最新0.9.1就沒問題。
七.在其他節點繼續按照“三”到“六”的步驟做一遍
八.驗證是否能ping通
1.在節點1運行
docker run -it --rm=true busybox sh
進入容器後執行
ifconfig
此容器的ip是172.17.80.2
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:50:02
inet addr:172.17.80.2 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
2.同樣在節點2啟動容器並查看ip:“172.17.87.2”
在此容器上執行
ping 172.17.80.2
PING 172.17.80.2 (172.17.80.2): 56 data bytes
64 bytes from 172.17.80.2: seq=0 ttl=62 time=0.415 ms
64 bytes from 172.17.80.2: seq=1 ttl=62 time=0.214 ms
部署Flannel,實現跨主機Docker容器通信