IP Masquerade Agent 使用者指南

此頁面說明如何設定與啟用 ip-masq-agent

事前準備

您需要有一個 Kubernetes 叢集,且 kubectl 命令列工具必須設定為與您的叢集通訊。建議在至少有兩個節點且未作為控制平面主機的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用下列 Kubernetes 實驗環境

若要檢查版本,請輸入 kubectl version

IP Masquerade Agent 使用者指南

ip-masq-agent 設定 iptables 規則,以將 Pod 的 IP 位址隱藏在叢集節點的 IP 位址之後。這通常在將流量傳送到叢集 Pod CIDR 範圍之外的目的地時完成。

主要術語

  • NAT (網路位址轉譯):是一種透過修改 IP 標頭中的來源和/或目的地位址資訊,將一個 IP 位址重新對應到另一個 IP 位址的方法。通常由執行 IP 路由的裝置執行。
  • 偽裝 (Masquerading):NAT 的一種形式,通常用於執行多對一的位址轉譯,其中多個來源 IP 位址被遮罩在單一位址之後,該位址通常是執行 IP 路由的裝置。在 Kubernetes 中,這是節點的 IP 位址。
  • CIDR (無類別網域間路由):基於可變長度子網路遮罩,允許指定任意長度的前綴。CIDR 引入了一種新的 IP 位址表示方法,現在通常稱為 CIDR 標記法,其中位址或路由前綴以尾碼寫入,表示前綴的位元數,例如 192.168.2.0/24。
  • 鏈結本機 (Link Local):鏈結本機位址是一種網路位址,僅在主機所連接的網路區段或廣播網域內的通訊中有效。IPv4 的鏈結本機位址定義於 CIDR 標記法的 169.254.0.0/16 位址區塊中。

ip-masq-agent 配置 iptables 規則,以處理在將流量傳送到叢集節點 IP 和叢集 IP 範圍之外的目的地時,對節點/Pod IP 位址進行偽裝 (masquerading)。這實際上是將 Pod IP 位址隱藏在叢集節點的 IP 位址之後。在某些環境中,發送到「外部」位址的流量必須來自已知的機器位址。例如,在 Google Cloud 中,任何發送到網際網路的流量都必須來自 VM 的 IP。當使用容器時 (如 Google Kubernetes Engine 中),Pod IP 將因出口 (egress) 而被拒絕。為了避免這種情況,我們必須將 Pod IP 隱藏在 VM 自身的 IP 位址之後,這通常稱為「偽裝 (masquerade)」。依預設,代理程式 (agent) 配置為將 RFC 1918 指定的三個私有 IP 範圍視為非偽裝 CIDR。這些範圍是 10.0.0.0/8172.16.0.0/12192.168.0.0/16。依預設,代理程式也會將鏈結本機 (169.254.0.0/16) 視為非偽裝 CIDR。代理程式配置為每 60 秒從 /etc/config/ip-masq-agent 位置重新載入其配置,這也是可配置的。

masq/non-masq example

代理程式配置檔案必須以 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

若要建立 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 範圍。例如,若要僅允許 ip-masq-agent 考慮 10.0.0.0/8,您可以在名為「config」的檔案中建立下列 ConfigMap

執行下列命令以將 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-agent 處理,這會設定適當的 iptables 規則。若要讓 ip-masq-agent 忽略鏈結本機,您可以在 ConfigMap 中將 masqLinkLocal 設定為 true。

nonMasqueradeCIDRs:
  - 10.0.0.0/8
resyncInterval: 60s
masqLinkLocal: true
上次修改時間:2023 年 7 月 31 日下午 4:52 PST:更新 ip-masq-agent.md 以新增遺失的 `/` (9d8ad64f9b)