本页展示了如何配置和启用 ip-masq-agent。
你需要有一个 Kubernetes 集群,并且必须配置 kubectl 命令行工具以与你的集群通信。建议在至少有两个节点的集群上运行本教程,且这些节点不能作为控制平面主机。如果你还没有集群,可以通过 minikube 创建一个,或者使用以下 Kubernetes 演练场之一。
要检查版本,请输入 kubectl version。
ip-masq-agent 用于配置 iptables 规则,将 Pod 的 IP 地址隐藏在集群节点的 IP 地址之后。这通常在向集群 Pod CIDR 范围之外的目的地发送流量时执行。
ip-masq-agent 配置 iptables 规则,用于在向集群节点 IP 和集群 IP 范围之外的目的地发送流量时处理节点/Pod IP 地址的伪装。这本质上是将 Pod IP 地址隐藏在集群节点 IP 地址之后。在某些环境中,发送到“外部”地址的流量必须来自已知的机器地址。例如,在 Google Cloud 中,任何发送到互联网的流量都必须来自虚拟机的 IP。当使用容器时(如在 Google Kubernetes Engine 中),Pod IP 在出口流量中会被拒绝。为了避免这种情况,我们必须将 Pod IP 隐藏在虚拟机自身的 IP 地址之后——这通常被称为“伪装”。默认情况下,代理被配置为将 RFC 1918 指定的三个私有 IP 范围视为非伪装 CIDR。这些范围是 10.0.0.0/8、172.16.0.0/12 和 192.168.0.0/16。默认情况下,代理也会将链路本地地址 (169.254.0.0/16) 视为非伪装 CIDR。代理配置为每 60 秒(此时间也可配置)从 /etc/config/ip-masq-agent 位置重新加载其配置。

代理配置文件必须使用 YAML 或 JSON 语法编写,并且可以包含三个可选键:
nonMasqueradeCIDRs:CIDR 标记法字符串列表,指定非伪装范围。masqLinkLocal:一个布尔值 (true/false),指示是否对链路本地前缀 169.254.0.0/16 的流量进行伪装。默认值为 false。resyncInterval:代理尝试从磁盘重新加载配置的时间间隔。例如:'30s',其中 's' 表示秒,'ms' 表示毫秒。发送到 10.0.0.0/8、172.16.0.0/12 和 192.168.0.0/16 范围的流量将不会被伪装。任何其他流量(假定为互联网流量)都将被伪装。Pod 的本地目的地示例包括其节点的 IP 地址、其他节点的地址或集群 IP 范围内的 IP 地址。默认情况下,任何其他流量都将被伪装。以下条目显示了 ip-masq-agent 应用的默认规则集:
iptables -t nat -L IP-MASQ-AGENT
target prot opt source destination
RETURN all -- anywhere 169.254.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/8 /* ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE */ ADDRTYPE match dst-type !LOCAL
RETURN all -- anywhere 172.16.0.0/12 /* ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE */ ADDRTYPE match dst-type !LOCAL
RETURN all -- anywhere 192.168.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
默认情况下,在 GCE/Google Kubernetes Engine 中,如果启用了网络策略,或者您使用的集群 CIDR 不在 10.0.0.0/8 范围内,ip-masq-agent 将在您的集群中运行。如果您在其他环境中运行,可以将 ip-masq-agent DaemonSet 添加到您的集群中。
要创建 ip-masq-agent,请运行以下 kubectl 命令:
kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/ip-masq-agent/master/ip-masq-agent.yaml
您还必须将适当的节点标签应用于您希望代理在其上运行的集群中的任何节点。
kubectl label nodes my-node node.kubernetes.io/masq-agent-ds-ready=true
更多信息可以在 ip-masq-agent 文档中找到,点击此处。
在大多数情况下,默认规则集应该足够;但是,如果这对您的集群不适用,您可以创建并应用一个 ConfigMap 来定制受影响的 IP 范围。例如,要仅允许将 10.0.0.0/8 纳入 ip-masq-agent 的考虑范围,您可以在名为 "config" 的文件中创建以下 ConfigMap。
重要的是文件必须命名为 config,因为默认情况下,它将被 ip-masq-agent 用作查找键。
nonMasqueradeCIDRs:
- 10.0.0.0/8
resyncInterval: 60s
运行以下命令将 configmap 添加到您的集群中:
kubectl create configmap ip-masq-agent --from-file=config --namespace=kube-system
这将更新位于 /etc/config/ip-masq-agent 的文件,该文件会定期(每 resyncInterval)进行检查并应用于集群节点。重新同步间隔期满后,您应该会看到 iptables 规则反映了您的更改。
iptables -t nat -L IP-MASQ-AGENT
Chain IP-MASQ-AGENT (1 references)
target prot opt source destination
RETURN all -- anywhere 169.254.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/8 /* ip-masq-agent: cluster-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
默认情况下,链路本地范围 (169.254.0.0/16) 也由 ip-masq 代理处理,它会设置适当的 iptables 规则。要使 ip-masq-agent 忽略链路本地地址,您可以在 ConfigMap 中将 masqLinkLocal 设置为 true。
nonMasqueradeCIDRs:
- 10.0.0.0/8
resyncInterval: 60s
masqLinkLocal: true