flannel 支持多种 backend,前面咱们讨论的是 vxlan,host-gw 是 flannel 的另外一个 backend,本节会将前面的 vxlan backend 切换成 host-gw。
docker
与 vxlan 不一样,host-gw 不会封装数据包,而是在主机的路由表中建立到其余主机 subnet 的路由条目,从而实现容器跨主机通讯。要使用 host-gw 首先修改 flannel 的配置 flannel-config.json: 数据库
{ json
"Network": "10.2.0.0/16", 网络
"SubnetLen": 24, 性能
"Backend": { 学习
"Type": "host-gw" ui
} spa
}
code
Type
用 host-gw
替换原先的 vxlan
。更新 etcd 数据库: orm
etcdctl --endpoints=192.168.56.101:2379 set /docker-test/network/config < flannel-config.json
Ctrl+C 掉以前 host1 和 host2 的 flanneld 进程并重启。
flanneld -etcd-endpoints=http://192.168.56.101:2379 -iface=enp0s8 -etcd-prefix=/docker-test/network
host1 上 flanneld 启动输出以下:
与以前 vxlan backend 启动时有几点不一样:
① flanneld 检查到原先已分配的 subnet 10.2.40.0/24,重用之。
② flanneld 从 etcd 数据库中检索到 host2 的 subnet 10.2.17.0/24,但由于其 type=vxlan
,当即忽略。
③ 两分钟后,再次发现 subnet 10.2.17.0/24,将其加到路由表中。此次没有忽略 subnet 的缘由是此时咱们在 host2 上重启了 flanneld,根据当前 etcd 的配置使用 host-gw backend。
查看 host1 的路由表,增长了一条到 10.2.17.0/24 的路由,网关为 host2 的 IP 192.168.56.105。
相似的,host2 启动 flanneld 时会重用 subnet 10.2.17.0/24,并将 host1 的 subnet 10.2.40.0/24 添加到路由表中,网关为 host1 IP 192.168.56.104。
从 /run/flannel/subnet.env 能够看到 host-gw 使用的 MTU 为 1500:
这与 vxlan MTU=1450 不一样,因此应该修改 docker 启动参数 --mtu=1500
并重启 docker daemon。
下面对 host-gw 和 vxlan 这两种 backend 作个简单比较。
host-gw 把每一个主机都配置成网关,主机知道其余主机的 subnet 和转发地址。vxlan 则在主机间创建隧道,不一样主机的容器都在一个大的网段内(好比 10.2.0.0/16)。
虽然 vxlan 与 host-gw 使用不一样的机制创建主机之间链接,但对于容器则无需任何改变,bbox1 仍然能够与 bbox2 通讯。
因为 vxlan 须要对数据进行额外打包和拆包,性能会稍逊于 host-gw。
至此 flannel 已经讨论完了,下一节咱们开始学习另外一个网络方案:Weave。