設定 GKE Granular NAT

本文件說明如何設定 Google Kubernetes Engine (GKE) 精細網路位址轉譯 (NAT),將專屬輸出 IP 位址指派給特定 Kubernetes 命名空間。

有了精細 NAT,您就能為 Public NAT 設定特定的來源外部 IP 位址,或為 Private NAT 設定特定的來源內部 IP 位址,這些 NAT 都是由 Cloud NAT 閘道提供。

如要設定精細 NAT,請使用兩個叢集範圍的自訂資源宣告路由政策:

  • CloudNATGateway:使用這項資源,為叢集佈建新的公開或私有雲 Cloud NAT 閘道,或將叢集設為使用現有的 Cloud NAT 閘道。Cloud NAT 閘道必須位於叢集的區域和虛擬私有雲網路中。
  • TrafficSelector:使用這項資源,將來源命名空間 (namespaceSelector) 和目的地 CIDR 對應至目標 CloudNATGateway,定義 egress 路由規則。

事前準備

設定 GKE Granular NAT 前,請先完成下列必要條件:

  • 確認叢集需求:確認 VPC 原生叢集執行的是 GKE 1.37.1-gke.2611000 以上版本,且已啟用 GKE Dataplane V2。Autopilot 和 Standard 叢集,以及區域和可用區叢集,都支援 GKE 精細 NAT。
  • 驗證 IAM 權限:
    • 確認您具備叢集的 Kubernetes Engine 管理員 (roles/container.admin) 角色,可套用自訂資源。
    • 如果使用 Shared VPC:主專案管理員必須在主專案的 IAM 政策中,將 Compute 網路管理員 (roles/compute.networkAdmin) 角色授予服務專案的 GKE 服務代理 (service-SERVICE_PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.com)。這項角色可讓 GKE 建立及管理 Shared VPC 虛擬私有雲網路中的基礎 Cloud NAT 閘道、Cloud Router 和 IP 位址。獨立虛擬私有雲網路不需要手動授予這項權限。
  • 視需要更新防火牆規則,納入連結本機 IP 位址: 在傳出封包離開節點之前,GKE Granular NAT 會將其來源 IP 位址轉譯為 169.254.32.0/20 範圍內的連結本機位址。如果貴組織強制執行嚴格的自訂防火牆規則,請確認規則允許這項行為:
    • 輸出規則:如果輸出允許規則的目標是叢集節點,請勿指定來源參數。如果指定來源參數,則必須包含連結本機位址範圍 (169.254.32.0/20)。
    • 輸入規則:如果輸入允許規則的目標是叢集節點,請避免指定目的地參數。如果指定目的地參數,則必須包含連結本機位址範圍 (169.254.32.0/20)。
  • 虛擬私有雲內部流量:確認 TrafficSelector 資源「不」包含叢集虛擬私有雲網路中子網路範圍的目的地 CIDR,或使用虛擬私有雲網路對接連線的其他虛擬私有雲網路。Private NAT 僅提供 NCC 輪輻連線能力,或使用 Hybrid NAT 的連線能力。
  • 虛擬私有雲路由規定:針對要由 Public NAT 處理的每個 CIDR,叢集的虛擬私有雲網路必須包含靜態路由,其目的地範圍包含 CIDR,且下一個躍點為預設網際網路閘道。

限制

  • 命名空間層級精細度:您只能在 Kubernetes 命名空間層級套用 GKE 精細網路位址轉譯政策。在預先發布版中,您無法使用命名空間中的標籤選取器,選取個別 Pod 或服務帳戶。
  • 閘道規則容量:每個 Cloud NAT 閘道最多支援 128 項 NAT 規則,因此每個閘道最多可為 128 個不同的 GKE 命名空間設定政策。如果叢集需要超過 128 個命名空間的輸出政策,則必須佈建額外的閘道。
  • Cloud NAT 限制:請查看標準 Cloud NAT 限制,因為這些限制也適用於精細 NAT 輸出流量。詳情請參閱Public NAT和Private NAT的限制。

在叢集啟用 GKE 精細 NAT

您可以在建立新叢集時啟用精細 NAT,也可以更新現有叢集。

建立新叢集

Autopilot

如要建立啟用精細 NAT 和 GKE Dataplane V2 的新叢集,請執行下列指令:

gcloud container clusters create-auto "CLUSTER_NAME" \
    --LOCATION_FLAG="LOCATION" \
    --network="VPC_NETWORK_NAME" \
    --enable-granular-nat \
    --network-suite=STANDARD

更改下列內容:

  • CLUSTER_NAME:新叢集的名稱。
  • LOCATION_FLAG:為區域叢集指定 --region。
  • LOCATION:叢集的 Compute Engine 區域或可用區 (例如 us-central1 或 us-central1-a)。
  • VPC_NETWORK_NAME:虛擬私有雲網路的名稱。

標準

如要建立啟用精細 NAT 和 GKE Dataplane V2 的新叢集,請執行下列指令:

gcloud container clusters create <var>CLUSTER_NAME</var> \
    --LOCATION_FLAG="LOCATION" \
    --network="VPC_NETWORK_NAME" \
    --enable-dataplane-v2 \
    --enable-granular-nat \
    --network-suite=STANDARD

更改下列內容:

  • CLUSTER_NAME:新叢集的名稱。
  • LOCATION_FLAG:為區域叢集指定 --region,或為可用區叢集指定 --zone。
  • LOCATION:叢集的 Compute Engine 區域或可用區 (例如 us-central1 或 us-central1-a)。
  • VPC_NETWORK_NAME:虛擬私有雲網路的名稱。

更新現有叢集

Autopilot

如要在現有叢集啟用精細 NAT,請執行下列指令:

gcloud container clusters update `CLUSTER_NAME` \
    --LOCATION_FLAG="LOCATION" \
    --enable-granular-nat

更改下列內容:

  • CLUSTER_NAME:現有叢集的名稱。
  • LOCATION_FLAG:如果現有叢集是區域叢集,請指定 --region;如果是可用區叢集,請指定 --zone。
  • LOCATION:叢集所在的 Compute Engine 區域或可用區。

標準

如要在現有 Standard 叢集啟用精細 NAT,請執行下列指令:

gcloud container clusters update `CLUSTER_NAME` \
    --LOCATION_FLAG="LOCATION" \
    --enable-granular-nat \
    --network-suite=STANDARD

更改下列內容:

  • CLUSTER_NAME:現有叢集的名稱。
  • LOCATION_FLAG:如果現有叢集是區域叢集,請指定 --region;如果是可用區叢集,請指定 --zone。
  • LOCATION:叢集所在的 Compute Engine 區域或可用區。

使用靜態外部 egress IP 位址設定公開精細 NAT

如果不同命名空間中的工作負載,必須向外部第三方服務 (例如強制執行嚴格 IP 位址許可清單的合作夥伴 API 或 Payment Gateway) 提供不同的靜態公開 IP 位址,請設定公開精細網路位址轉譯。

  1. 為 Cloud NAT 閘道建立區域性 Cloud Router。GKE 不會自動為您建立 Cloud Router。

    gcloud compute routers create ROUTER_NAME \
        --network=VPC_NETWORK_NAME \
        --region=REGION
    

    更改下列內容:

    • ROUTER_NAME:新區域 Cloud Router 的名稱。
    • VPC_NETWORK_NAME:虛擬私有雲網路的名稱。
    • REGION:叢集所在的 Compute Engine 區域 (例如 us-central1)。
  2. 在 GKE 叢集所在的區域預留靜態外部 IP 位址,為命名空間分配專屬的外部 egress IP 位址,並為閘道分配預設 IP 位址:

    gcloud compute addresses create nat-ip-default \
        --region=REGION
    gcloud compute addresses create nat-ip-1 \
        --region=REGION
    gcloud compute addresses create nat-ip-2 \
        --region=REGION
    

    將 REGION 替換為叢集所在的 Compute Engine 區域 (例如 us-central1)。

  3. 將下列資訊清單儲存為 cloud-nat-public.yaml,定義代管區域 Cloud NAT 閘道:

    kind: CloudNATGateway
    apiVersion: networking.gke.io/v1
    metadata:
      name: cloud-nat-public-managed
    spec:
      type: Public
      cloudRouter:
        name: projects/PROJECT_ID/regions/REGION/routers/ROUTER_NAME
      publicNATOptions:
        defaultNatExternalIPs:
        - name: nat-ip-default
      connectionOptions:
        udpTimeoutSeconds: 30
        tcpEstablishedTimeoutSeconds: 1200
        tcpTransitoryTimeoutSeconds: 30
        tcpTimeWaitSeconds: 120
        icmpTimeoutSeconds: 30
      portOptions:
        minPortsPerVM: 32
        maxPortsPerVM: 65536
      logOptions:
        enable: true
        filter: ALL
    

    更改下列內容:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • REGION:叢集和 Cloud Router 所在的 Compute Engine 區域。
    • ROUTER_NAME:Cloud Router 的名稱。
  4. 將下列資訊清單儲存為 traffic-selectors-public.yaml,將各個目標命名空間對應至指定的 Public NAT IP 位址:

    kind: TrafficSelector
    apiVersion: networking.gke.io/v2
    metadata:
      name: ns-red-public-route
      annotations:
        cloud.google.com/cloud-nat: '{"nat-external-ips":"nat-ip-1"}'
    spec:
      action:
        kind: CloudNATGateway
        name: cloud-nat-public-managed
      subject:
        pods:
          namespaceSelector:
            matchLabels:
              app: namespace-red-app
      egress:
        to:
          ipBlock:
            cidrs:
            - 0.0.0.0/0
    ---
    kind: TrafficSelector
    apiVersion: networking.gke.io/v2
    metadata:
      name: ns-green-public-route
      annotations:
        cloud.google.com/cloud-nat: '{"nat-external-ips":"nat-ip-2"}'
    spec:
      action:
        kind: CloudNATGateway
        name: cloud-nat-public-managed
      subject:
        pods:
          namespaceSelector:
            matchLabels:
              app: namespace-green-app
      egress:
        to:
          ipBlock:
            cidrs:
            - 0.0.0.0/0
    

    在這些資訊清單中:

    • cloud.google.com/cloud-nat 註解會指定哪個預留靜態外部 IP 位址會轉譯相符命名空間的輸出流量。
    • namespaceSelector.matchLabels 欄位會比對套用至目標 Kubernetes 命名空間的標籤。
    • 包含 0.0.0.0/0 的 egress.to.ipBlock.cidrs 清單會透過指定的 Cloud NAT 閘道,導向所有傳出網際網路流量。
  5. 將這兩個資訊清單套用至叢集:

    kubectl apply -f cloud-nat-public.yaml -f traffic-selectors-public.yaml
    

GKE 會自動佈建 Cloud NAT 閘道、根據命名空間工作負載選取器設定轉譯規則,並指派預留的外部 IP 位址。

為虛擬私有雲或 NCC 連線設定私人 Granular Cloud NAT

使用私人 Granular Cloud NAT,為連至地端部署網路的工作負載,或 NCC 中樞上的其他虛擬私有雲輪輻,指派特定內部 IP 位址範圍。

  1. 在虛擬私有雲網路中,建立用途為 PRIVATE_NAT 的專用子網路:

    gcloud compute networks subnets create private-nat-subnet-1 \
        --region=REGION \
        --network=VPC_NETWORK_NAME \
        --range=10.0.0.0/29 \
        --purpose=PRIVATE_NAT
    
    gcloud compute networks subnets create private-nat-subnet-2 \
        --region=REGION \
        --network=VPC_NETWORK_NAME \
        --range=10.0.1.0/29 \
        --purpose=PRIVATE_NAT
    

    更改下列內容:

    • REGION:叢集所在的 Compute Engine 區域。
    • VPC_NETWORK_NAME:虛擬私有雲網路的名稱。
  2. 將下列資訊清單儲存為 cloud-nat-private.yaml,建立私人 CloudNATGateway 資源:

    kind: CloudNATGateway
    apiVersion: networking.gke.io/v1
    metadata:
      name: cloud-nat-private-managed
    spec:
      type: Private
      privateNATOptions:
        # Optional: Specify hubLink only if you are routing traffic through Network Connectivity Center
        hubLink: //networkconnectivity.googleapis.com/projects/PROJECT_ID/locations/global/hubs/HUB_NAME
      cloudRouter:
        name: projects/PROJECT_ID/regions/REGION/routers/ROUTER_NAME
      connectionOptions:
        udpTimeoutSeconds: 30
        tcpEstablishedTimeoutSeconds: 1200
    

    更改下列內容:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • HUB_NAME:選用。Network Connectivity Center 中樞的名稱。只有在透過 Network Connectivity Center 轉送流量時,才需要指定這項設定。如果您未使用 Network Connectivity Center,請完全省略 privateNATOptions 區塊。
    • ROUTER_NAME:Cloud Router 的名稱。
  3. 將下列資訊清單儲存為 traffic-selectors-private.yaml,建立私人 TrafficSelector 資源,將命名空間對應至私人 NAT 閘道:

    kind: TrafficSelector
    apiVersion: networking.gke.io/v2
    metadata:
      name: ns-red-private-route
      annotations:
        cloud.google.com/cloud-nat: '{"nat-subnet":"private-nat-subnet-1"}'
    spec:
      action:
        kind: CloudNATGateway
        name: cloud-nat-private-managed
      subject:
        pods:
          namespaceSelector:
            matchLabels:
              app: namespace-red-app
      egress:
        to:
          ipBlock:
            cidrs:
            - 10.200.0.0/24
    ---
    kind: TrafficSelector
    apiVersion: networking.gke.io/v2
    metadata:
      name: ns-green-private-route
      annotations:
        cloud.google.com/cloud-nat: '{"nat-subnet":"private-nat-subnet-2"}'
    spec:
      action:
        kind: CloudNATGateway
        name: cloud-nat-private-managed
      subject:
        pods:
          namespaceSelector:
            matchLabels:
              app: namespace-green-app
      egress:
        to:
          ipBlock:
            cidrs:
            - 10.200.1.0/24
    
  4. 套用 Private NAT 設定:

    kubectl apply -f cloud-nat-private.yaml -f traffic-selectors-private.yaml
    

使用現有的 Cloud NAT 閘道

如果您在同一個虛擬私有雲網路中管理多個 GKE 叢集,或叢集共用次要 Pod IP 位址範圍,可以設定精細網路位址轉譯,使用相同區域和虛擬私有雲網路中的現有 Cloud NAT 閘道。這項設定可讓不同叢集的工作負載在連線至外部端點時,呈現統一的來源外部 IP 位址集區。

  1. 列出區域中現有的 Cloud NAT 閘道,找出閘道名稱、相關聯的 Cloud Router 和虛擬私有雲網路。區域和虛擬私有雲網路必須與 GKE 叢集使用的區域和網路相符:

    gcloud beta compute routers nats list \
        --router=ROUTER_NAME \
        --region=REGION \
        --format=yaml
    

    更改下列內容:

    • ROUTER_NAME:管理現有閘道的 Cloud Router 名稱。
    • REGION:叢集和現有閘道所在的 Compute Engine 區域 (例如 us-central1)。
  2. 將下列資訊清單儲存為 cloud-nat-shared.yaml,將叢集連結至現有閘道:

    kind: CloudNATGateway
    apiVersion: networking.gke.io/v1
    metadata:
      name: cloud-nat-public-shared
    spec:
      type: Public # type is a required field even for shared gateways. Can be either Public or Private.
      gatewayName: SHARED_NAT_GATEWAY_NAME
      cloudRouter:
        name: projects/PROJECT_ID/regions/REGION/routers/ROUTER_NAME
    

    更改下列內容:

    • SHARED_NAT_GATEWAY_NAME:現有 Cloud NAT 閘道的名稱。
    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • REGION:叢集和現有閘道所在的 Compute Engine 區域。
    • ROUTER_NAME:管理現有閘道的區域 Cloud Router 名稱。
  3. 將下列資訊清單儲存為 traffic-selector-shared.yaml,透過共用閘道路由命名空間的傳出流量:

    kind: TrafficSelector
    apiVersion: networking.gke.io/v2
    metadata:
      name: ns-shared-public-route
      annotations:
        cloud.google.com/cloud-nat: '{"nat-external-ips":"NAT_IP_NAME"}'
    spec:
      action:
        kind: CloudNATGateway
        name: cloud-nat-public-shared
      subject:
        pods:
          namespaceSelector:
            matchLabels:
              app: NAMESPACE_APP_LABEL
      egress:
        to:
          ipBlock:
            cidrs:
            - 0.0.0.0/0
    

    更改下列內容:

    • NAT_IP_NAME:在共用閘道上設定的保留外部靜態 IP 位址名稱。
    • NAMESPACE_APP_LABEL:套用至目標 Kubernetes 命名空間的標籤。
  4. 將資訊清單套用至需要共用閘道的每個 GKE 叢集:

    kubectl apply -f cloud-nat-shared.yaml -f traffic-selector-shared.yaml
    

    在每個叢集重複執行這個步驟,確保所選命名空間中的工作負載必須透過共用閘道輸出。

清除並停用精細 NAT

如要避免持續產生帳單費用,並防止 Google Cloud 資源孤立,請嚴格按照操作順序,從叢集移除精細網路位址轉譯。

如要清除精細 NAT 資源,請完成下列步驟:

  1. 刪除使用者建立的 TrafficSelector 資源:

    刪除為命名空間建立的 TrafficSelector 自訂資源。請勿使用 --all 旗標,因為刪除系統建立的選取器 (例如 gnat-intra-cluster 或 gnat-default-internet) 可能會中斷叢集流量。

    kubectl delete trafficselector SELECTOR_NAME
    

    將 SELECTOR_NAME 替換為 TrafficSelector 資源的名稱。

    精細 NAT 控制器會與 Google Cloud 網路協調,刪除與命名空間相關聯的特定 NAT 規則和工作負載對應。

    請先確認所有 TrafficSelector 資源都已終止,再繼續操作。

  2. 刪除 CloudNATGateway 資源:

    刪除叢集中的所有 CloudNATGateway 自訂資源:

    kubectl delete cloudnatgateway --all
    
    • 代管閘道:移除所有轉譯規則後,GKE 會自動刪除 Google Cloud 中的基礎 Cloud NAT 閘道。
    • 共用閘道:GKE 會清除與命名空間相關聯的 NAT 規則。GKE 不會移除叢集的次要 Pod IP 位址關聯,共用的 Cloud NAT 閘道也會繼續為其他叢集提供服務。
  3. 停用叢集上的功能:

    完全移除所有精細 NAT 自訂資源後,請停用叢集上的功能:

    gcloud container clusters update CLUSTER_NAME \
        --location=LOCATION \
        --no-enable-granular-nat
    

    更改下列內容:

    • CLUSTER_NAME:叢集名稱。
    • LOCATION:叢集所在的 Compute Engine 區域或可用區 (例如 us-central1 或 us-central1-a)。

疑難排解

如果遇到連線問題,請使用下列方法診斷精細 NAT 設定和資料層故障。

驗證資源狀態和記錄

  • 檢查自訂資源狀態:檢查 CloudNATGateway 或 TrafficSelector 自訂資源是否有錯誤條件:

    kubectl describe cloudnatgateway GATEWAY_NAME
    kubectl describe trafficselector SELECTOR_NAME
    

    更改下列內容:

    • GATEWAY_NAME:CloudNATGateway 資源的名稱。
    • SELECTOR_NAME:TrafficSelector 資源的名稱。
  • 查看 Cloud NAT 規則:確認基礎 Cloud NAT 閘道上的 GKE 工作負載選取器規則已啟用:

    gcloud beta compute routers nats rules list \
        --router=ROUTER_NAME \
        --nat=NAT_NAME \
        --region=REGION \
        --format=yaml
    

    更改下列內容:

    • ROUTER_NAME:區域性 Cloud Router 的名稱。
    • NAT_NAME:Cloud NAT 閘道的名稱。
    • REGION:資源所在的 Compute Engine 區域。
  • 分析 Cloud NAT 記錄:如果輸出流量遭到捨棄或無法轉譯,請在 Cloud Logging 查看 Cloud NAT 記錄項目。篩選 gke_workload_identity 欄位,直接將連線事件與特定 GKE 命名空間建立關聯。

如要進一步瞭解如何診斷一般 Cloud NAT 錯誤,請參閱「排解 Cloud NAT 問題」。

後續步驟