從 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計費元件:
完成本教學課程後,您可以刪除建立的資源,避免持續產生費用。詳情請參閱「清除所用資源」一節。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the required APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the required APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
啟動 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 的叢集。在實務上,這會是您已使用的叢集。
將
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
您將 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 範例應用程式。
使用代管 Cloud Service Mesh 設定新叢集
建立叢集並佈建代管 Cloud Service Mesh
在本節中,您將建立要遷移的叢集。您將佈建代管 Cloud Service Mesh,並部署 Online Boutique,以便從使用 Istio 的叢集複製部署作業。
建立新叢集:
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_IDCloud 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 閘道
您將 Cloud Service Mesh 的 Ingress 閘道部署至名為
asm-ingress的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-csm \ create namespace asm-ingress使用
istio.io/rev=asm-managed標籤將asm-ingress命名空間新增至服務網格,並啟用自動 Sidecar Proxy 植入功能。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
您將 Online Boutique 部署至名為
onlineboutique的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-csm \ create namespace onlineboutique使用
istio.io/rev=asm-managed標籤將onlineboutique命名空間新增至服務網格,並啟用自動 Sidecar Proxy 植入功能。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 Ingress 閘道的外部 IP 位址:
kubectl \ --context cluster-with-csm \ --namespace asm-ingress \ get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'複製
asm-ingressgatewayService 的外部 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}' \ )
使用初期測試部署作業,透過 Cloud Service Mesh 測試叢集
在本節中,您將使用 Istio 設定叢集,將 50% 的 Online Boutique 使用者流量轉移至叢集上 Online Boutique 的執行個體,並使用代管 Cloud Service Mesh。如要達成這個目標,請將兩個 Istio 資源部署至叢集 (使用 Istio):
- 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 進行管理。
遷移至已啟用代管 Cloud Service Mesh 的叢集
本節假設您擁有網域名稱,且有權存取網域名稱伺服器 (DNS) 設定。
在 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
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除資源
刪除已啟用代管 Cloud Service Mesh 的叢集:
gcloud container clusters delete cluster-with-managed-asm \
--zone=us-central1-a \
--project=PROJECT_ID