從 Istio 1.11 以上版本遷移至 Cloud Service Mesh
本教學課程說明如何使用 Istio,將應用程式從 Google Kubernetes Engine (GKE) 叢集遷移至使用代管 Cloud Service Mesh 的新叢集。Cloud Service Mesh 是 Google 完全代管的 Istio 相容服務網格。
在本教學課程中,您將執行下列作業:
- 建立新的 Google Kubernetes Engine 叢集,並在叢集上安裝 Istio 和 Istio Ingress 閘道。這個叢集會做為您要遷移的現有叢集。
- 將 Online Boutique 範例應用程式部署至叢集,並搭配 Istio 使用。
- 在同一個 Google Cloud 專案中,建立另一個 Google Kubernetes Engine 叢集。
- 在第二個叢集上啟用代管 Cloud Service Mesh,並部署 Cloud Service Mesh Ingress 閘道。
- 將 Online Boutique 部署到已啟用 Cloud Service Mesh 的叢集,複製已啟用 Istio 的叢集部署作業。
- 在具有 Istio 的叢集上使用 Istio 的流量分割功能,將 50% 的使用者流量從具有 Istio 的叢集轉移至具有 Cloud Service Mesh 的叢集。
- 將叢集的網域名稱系統 (DNS) 項目從 Istio 指向 Cloud Service Mesh 叢集,完成從 Istio 遷移至 Cloud Service Mesh 的作業。
初期測試部署
「金絲雀部署」是軟體開發技術,用於在向所有使用者發布新版軟體前,先測試新版軟體。這項技術會逐步提高傳送至新版本的流量百分比。在本教學課程中,您將設定採用代管 Cloud Service Mesh 的新叢集,並逐步將使用者流量轉移至該叢集。首先,請將 0% 的使用者流量導向新叢集,然後是 50%,最後是 100%。在實際工作環境中,您應使用較小且較多的增量。如果發現新叢集無法處理一定比例的流量,可以將百分比調降至 0% 來復原。
Canary 控制層與 Canary 叢集
從 Istio 遷移至代管 Cloud Service Mesh 時,有兩種常用策略:
- Canary 控制層遷移:採用這項策略時,您會在安裝 Istio 的同一叢集上,佈建代管 Cloud Service Mesh。
- Canary 叢集遷移:採用這項策略時,您會建立新叢集,然後在該叢集上佈建代管 Cloud Service Mesh。
在本教學課程中,您將逐步瞭解 Canary 叢集遷移策略。
費用
本教學課程使用下列 Google Cloud的計費元件:
完成本教學課程後,您可以刪除已建立的資源,避免持續產生費用。詳情請參閱「清除」。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the required APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the required APIs.
將
PROJECT_ID
替換為您的專案 ID,然後建立新的叢集:gcloud container clusters create cluster-with-istio \ --project=PROJECT_ID \ --zone=us-central1-a \ --machine-type=e2-standard-2 --num-nodes=3
重新命名叢集環境,方便使用叢集:
kubectl config rename-context \ gke_PROJECT_ID_us-central1-a_cluster-with-istio \ cluster-with-istio
確認叢集環境定義已重新命名:
kubectl config get-contexts --output="name"
在叢集上安裝 Istio。為求簡便,您將安裝 Istio 的預設設定檔,以及與
istioctl
安裝版本相符的版本。istioctl install
系統會要求你輸入「y」,然後按下 Enter 鍵。
輸出內容類似如下:
This will install the Istio X.Y.Z default profile with ["Istio core" "Istiod" "Ingress gateways"] components into the cluster. Proceed? (y/N) ✔ Istio core installed ✔ Istiod installed ✔ Ingress gateways installed ✔ Installation complete Making this installation the default for injection and validation.
您將 Online Boutique 部署至名為
onlineboutique
的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-istio \ create namespace onlineboutique
部署 Online Boutique 的 12 項服務,包括模擬使用者流量的負載產生器:
kubectl \ --namespace=onlineboutique \ --context=cluster-with-istio \ apply -f microservices-demo/release/kubernetes-manifests.yaml
上一個步驟也部署了名為
frontend-external
的服務 (類型為LoadBalancer
),並指派了外部 IP 位址。不過,您只想透過 Istio 輸入閘道部署允許公開輸入。刪除frontend-external
服務資源:kubectl \ --namespace=onlineboutique \ --context=cluster-with-istio \ delete service frontend-external
部署 Istio
Gateway
資源和 IstioVirtualService
資源,讓公開流量存取 Online Boutique:kubectl \ --namespace=onlineboutique \ --context=cluster-with-istio \ apply -f microservices-demo/istio-manifests/frontend-gateway.yaml
取得 Istio 輸入閘道的外部 IP 位址:
kubectl \ --namespace istio-system \ --context=cluster-with-istio \ get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'
複製
istio-ingressgateway
服務的外部 IP 位址,然後透過網路瀏覽器存取。您會看到 Online Boutique 範例應用程式。建立新叢集:
gcloud container clusters create cluster-with-csm \ --project=PROJECT_ID --zone=us-central1-a \ --machine-type=e2-standard-4 --num-nodes=2 \ --workload-pool PROJECT_ID.svc.id.goog
重新命名叢集環境,方便使用叢集:
kubectl config rename-context \ gke_PROJECT_ID_us-central1-a_cluster-with-csm \ cluster-with-csm
確認叢集環境定義已重新命名:
kubectl config get-contexts --output="name"
在專案的車隊中啟用 Cloud Service Mesh。機群能有條理地將資源分組,包括 Kubernetes 叢集和可以一同管理的其他資源。
gcloud container fleet mesh enable --project PROJECT_ID
輸出內容類似如下:
Waiting for Feature Service Mesh to be created...done.
向專案的機群註冊叢集:
gcloud container fleet memberships register cluster-with-csm-membership \ --gke-cluster=us-central1-a/cluster-with-csm \ --enable-workload-identity \ --project PROJECT_ID
輸出內容類似如下:
Waiting for membership to be created...done. Finished registering to the Fleet.
在叢集上啟用代管型 Cloud Service Mesh:
gcloud container fleet mesh update \ --management automatic \ --memberships cluster-with-csm-membership \ --project PROJECT_ID
輸出內容類似如下:
Waiting for Feature Service Mesh to be updated...done.
確認叢集已佈建代管 Cloud Service Mesh,且已可供使用:
gcloud container fleet mesh describe --project PROJECT_ID
Cloud Service Mesh 大約需要 10 分鐘才能完成佈建,並在叢集上啟用。如果看到
controlPlaneManagement.state: DISABLED
或controlPlaneManagement.state: PROVISIONING
,請每隔幾分鐘重新執行上一個指令,直到看到controlPlaneManagement.state: ACTIVE
為止。輸出內容類似如下:
createTime: '2022-07-06T01:05:39.110120474Z' membershipSpecs: projects/123456789123/locations/global/memberships/cluster-with-csm-membership: mesh: management: MANAGEMENT_AUTOMATIC membershipStates: projects/123456789123/locations/global/memberships/cluster-with-csm-membership: servicemesh: controlPlaneManagement: details: - code: REVISION_READY details: 'Ready: asm-managed' state: ACTIVE dataPlaneManagement: details: - code: OK details: Service is running. state: ACTIVE state: code: OK description: 'Revision(s) ready for use: asm-managed.' updateTime: '2022-07-06T01:19:24.243993678Z' name: projects/your-project-id/locations/global/features/servicemesh resourceState: state: ACTIVE spec: {} state: state: {} updateTime: '2022-07-06T01:19:27.475885687Z'
您會將 Cloud Service Mesh 的 Ingress 閘道部署至名為
asm-ingress
的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-csm \ create namespace asm-ingress
使用
istio.io/rev=asm-managed
標籤將asm-ingress
命名空間新增至服務網格,並啟用自動補充資訊代理程式植入功能。kubectl \ --context cluster-with-csm \ label namespace asm-ingress 'istio.io/rev=asm-managed'
部署 Cloud Service Mesh Ingress 閘道:
kubectl \ --context cluster-with-csm \ --namespace=asm-ingress \ apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/asm-gateway-deployment-svc.yaml kubectl \ --context cluster-with-csm \ --namespace=asm-ingress \ apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/gateway.yaml
輸出內容類似如下:
namespace/asm-ingress configured serviceaccount/asm-ingressgateway configured service/asm-ingressgateway configured deployment.apps/asm-ingressgateway configured gateway.networking.istio.io/asm-ingressgateway configured
您將 Online Boutique 部署至名為
onlineboutique
的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-csm \ create namespace onlineboutique
使用
istio.io/rev=asm-managed
標籤將onlineboutique
命名空間新增至服務網格,並啟用自動補充資訊代理程式植入功能。kubectl \ --context cluster-with-csm \ label namespace onlineboutique 'istio.io/rev=asm-managed'
部署 Online Boutique 的 12 項服務,包括模擬使用者流量的負載產生器:
kubectl \ --context cluster-with-csm \ --namespace=onlineboutique \ apply -f anthos-service-mesh-samples/docs/shared/online-boutique/kubernetes-manifests.yaml kubectl \ --context cluster-with-csm \ --namespace=onlineboutique \ apply -f anthos-service-mesh-samples/docs/shared/online-boutique/virtual-service.yaml
取得 Cloud Service Mesh 輸入閘道的外部 IP 位址:
kubectl \ --context cluster-with-csm \ --namespace asm-ingress \ get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'
複製
asm-ingressgateway
Service 的外部 IP 位址,然後透過網路瀏覽器存取。您會看到 Online Boutique 範例應用程式。下一節會用到外部 IP 位址,因此請將該位址複製到環境變數中:export INGRESS_IP_OF_CLUSTER_WITH_MANAGED_ASM=$( \ kubectl \ --context cluster-with-csm \ --namespace asm-ingress \ get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}' \ )
- ServiceEntry,向 Istio 說明受管理 Cloud Service Mesh 叢集的 Online Boutique 端點
- VirtualService,告知 Istio 輸入閘道將流量平均分配。
在
ServiceEntry
資源中,設定受管理 Cloud Service Mesh 叢集 Ingress 閘道的 IP 位址:sed -i "s/1.2.3.4/${INGRESS_IP_OF_CLUSTER_WITH_MANAGED_ASM}/" anthos-service-mesh-samples/docs/migrate-to-managed-asm/service-entry.yaml
將
ServiceEntry
部署至叢集 (使用 Istio):kubectl \ --context cluster-with-istio \ --namespace onlineboutique \ apply -f anthos-service-mesh-samples/docs/migrate-to-managed-asm/service-entry.yaml
將
VirtualService
部署至叢集 (使用 Istio):kubectl \ --context cluster-with-istio \ --namespace onlineboutique \ apply -f anthos-service-mesh-samples/docs/migrate-to-managed-asm/virtual-service.yaml
在網路瀏覽器中,造訪叢集 Ingress 閘道的 IP 位址 (使用 Istio):
kubectl \ --context cluster-with-istio \ --namespace istio-system \ get service
多次重新整理 Online Boutique 首頁,並每次都檢查頁面頁尾。請注意,50% 的要求是由叢集上的 Pod 處理,且該叢集已啟用代管 Cloud Service Mesh。
在 DNS 設定中新增 A 記錄,將網域名稱 (例如 example.com) 指向叢集上執行 Istio 的 Ingress Gateway IP 位址。
在網路瀏覽器中前往網域名稱,即可存取 Online Boutique。
盡量縮短 DNS 記錄存留時間 (TTL),確保您在需要回溯時,可以快速還原 DNS 項目。
將網域名稱的 A 記錄設為叢集 Ingress 閘道的外部 IP 位址,並使用代管式 Cloud Service Mesh。
遷移作業成功後,請使用 Istio 刪除叢集:
gcloud container clusters delete cluster-with-istio \ --zone=us-central1-a \ --project=PROJECT_ID
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
啟動 Cloud Shell
在本教學課程中,您將使用 Cloud Shell,這是託管於 Google Cloud 的殼層環境,可讓您管理Google Cloud 資源。
Cloud Shell 已預先安裝 Google Cloud CLI、kubectl 和 istioctl 指令列工具。gcloud CLI 是 Google Cloud的主要 CLI。
從本頁面右上角開啟 Cloud Shell 工作階段,按一下 terminal,然後按一下「確認」。此時 Cloud Shell 工作階段會在頁面底部的頁框中開啟,在該 Cloud Shell 工作階段中完成下列指令。
下載程式碼範例
複製包含您將使用的 Kubernetes 和 Istio 資源的 git 存放區:
git clone https://github.com/GoogleCloudPlatform/anthos-service-mesh-samples.git
git clone https://github.com/GoogleCloudPlatform/microservices-demo.git
使用 Istio 設定叢集
建立叢集並安裝 Istio
在本節中,您將建立使用 Istio 的叢集。在實務上,這會是您已使用的叢集。
部署 Online Boutique
設定已啟用代管 Cloud Service Mesh 的新叢集
建立叢集並佈建代管 Cloud Service Mesh
在本節中,您將建立要遷移的叢集。您將佈建代管 Cloud Service Mesh,並部署 Online Boutique,以便從使用 Istio 的叢集複製部署作業。
部署 Cloud Service Mesh 的 Ingress 閘道
部署 Online Boutique
使用 Canary 部署作業測試叢集與 Cloud Service Mesh
在本節中,您將設定叢集和 Istio,將 50% 的 Online Boutique 使用者流量轉移至叢集上 Online Boutique 的執行個體,並使用代管 Cloud Service Mesh。如要達成這個目標,請將兩個 Istio 資源部署至叢集 (使用 Istio):
遷移至已啟用代管 Cloud Service Mesh 的叢集
本節假設您擁有網域名稱,且有權存取網域名稱伺服器 (DNS) 設定。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
刪除資源
刪除已啟用代管 Cloud Service Mesh 的叢集:
gcloud container clusters delete cluster-with-managed-asm \
--zone=us-central1-a \
--project=PROJECT_ID