為 Cloud Service Mesh 設定 IPv6 雙重堆疊

本頁面說明如何使用 Traffic Director (TD) 代理負載平衡器,在 Cloud Service Mesh 上平衡 IPv6 流量,以及如何從 IPv4 部署遷移至雙重堆疊 (IPv4 和 IPv6) 部署,以及如何從雙重堆疊遷移至 IPv4

在雙重堆疊部署作業中,您可以選擇要將 IPv4 或 IPv6 傳送至服務後端。代理程式或 gRPC 用戶端會依照您的偏好順序測試每個資料路徑,並選取符合您偏好設定和支援內容的資料路徑。

在 C++ 和 Python 的 gRPC 1.66.1 以上版本、Node 的 1.12 以上版本,以及 Go 的 1.71 以上版本中,支援雙堆疊功能。目前不支援 Java。

不支援雙堆疊的 gRPC 版本 (即Go 和 1.66 以下版本 (其他語言) 只會使用每個端點的第一個位址,並按照 TD 傳送的順序使用。

事前準備

本指南假設您已完成以下操作:

設定 IPv6 後端服務

在本節中,您將設定下列項目:

  • 兩個後端群組 (執行個體群組、代管執行個體群組或網路端點群組),每個區域各有一個。
  • 每個後端群組中各有兩個 VM 執行個體。
  • 用於確認執行個體健康狀態的檢查。
  • 防火牆規則可讓健康狀態檢查存取後端。
  • 後端服務。
  • 後端服務,其中包含已設定的兩個後端群組。

為後端設定子網路

下列指令會為 IPv4 和 IPv6 分配內部位址範圍,並允許子網路上的 VM 使用任一位址類型進行分配。

請注意,僅支援自訂模式子網路。不支援自動模式。您可以為整個 VPC 網路切換至自訂模式,然後填入 IPv6 後端 (MIG 或 NEG)。

  1. 建立雙堆疊網路:

    gcloud compute networks create NETWORK \
        --subnet-mode=custom \
        --enable-ula-internal-ipv6
    
  2. 為後端 VM 建立雙重堆疊子網路:

    gcloud compute networks subnets create SUBNET \
        --network=NETWORK \
        --range=PRIMARY_IPv4_RANGE \
        --stack-type=IPV4_IPV6 \
        --ipv6-access-type=IPv6_ACCESS_TYPE \
        --region=REGION
    

    更改下列內容:

    • SUBNET:新子網路的名稱。
    • NETWORK:虛擬私人雲端網路的名稱,此網路將包含新的子網路。
    • PRIMARY_IPv4_RANGE:新子網路的主要 IPv4 範圍,採用 CIDR 標記法。詳情請參閱「IPv4 子網路範圍」。
    • IPv6_ACCESS_TYPE:IPv6 存取權類型。可以是 EXTERNALINTERNAL
    • REGION: Google Cloud 要建立新子網路的區域。

設定後端

您可以選擇使用代管執行個體群組 (MIG)非代管執行個體群組網路端點群組 (NEG)

代管執行個體群組:MIG

  1. 使用 dual-stack-gateway-template 建立代管執行個體群組:

    gcloud alpha compute instance-templates create dual-stack-gateway-template \
    --region=REGION \
    --image-family=debian-12 \
    --image-project=debian-cloud \
    --tags=dual-stack-http-server \
    --network=NETWORK \
    --subnet=SUBNET \
    --stack-type=IPV4_IPV6 \
    --service-proxy=enabled,scope=gateway-proxy
    
  2. 建立代管閘道 Proxy 執行個體群組:

    gcloud compute instance-groups managed create dual-stack-ZONE-gateway-mig \
    --zone=ZONE \
    --size=1 \
    --template=dual-stack-gateway-template
    
  3. 建立後端代管執行個體群組:

    gcloud compute instance-templates create dual-stack-backend-template \
      --region=REGION \
      --image-family=debian-12 \
      --image-project=debian-cloud \
      --tags=dual-stack-http-server \
      --network=NETWORK \
      --subnet=SUBNET \
      --stack-type=IPV4_IPV6 \
      --metadata=startup-script="#! /bin/bash
    sudo apt-get update -y
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype <html><body><h1>'\`dual-stack-server\`'</h1></body></html>' | sudo tee /var/www/html/index.html"
    
    gcloud compute instance-groups managed create dual-stack-ZONE-backend-mig \
    --zone=ZONE \
    --size=1 \
    --template=dual-stack-backend-template
    
  4. 將命名通訊埠新增至代管執行個體群組:

    gcloud compute instance-groups set-named-ports us-ig-1 \
      --named-ports http:80 \
      --zone ZONE \
    
    gcloud compute instance-groups set-named-ports us-ig-2 \
      --named-ports http:80 \
      --zone ZONE
    

    負載平衡和自動修復應分別採用不同的健康狀態檢查。負載平衡的健康狀態檢查通常會設定為更嚴格,因為這些檢查將決定 VM 是否接收使用者流量,以及您是否需要快速重新導向流量。

    自動修復健康狀態檢查會使 Compute Engine 主動替換故障的 VM,因此這類健康狀態檢查應比負載平衡健康狀態檢查保守。請注意,雙重堆疊 VM 的自動修復功能會以 IPv4 健康狀態檢查為依據。

  5. 建立健康狀態檢查:

    gcloud compute health-checks create http dualstack-health-check-http \
    
  6. 確認防火牆規則允許健康狀態檢查:

    IPv4

    gcloud compute firewall-rules create dual-stack-allow-ipv4-health-check \
    --network=NETWORK \
    --action=ALLOW \
    --direction=INGRESS \
    --source-ranges=35.191.0.0/16,130.211.0.0/22 \
    --target-tags=dual-stack-http-server \
    --rules=tcp:22,80
    

    IPv6

    gcloud compute firewall-rules create dual-stack-allow-ipv6-health-check \
    --network=NETWORK \
    --action=ALLOW \
    --direction=INGRESS \
    --source-ranges=::/0 \
    --target-tags=dual-stack-http-server \
    --rules=tcp:22,80
    
  7. 為 MIG 設定自動修復政策,藉此套用健康狀態檢查:

    gcloud compute instance-groups managed update us-mig-1 \
      --health-check dualstack-health-check-http \
      --initial-delay 300 \
      --zone us-central1-a
    

如果 VM 正在啟動中,initial-delay 設定會延遲自動修復,以避免過早重新建立 VM 的可能性。當 VM 的 currentAction 欄位變更為 VERIFYING 時,初始延遲計時器會啟動。

非代管執行個體群組

  1. 設定執行個體群組:

    gcloud compute instance-groups unmanaged create us-ig-1 \
    --zone us-central1-a \
    
    gcloud compute instance-groups unmanaged create us-ig-2 \
    --zone us-central1-b
    
  2. 在每個執行個體群組中建立兩個雙層堆疊 VM 執行個體:

    gcloud compute instances create inst-us-central1-1 \
    --image-family debian-12 \
    --image-project debian-cloud \
    --tags network-lb \
    --zone us-central1-a \
    --network-interface [--network=NETWORK | --subnet=SUBNET] \
    --stack-type=IPV4_IPV6 \
    
    gcloud compute instances create inst-us-central1-2 \
    --image-family debian-12 \
    --image-project debian-cloud \
    --tags network-lb \
    --zone us-central1-a \
    --network-interface [--network=NETWORK | --subnet=SUBNET] \
    --stack-type=IPV4_IPV6 \
    
    gcloud compute instances create inst-us-central1-3 \
    --image-family debian-12 \
    --image-project debian-cloud \
    --tags network-lb \
    --zone us-central1-b \
    --network-interface [--network=NETWORK | --subnet=SUBNET] \
    --stack-type=IPV4_IPV6 \
    
    gcloud compute instances create inst-us-central1-4 \
    --image-family debian-12 \
    --image-project debian-cloud \
    --tags network-lb \
    --zone us-central1-b \
    --network-interface [--network=NETWORK | --subnet=SUBNET] \
    --stack-type=IPV4_IPV6
    

    系統會自動指派 IPv6 位址。

  3. 將 VM 新增至執行個體群組:

    gcloud compute instance-groups unmanaged add-instances us-ig-1 \
    --instances inst-us-central1-1,inst-us-central1-2 \
    --zone us-central1-a \
    
    gcloud compute instance-groups unmanaged add-instances us-ig-2 \
    --instances inst-us-central1-3,inst-us-central1-4 \
    --zone us-central1-b
    

NEG

  1. 新增後端,其中 --network-endpoint-type 為 GCE_VM_IP_PORT:

    gcloud compute network-endpoint-groups create us-neg-lb-1 \
      --network=NETWORK SUBNET \
      --zone=us-central1-a --network-endpoint-type=GCE_VM_IP_PORT \
    
    gcloud compute network-endpoint-groups create us-neg-lb-2 \
      --network=NETWORK SUBNET \
      --zone=us-central1-b  --network-endpoint-type=GCE_VM_IP_PORT
    
  2. 將端點新增至網路端點群組:

    gcloud compute network-endpoint-groups update us-neg-lb-1 \--zone=us-central1-a \
    --add-endpoint 'instance=inst-us-central1-1,ip=IPV4_ADRS_1, ipv6=IPV6_ADRS_1,port=80' \
    --add-endpoint 'instance=inst-us-central1-2,ip=IPV4_ADRS_2, ipv6=IPV6_ADRS_2,port=80' \
    
    gcloud compute network-endpoint-groups update us-neg-lb-2 --zone=us-central1-b \
    --add-endpoint 'instance=inst-us-central1-3,ip=IPV4_ADRS_3, ipv6=IPV6_ADRS_3,port=80' \
    --add-endpoint 'instance=inst-us-central1-4,ip=IPV4_ADRS_4,ipv6=IPV6_ADRS_4,port=80'
    

設定健康狀態檢查

  1. 為後端服務建立健康狀態檢查:

      gcloud compute health-checks create http[s] my-health-check 
    --global
    --request-path '/'
    --port SERVICE_PORT

    SERVICE_PORT 替換為介於 1 至 65535 之間的通訊埠編號。

  2. 建立防火牆規則,允許健康狀態檢查:

    gcloud compute firewall-rules create allow-scan-probe \
        --action allow \
        --description "allow-scan-probe" \
        --rules tcp:SERVICE_PORT \
        --source-ranges 2600:2d00:1:b029::/64 \
        --priority 10 \
        --network=NETWORK
    

範圍 2600:2d00:1:b029::/64 會用於健康檢查器的來源 IP 位址,用於探測 VM 的健康狀態。請注意,2600:2d00:1:b029::/64 會用做 IPv6 健康檢查器的來源 IP 位址,用於探查網路負載平衡的後端 VM 健康狀態。

使用 Backends 建立及更新後端服務

  1. 建立後端服務:

    gcloud compute backend-services create my-backend-service \
    --ip-address-selection-policy PREFER_IPV6  \
    --global \
    --health-checks my-health-check \
    --load-balancing-scheme=INTERNAL_SELF_MANAGED \
    --timeout=5m
    
  2. 將後端新增至後端服務:

    gcloud compute backend-services add-backend my-backend-service \
    --instance-group us-ig-1 \
    --instance-group-zone us-central1-a \
    --global \
    
    gcloud compute backend-services add-backend my-backend-service \
    --instance-group us-ig-2 \
    --instance-group-zone us-central1-b \
    --global
    

設定 Cloud Service Mesh 服務

本節說明如何在 Traffic Director 中設定 IPv6 服務。服務可能會是服務網格設定的一部分,也可能用於設定服務閘道,例如執行 Envoy 的 VM。

後端服務已設定 PREFER_IPV6,您可以建立 appnet 閘道資源。

建立閘道資源

  1. 在名為 dual-stack-gateway.yaml 的檔案中,建立 HTTP 流量的 Gateway 規格:

    cat <<EOF | tee dual-stack-gateway.yaml
    name: dual-stack-gateway
    scope: gateway-proxy
    ipVersion: IPV6
    ports:
    - 80
    type: OPEN_MESH
    EOF
    
  2. 根據 dual-stack-gateway.yaml 規格建立 Gateway 資源:

    gcloud network-services gateways import dual-stack-gateway \
      --source=dual-stack-gateway.yaml \
      --location=global
    

使用 HTTPRoute 設定路由

  1. 在名為 dual-stack-http_route.yaml 的檔案中,建立 HTTPRoute 規格:

    cat <<EOF | tee dual-stack-http-route.yaml
    name: dual-stack-http-route
    hostnames:
    - dual-stack-server
    gateways:
    - projects/PROJECT_ID/locations/global/gateways/dual-stack-gateway
    rules:
    - action:
        destinations:
        - serviceName: "projects/PROJECT_ID/locations/global/backendServices/dual-stack-backend-service"
    EOF
    
  2. 使用 dual-stack-http-route.yaml 中的規範建立 HTTPRoute 資源。

    gcloud network-services http-routes import dual-stack-http-route \
      --source=dual-stack-http-route.yaml \
      --location=global
    
  3. 如要驗證端對端連線,請從閘道 MIG 以 SSH 連線至 VM,然後執行下列指令:

    curl -H 'Host: dual-stack-server' http://[::]
    

    輸出結果會與下列內容類似:

    <!doctype <html><body><h1>'`dual-stack-server`'</h1></body></html>
    

從 IPv4 遷移至雙重堆疊

您必須符合下列先決條件,才能從 IPv4 遷移至雙層架構:

  • 現有的單一堆疊 VM 執行個體群組 us-ig-1us-ig-2,搭配 IPV4_ONLY 堆疊和現有 VM
  • 單一 IPv4 後端服務 my-ipv4-backend-service 指向 us-ig-1us-ig-2
  • IPV4_ONLY VM 子網路
  • 使用 IPv4 版本位址設定的閘道資源

將子網路升級為雙堆疊

更新後端現有的子網路,以支援 IPv6:

gcloud compute networks subnets update SUBNET \
  --stack-type IPV4_IPV6 \
  --ipv6-access-type=IPv6_ACCESS_TYPE \

更改下列內容:

  • SUBNET:新子網路的名稱。
  • IPv6_ACCESS_TYPE:IPv6 存取權類型。可以是 EXTERNALINTERNAL

將每個 VM 升級為雙堆疊

gcloud compute instances network-interfaces update EXISTING_VM_NAME \
  --stack-type=IPV4_IPV6 \
  --zone=us-central1

EXISTING_VM_NAME 替換為現有 VM 的名稱。

將新的雙堆疊 VM 新增至各個執行個體群組

  1. 建立新的 VM 執行個體:

    gcloud compute instances create my-new-vm-1 \
      --image-family debian-12 \
      --tags network-lb \
      --zone us-central1-a \
      --network-interface [--network=NETWORK | --subnet=SUBNET] \
      --stack-type=IPV4_IPV6 \
    
    gcloud compute instances create my-new-vm-2 \
      --image-family debian-12 \
      --tags network-lb \
      --zone us-central1-b \
      --network-interface [--network=NETWORK | --subnet=SUBNET] \
      --stack-type=IPV4_IPV6
    
  2. 將新的執行個體新增至執行個體群組:

    gcloud compute instance-groups unmanaged add-instances us-ig-1 \
      --instances my-new-vm-1 \
      --zone us-central1-a \
    
    gcloud compute instance-groups unmanaged add-instances us-ig-2 \
      --instances my-new-vm-2 \
      --zone us-central1-b
    
  3. 建立 IPv6 後端服務:

    gcloud compute backend-services create dual-stack-backend-service \
      --global \
      --load-balancing-scheme=INTERNAL_SELF_MANAGED \
      --protocol=HTTP \
      --health-checks=dual-stack-health-check-http \
      --ip-address-selection-policy=PREFER_IPV6
    
  4. 將更新後的 Instance-Group 新增至新建立的雙層架構後端服務:

    gcloud compute backend-services add-backend dual-stack-backend-service \
      --instance-group=us-ig-1 \
      --instance-group-zone=ZONE \
      --global
    
    gcloud compute backend-services add-backend dual-stack-backend-service \
      --instance-group=us-ig-2 \
      --instance-group-zone=ZONE \
      --global
    

從雙重堆疊遷移至 IPv4

您必須符合下列先決條件,才能從雙層堆疊遷移至 IPv4:

  • 現有的雙堆疊 VM 執行個體群組 us-ig-1us-ig-2,搭配 IPV4_IPV6 堆疊和現有 VM
  • 單一 IPv6 後端服務 my-ipv6-backend-service 指向 us-ig-1us-ig-2
  • IPV4_IPV6 VM 子網路
  • 閘道資源

將每個 VM 降級至 IPv4

gcloud compute instances network-interfaces update EXISTING_VM_NAME \
  --stack-type=IPV4_ONLY \
  --zone=us-central1

將新的 IPv4 堆疊 VM 新增至各個執行個體群組

  1. 建立新的 VM 執行個體:

    gcloud compute instances create my-new-vm-1 \
      --image-family debian-12 \
      --tags network-lb \
      --zone us-central1-a \
      --network-interface [--network=NETWORK | --subnet=SUBNET] \
      --stack-type=IPV4_ONLY \
    
    gcloud compute instances create my-new-vm-2 \
      --image-family debian-12 \
      --tags network-lb \
      --zone us-central1-b \
      --network-interface [--network=NETWORK | --subnet=SUBNET] \
      --stack-type=IPV4_ONLY
    
  2. 將新的執行個體新增至執行個體群組:

    gcloud compute instance-groups unmanaged add-instances us-ig-1 \
      --instances my-new-vm-1 \
      --zone us-central1-a \
    
    gcloud compute instance-groups unmanaged add-instances us-ig-2 \
      --instances my-new-vm-2 \
      --zone us-central1-b
    
  3. 建立 IPv4 後端服務:

    gcloud compute backend-services create my-ipv4-backend-service \
      –ip-address-selection-policy IPV4_ONLY \
      --global \
      --health-checks my-health-check \
      --load-balancing-scheme INTERNAL_SELF_MANAGED \
      --timeout=5m
    
  4. 將更新後的 Instance-Groups 新增至新建立的 IPv4 後端服務:

    gcloud compute backend-services add-backend my-ipv4-backend-service \
      --instance-group us-ig1 \
      --instance-group-zone us-central1-a \
      --global \
    
    gcloud compute backend-services add-backend my-ipv4-backend-service \
      --instance-group us-ig2 \
      --instance-group-zone us-central1-b \
      --global
    

    如今,IPv4 和 IPv6 後端服務都能提供流量。更新網址對應,將部分用戶端流量導向新的 IPv4 後端服務。