配置docker 连接flannel
编辑host1的docker配置文件/etc/systemd/system/docker.service.d/10-machine.conf,设置 --bip 和 --mtu ,这两个的值要与/run/flannel/subnet.env中对应的 subnet和mtu 值一致
root@host1:~# cat /run/flannel/subnet.env
FLANNEL_NETWORK=10.2.0.0/16
FLANNEL_SUBNET=10.2.46.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=false
root@host1:~# cat /etc/systemd/system/docker.service.d/10-machine.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver overlay2 --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=generic --bip=10.2.46.1/24 --mtu=1450
#--cluster-store=consul://10.12.31.213:8500 --cluster-advertise=ens160:2376
Environment=
root@host1:~# systemctl daemon-reload
root@host1:~# systemctl restart docker.service
root@host1:~# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/local/bin/flanneld-amd64 -etcd-endpoints=http://10.12.31.213:2379 -iface=ens160 -etcd-prefix=/docker-test/network &
exit 0
root@host1:~# ip route
default via 10.12.28.6 dev ens160 onlink
10.2.1.0/24 via 10.2.1.0 dev flannel.1 onlink
10.2.46.0/24 dev docker0 proto kernel scope link src 10.2.46.1
10.12.28.0/22 dev ens160 proto kernel scope link src 10.12.31.211
172.22.0.0/16 via 10.12.28.1 dev ens160
root@host1:~# ip addr show flannel.1
6: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
link/ether 66:2b:a5:56:0d:c6 brd ff:ff:ff:ff:ff:ff
inet 10.2.46.0/32 scope global flannel.1
valid_lft forever preferred_lft forever
inet6 fe80::642b:a5ff:fe56:dc6/64 scope link
valid_lft forever preferred_lft forever
root@host1:~# docker run -itd --name=bboxf1 busybox
2fc115c2d9cd22be5537690c795ac034d20fc1916dde4c5e7a38e891e2226bc9
root@host1:~# docker exec bboxf1 ip r
default via 10.2.46.1 dev eth0
10.2.46.0/24 dev eth0 scope link src 10.2.46.2
root@host2:~# ip route
default via 10.12.28.6 dev ens160 onlink
10.2.1.0/24 dev docker0 proto kernel scope link src 10.2.1.1
10.2.46.0/24 via 10.2.46.0 dev flannel.1 onlink
10.12.28.0/22 dev ens160 proto kernel scope link src 10.12.31.212
172.22.0.0/16 via 10.12.28.1 dev ens160
root@host2:~# ip addr show flannel.1
6: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
link/ether 8a:8c:b5:42:47:40 brd ff:ff:ff:ff:ff:ff
inet 10.2.1.0/32 scope global flannel.1
valid_lft forever preferred_lft forever
inet6 fe80::888c:b5ff:fe42:4740/64 scope link
valid_lft forever preferred_lft forever
root@host2:~# docker run -itd --name=bboxf2 busybox
5493e6a36ae9d812c2df20fb5628dd070f83c60ea23abbd1dfb98387c49882cd
root@host2:~# docker exec bboxf2 ip r
default via 10.2.1.1 dev eth0
10.2.1.0/24 dev eth0 scope link src 10.2.1.2
网络拓扑大致如下