設定多雲端或混合式網格
本頁說明如何為下列平台設定多雲或混合式網格:
- 混合式:GKE on Google Cloud 和 Google Distributed Cloud (預先發布版)
- 混合式:GKE on Google Cloud 和 Google Distributed Cloud (預先發布版)
- 多雲:GKE on Google Cloud 和 Amazon EKS (預先發布版)
按照這些操作說明設定兩個叢集後,您可以擴充這個程序,將任意數量的叢集併入網格。
必要條件
- 所有叢集都必須註冊至同一個機群主專案。
- 所有 GKE 叢集都必須位於同一個網路的共用虛擬私有雲設定中。
- 網格中的每個叢集都必須能連上叢集的 Kubernetes 控制層位址和閘道位址。您應允許 GKE 叢集所在的 Google Cloud 專案建立外部負載平衡類型。建議您使用授權網路和 VPC 防火牆規則來限制存取權。
- 系統不支援私人叢集,包括 GKE 私人叢集。如果您使用 Google Distributed Cloud 和 Google Distributed Cloud 等地端叢集,Kubernetes 控制層位址和閘道位址必須可從 GKE 叢集中的 Pod 存取。建議您使用 CloudVPN,將 GKE 叢集的子網路連線至地端部署叢集的網路。
- 如果使用 Istio CA,請為所有叢集使用相同的自訂根憑證。
事前準備
本指南假設您使用 asmcli
工具安裝 Cloud Service Mesh。您需要 asmcli
,以及 asmcli
下載至您在執行 asmcli install
時,於 --output_dir 中指定目錄的設定套件。詳情請參閱「安裝依附工具並驗證叢集」一文,瞭解如何:
您需要存取要在網格中設定的所有叢集的 kubeconfig 檔案。如要為 GKE 叢集建立新的 kubeconfig 檔案,請在終端機中匯出 KUBECONFIG
環境變數,並將檔案的完整路徑做為值,然後產生 kubeconfig 項目。
設定環境變數和預留位置
安裝東西向閘道時,您需要下列環境變數。
為網路名稱建立環境變數:
GKE 叢集預設會使用叢集網路名稱:
export NETWORK_1="PROJECT_ID-CLUSTER_NETWORK" ``````
其他叢集使用
default
:export NETWORK_2="default" ``````
如果您在其他叢集上安裝 Cloud Service Mesh,且 --network_id
的值不同,則應將相同的值傳遞至 NETWORK_2
。
安裝東西向閘道
在 CLUSTER_1 (您的 GKE 叢集) 中安裝閘道,專門用於前往 CLUSTER_2 (您的多雲或內部部署叢集) 的東西向流量:
asm/istio/expansion/gen-eastwest-gateway.sh \ --network ${NETWORK_1} \ --revision asm-1264-1 | \ ./istioctl --kubeconfig=PATH_TO_KUBECONFIG_1 install -y -f -
請注意,這個閘道預設會在網際網路上公開。為防範外部攻擊,生產系統可能需要額外的存取限制,例如防火牆規則。
在 CLUSTER_2 中安裝專為 CLUSTER_1 東西向流量使用的閘道。
asm/istio/expansion/gen-eastwest-gateway.sh \ --network ${NETWORK_2} \ --revision asm-1264-1 | \ ./istioctl --kubeconfig=PATH_TO_KUBECONFIG_2 install -y -f -
公開服務
由於叢集位於不同網路上,您需要在兩個叢集的東西向閘道上公開所有服務 (\*.local
)。雖然這個閘道在網際網路上公開,但只有具備可信任的 mTLS 憑證和工作負載 ID 的服務,才能存取閘道後方的服務,就像這些服務位於同一網路上一樣。
透過每個叢集的東西向閘道公開服務
kubectl --kubeconfig=PATH_TO_KUBECONFIG_1 apply -n istio-system -f \
asm/istio/expansion/expose-services.yaml
kubectl --kubeconfig=PATH_TO_KUBECONFIG_2 apply -n istio-system -f \
asm/istio/expansion/expose-services.yaml
啟用端點探索功能
執行 asmcli create-mesh
指令,啟用端點探索功能。這個範例只顯示兩個叢集,但您可以執行指令,在其他叢集上啟用端點探索功能,但須遵守 GKE Hub 服務限制。
./asmcli create-mesh \
FLEET_PROJECT_ID \
PATH_TO_KUBECONFIG_1 \
PATH_TO_KUBECONFIG_2
驗證多叢集連線
本節說明如何將範例 HelloWorld
和 Sleep
服務部署至多叢集環境,以驗證跨叢集負載平衡是否正常運作。
啟用 Sidecar 注入功能
找出修訂版本標籤值,後續步驟會用到。
使用下列指令找出修訂版本標籤,您會在後續步驟中使用該標籤。
kubectl -n istio-system get pods -l app=istiod --show-labels
輸出看起來類似以下內容:
NAME READY STATUS RESTARTS AGE LABELS istiod-asm-173-3-5788d57586-bljj4 1/1 Running 0 23h app=istiod,istio.io/rev=asm-173-3,istio=istiod,pod-template-hash=5788d57586 istiod-asm-173-3-5788d57586-vsklm 1/1 Running 1 23h app=istiod,istio.io/rev=asm-173-3,istio=istiod,pod-template-hash=5788d57586
在輸出內容的 LABELS
欄下方,記下 istiod
修訂版本標籤的值,該值位於 istio.io/rev=
前置字串後方。在本範例中,這個值為 asm-173-3
。在下一節的步驟中,請使用修訂版本值。
安裝 HelloWorld 服務
在每個叢集中建立範例命名空間和服務定義。在下列指令中,將 REVISION 替換為您在上一步記下的
istiod
修訂版本標籤。for CTX in ${CTX_1} ${CTX_2} do kubectl create --context=${CTX} namespace sample kubectl label --context=${CTX} namespace sample \ istio-injection- istio.io/rev=REVISION --overwrite done
其中 REVISION 是您先前記下的
istiod
修訂版本標籤。輸出內容會如下所示:
label "istio-injection" not found. namespace/sample labeled
您可以放心忽略
label "istio-injection" not found.
在兩個叢集中建立 HelloWorld 服務:
kubectl create --context=${CTX_1} \ -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \ -l service=helloworld -n sample
kubectl create --context=${CTX_2} \ -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \ -l service=helloworld -n sample
將 HelloWorld v1 和 v2 部署到每個叢集
將
HelloWorld v1
部署至CLUSTER_1
和v2
至CLUSTER_2
,這有助於稍後驗證跨叢集負載平衡:kubectl create --context=${CTX_1} \ -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \ -l version=v1 -n sample
kubectl create --context=${CTX_2} \ -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \ -l version=v2 -n sample
使用下列指令確認
HelloWorld v1
和v2
是否正在執行。確認輸出內容與顯示的內容類似:kubectl get pod --context=${CTX_1} -n sample
NAME READY STATUS RESTARTS AGE helloworld-v1-86f77cd7bd-cpxhv 2/2 Running 0 40s
kubectl get pod --context=${CTX_2} -n sample
NAME READY STATUS RESTARTS AGE helloworld-v2-758dd55874-6x4t8 2/2 Running 0 40s
部署 Sleep 服務
將
Sleep
服務部署至兩個叢集。這個 Pod 會產生人工網路流量,以供示範:for CTX in ${CTX_1} ${CTX_2} do kubectl apply --context=${CTX} \ -f ${SAMPLES_DIR}/samples/sleep/sleep.yaml -n sample done
等待每個叢集啟動
Sleep
服務。確認輸出內容與下圖類似:kubectl get pod --context=${CTX_1} -n sample -l app=sleep
NAME READY STATUS RESTARTS AGE sleep-754684654f-n6bzf 2/2 Running 0 5s
kubectl get pod --context=${CTX_2} -n sample -l app=sleep
NAME READY STATUS RESTARTS AGE sleep-754684654f-dzl9j 2/2 Running 0 5s
驗證跨叢集負載平衡
多次呼叫 HelloWorld
服務,並檢查輸出內容,確認 v1 和 v2 交替回覆:
呼叫
HelloWorld
服務:kubectl exec --context="${CTX_1}" -n sample -c sleep \ "$(kubectl get pod --context="${CTX_1}" -n sample -l \ app=sleep -o jsonpath='{.items[0].metadata.name}')" \ -- /bin/sh -c 'for i in $(seq 1 20); do curl -sS helloworld.sample:5000/hello; done'
輸出結果會與下列內容相似:
Hello version: v2, instance: helloworld-v2-758dd55874-6x4t8 Hello version: v1, instance: helloworld-v1-86f77cd7bd-cpxhv ...
再次呼叫
HelloWorld
服務:kubectl exec --context="${CTX_2}" -n sample -c sleep \ "$(kubectl get pod --context="${CTX_2}" -n sample -l \ app=sleep -o jsonpath='{.items[0].metadata.name}')" \ -- /bin/sh -c 'for i in $(seq 1 20); do curl -sS helloworld.sample:5000/hello; done'
輸出結果會與下列內容相似:
Hello version: v2, instance: helloworld-v2-758dd55874-6x4t8 Hello version: v1, instance: helloworld-v1-86f77cd7bd-cpxhv ...
恭喜!您已驗證負載平衡的多叢集 Cloud Service Mesh!
清除所用資源
完成負載平衡驗證後,請從叢集中移除 HelloWorld
和 Sleep
服務。
kubectl delete ns sample --context ${CTX_1} kubectl delete ns sample --context ${CTX_2}