网络互通场景
云上有各种复杂的网络互通场景,例如:
- 容器要能访问外网
- 容器要能访问用户 IDC
- 容器要能访问云上其他 VPC
实现原理
数据面

当前的实现是让容器访问外网的数据包 SNAT 为节点 IP 出去,所以容器具备外网访问能力依赖于节点具备外网访问能力。
可以通过给节点分配外网 IP,绑定了弹性公网 IP,绑定 NAT 网关等方式让节点具备外网访问能力。
控制面
当前的实现是容器访问集群网络和 VPC 网络的不走 SNAT,访问其他网段都走 SNAT。
具体 iptables 规则的下发依赖于 ip-masq-agent。
ip-masq-agent 配置
配置示例:
VPC 网络:10.0.0.0/16
集群网络:172.18.0.0/16
# kubectl get cm -n kube-system ip-masq-agent-config -o yaml
apiVersion: v1 data: config: ‘{“NonMasqueradeCIDRs”:[“172.18.0.0/16”,”10.0.0.0/16”],”MasqLinkLocal”:true,”ResyncInterval”:”1m0s”}’ kind: ConfigMap metadata: name: ip-masq-agent-config namespace: kube-system ……
上述配置的意思是:访问目的网络 10.0.0.0/16 和 172.18.0.0/16 不做 SNAT,其他网段都做 SNAT,不对网段 169.254.0.0/16 做特殊处理,同步周期为1分钟。
iptables 具体规则
iptables -t nat -L IP-MASQ-AGENT
Chain IP-MASQ-AGENT (1 references)
target prot opt source destination
RETURN all — anywhere 172.18.0.0/16 / ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE / ADDRTYPE match dst-type !LOCAL
RETURN all — anywhere 10.0.0.0/16 / ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE / ADDRTYPE match dst-type !LOCAL
MASQUERADE all — anywhere anywhere / ip-masq-agent: outbound traffic should be subject to MASQUERADE (this match must come after cluster-local CIDR matches) / ADDRTYPE match dst-type !LOCAL
参考文献
弹性公网 IP:https://cloud.tencent.com/document/product/213/5733
NAT 网关:https://cloud.tencent.com/document/product/215/20079
https://github.com/aws/amazon-vpc-cni-k8s/blob/master/docs/images/ping2external.png
