-ip-masq: flanneld 为访问 Pod 网络外的流量设置 SNAT 规则,同时将传递给 Docker 的变量 --ip-masq(/run/flannel/docker 文件中)设置为 false,这样 Docker 将不再创建 SNAT 规则; Docker 的 --ip-masq 为 true 时,创建的 SNAT 规则比较“暴力”:将所有本节点 Pod 发起的、访问非 docker0 接口的请求做 SNAT,这样访问其他节点 Pod 的请求来源 IP 会被设置为 flannel.1 接口的 IP,导致目的 Pod 看不到真实的来源 Pod IP。 flanneld 创建的 SNAT 规则比较温和,只对访问非 Pod 网段的请求做 SNAT。
分发 flanneld systemd unit 文件到所有节点
1 2 3 4 5 6 7
cd /opt/k8s/work source /opt/k8s/bin/environment.sh for node_ip in${NODE_IPS[@]} do echo">>> ${node_ip}" scp flanneld.service root@${node_ip}:/etc/systemd/system/ done
启动 flanneld 服务
1 2 3 4 5 6
source /opt/k8s/bin/environment.sh for node_ip in${NODE_IPS[@]} do echo">>> ${node_ip}" ssh root@${node_ip}"systemctl daemon-reload && systemctl enable flanneld && systemctl restart flanneld" done
检查启动结果
1 2 3 4 5 6
source /opt/k8s/bin/environment.sh for node_ip in${NODE_IPS[@]} do echo">>> ${node_ip}" ssh root@${node_ip}"systemctl status flanneld|grep Active" done
[root@zhangjun-k8s01 work]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:22:0d:33:89:75 brd ff:ff:ff:ff:ff:ff inet 172.27.137.240/20 brd 172.27.143.255 scope global dynamic eth0 valid_lft 100647283sec preferred_lft 100647283sec 3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default link/ether ce:9c:a9:08:50:03 brd ff:ff:ff:ff:ff:ff inet 172.30.80.0/32 scope global flannel.1 valid_lft forever preferred_lft forever
flannel.1 网卡的地址为分配的 Pod 子网段的第一个 IP(.0),且是 /32 的地址;
1 2 3
[root@zhangjun-k8s01 work]# ip route show |grep flannel.1 172.30.32.0/24 via 172.30.32.0 dev flannel.1 onlink 172.30.184.0/24 via 172.30.184.0 dev flannel.1 onlink