為 Cloud Service Mesh 設定 IPv6 雙重堆疊
本頁面說明如何使用 Traffic Director (TD) 代理程式型負載平衡器,在 Cloud Service Mesh 上進行 IPv6 流量負載平衡,以及如何從 IPv4 部署作業遷移至雙重堆疊 (IPv4 和 IPv6) 部署作業,以及如何從雙重堆疊遷移至 IPv4。
在雙重堆疊部署作業中,您可以選擇將 IPv4 或 IPv6 傳送至服務後端。Proxy 或 gRPC 用戶端會依偏好順序測試每個資料路徑,並選取符合偏好設定和支援的資料路徑。
gRPC 1.66.1 以上版本支援 C++ 和 Python 的雙堆疊功能,Node 則支援 1.12 以上版本,Go 支援 1.71 以上版本。Java 目前不支援這項功能。
不支援雙重堆疊的 gRPC 版本 (即 Go 和 1.66 之前的版本 (其他語言) 只會使用每個端點的第一個位址,順序由 TD 傳送。
事前準備
本指南假設您已完成下列事項:
- 使用 Envoy 和無 Proxy 的工作負載設定服務路由 API。
- 已完成新手上路指南「繼續進行設定程序」一節中的步驟。
設定 IPv6 後端服務
在本節中,您將設定下列項目:
- 兩個後端群組 (執行個體群組、代管執行個體群組或網路端點群組),分別位於同一區域內的兩個不同可用區。
- 每個後端群組有兩個 VM 執行個體。
- 確認執行個體健康狀態的檢查。
- 防火牆規則允許健康狀態檢查連線至後端。
- 後端服務。
- 後端服務,用於納入設定的兩個後端群組。
為後端設定子網路
下列指令會為 IPv4 和 IPv6 分配內部地址範圍,並允許子網路上的 VM 分配任一地址類型。
請注意,僅支援自訂模式子網路。不支援自動模式。您可以將整個 VPC 網路切換為自訂模式,然後填入 IPv6 後端 (MIG 或 NEG)。
建立雙堆疊網路:
gcloud compute networks create NETWORK \ --subnet-mode=custom \ --enable-ula-internal-ipv6為後端 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 存取權類型。
可以是
EXTERNAL或INTERNAL。 - REGION:要建立新子網路的 Google Cloud 區域。
設定後端
您可以選擇使用代管執行個體群組 (MIG)、非代管執行個體群組或網路端點群組 (NEG)。
代管執行個體群組:MIG
使用
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
建立閘道 Proxy 代管執行個體群組:
gcloud compute instance-groups managed create dual-stack-ZONE-gateway-mig \ --zone=ZONE \ --size=1 \ --template=dual-stack-gateway-template
建立後端代管執行個體群組:
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
將具名通訊埠新增至代管執行個體群組:
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 健康狀態檢查為依據。
建立健康狀態檢查:
gcloud compute health-checks create http dualstack-health-check-http \
確認防火牆規則允許健康狀態檢查:
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
為 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 時,初始延遲計時器會啟動。
非代管執行個體群組
設定執行個體群組:
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
在每個執行個體群組中建立兩個雙堆疊 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 位址。
將 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
新增後端,其中
--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
將端點新增至網路端點群組:
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'
設定健康狀態檢查
為後端服務建立健康狀態檢查:
gcloud compute health-checks create http[s] my-health-check
--global
--request-path '/'
--port SERVICE_PORT將 SERVICE_PORT 替換為通訊埠號碼 (1 到 65535)。
建立防火牆規則,允許健康狀態檢查:
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 建立及更新後端服務
建立後端服務:
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
將後端新增至後端服務:
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 閘道資源。
建立閘道資源
在名為
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從
dual-stack-gateway.yaml規格建立 Gateway 資源:gcloud network-services gateways import dual-stack-gateway \ --source=dual-stack-gateway.yaml \ --location=global
使用 HTTPRoute 設定路由
在名為
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使用
dual-stack-http-route.yaml中的規格建立 HTTPRoute 資源。gcloud network-services http-routes import dual-stack-http-route \ --source=dual-stack-http-route.yaml \ --location=global
如要驗證端對端連線,請從閘道 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-1和us-ig-2,具有IPV4_ONLY堆疊和現有 VM - 指向
us-ig-1和us-ig-2的單一 IPv4 後端服務my-ipv4-backend-service IPV4_ONLYVM 子網路- 設定 IPv4 版本位址的閘道資源
將子網路升級為雙堆疊
更新後端現有的子網路,以支援 IPv6:
gcloud compute networks subnets update SUBNET \ --stack-type IPV4_IPV6 \ --ipv6-access-type=IPv6_ACCESS_TYPE \
更改下列內容:
- SUBNET:新子網路的名稱。
- IPv6_ACCESS_TYPE:IPv6 存取權類型。
可以是
EXTERNAL或INTERNAL。
將每個 VM 升級為雙堆疊
gcloud compute instances network-interfaces update EXISTING_VM_NAME \ --stack-type=IPV4_IPV6 \ --zone=us-central1
將 EXISTING_VM_NAME 替換為現有 VM 的名稱。
在每個執行個體群組中新增雙堆疊 VM
建立新的 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
將新執行個體新增至執行個體群組:
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
建立 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
將更新後的執行個體群組新增至新建立的雙堆疊後端服務:
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-1和us-ig-2,具有IPV4_IPV6堆疊和現有 VM - 指向
us-ig-1和us-ig-2的單一 IPv6 後端服務my-ipv6-backend-service - IPV4_IPV6 VM 子網路
- 閘道資源
將每個 VM 降級至 IPv4
gcloud compute instances network-interfaces update EXISTING_VM_NAME \ --stack-type=IPV4_ONLY \ --zone=us-central1
在每個執行個體群組中新增 IPv4 堆疊 VM
建立新的 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
將新執行個體新增至執行個體群組:
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
建立 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
將更新後的執行個體群組新增至新建立的 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 後端服務。