將新叢集上的叢集內控制層遷移至代管控制層
本教學課程說明如何使用叢集內 Cloud Service Mesh,將應用程式從 Google Kubernetes Engine (GKE) 叢集遷移至使用代管 Cloud Service Mesh 的新叢集。代管 Cloud Service Mesh 是 Google 全代管且符合 Istio 標準的服務網格。
在本教學課程中,您將執行下列作業:
- 建立新的 Google Kubernetes Engine 叢集,並在叢集上安裝叢集內 Cloud Service Mesh 和 Cloud Service Mesh Ingress 閘道。這個叢集會做為您要遷移的現有叢集。
- 使用叢集內 Cloud Service Mesh,將 Online Boutique 範例應用程式部署至叢集。
- 在同一個 Google Cloud 專案中,建立另一個 Google Kubernetes Engine 叢集。
- 在第二個叢集上佈建代管 Cloud Service Mesh,並部署 Cloud Service Mesh Ingress 閘道。
- 將 Online Boutique 部署到已啟用代管 Cloud Service Mesh 的叢集,藉此複製已啟用叢集內 Cloud Service Mesh 的叢集部署作業。
- 使用叢集內 Cloud Service Mesh 叢集上的 Istio 流量分割功能,將 50% 的使用者流量從叢集內 Cloud Service Mesh 叢集轉移至代管 Cloud Service Mesh 叢集。
- 將叢集的網域名稱系統 (DNS) 項目指向代管 Cloud Service Mesh 叢集,完成從叢集內 Cloud Service Mesh 遷移至代管 Cloud Service Mesh 的作業。
初期測試部署
「初期測試部署」是軟體開發技術,用於在向所有使用者發布新版軟體前,先測試新版軟體。這項技術會逐步提高傳送至新版本的流量百分比。在本教學課程中,您將設定採用代管 Cloud Service Mesh 的新叢集,並逐步將使用者流量轉移至該叢集。首先,請將 0% 的使用者流量導向新叢集,然後是 50%,最後是 100%。在實際工作環境中,您應使用較小且較多的增量。如果發現新叢集無法處理一定比例的流量,可以將百分比調降至 0%,藉此還原。
Canary 控制層與 Canary 叢集
從叢內 Cloud Service Mesh 遷移至代管 Cloud Service Mesh 時,有兩種常用策略:
- Canary 叢集遷移:採用這項策略時,您會建立新叢集,然後在該叢集上佈建代管 Cloud Service Mesh。
- Canary 控制層遷移:採用這項策略時,您會在安裝叢內 Cloud Service Mesh 的同一叢集上,佈建代管 Cloud Service Mesh。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
設定叢集內 Cloud Service Mesh 的叢集
建立叢集並安裝叢集內 Cloud Service Mesh
在本節中,您將建立使用叢內 Cloud Service Mesh 的叢集。在實務上,這會是您已使用的叢集。
將
PROJECT_ID替換為您的專案 ID,然後建立新叢集:gcloud container clusters create cluster-with-in-cluster-asm \ --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-in-cluster-asm \ cluster-with-in-cluster-asm確認叢集環境定義已重新命名:
kubectl config get-contexts --output="name"下載可將 Cloud Service Mesh 安裝至目前工作目錄的版本: 1.23.6
curl https://storage.googleapis.com/csm-artifacts/asm/asmcli_1.23 > asmcli系統會要求你輸入「y」,然後按下 Enter 鍵。
輸出內容類似如下:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 167k 100 167k 0 0 701k 0 --:--:-- --:--:-- --:--:-- 701k將
asmcli指令碼設為可執行:chmod +x asmcli使用
asmcli安裝叢集內 Cloud Service Mesh:./asmcli install \ --project_id PROJECT_ID \ --cluster_name cluster-with-in-cluster-asm \ --cluster_location us-central1-a \ --output_dir . \ --enable_all \ --ca mesh_caasmcli工具可能需要幾分鐘才能完成作業。這項工具會輸出資訊訊息,方便您追蹤進度。成功後,輸出內容會類似如下:
... asmcli: Successfully installed ASM.
部署 Cloud Service Mesh 的 Ingress 閘道
您將 Cloud Service Mesh 的 Ingress 閘道部署至名為
asm-ingress的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-in-cluster-asm \ create namespace asm-ingress使用
istio.io/rev=asm-1236-11標籤將asm-ingress命名空間新增至服務網格,並啟用自動 Sidecar Proxy 植入功能。kubectl \ --context cluster-with-in-cluster-asm \ label --overwrite namespace asm-ingress istio.io/rev=asm-1236-11輸出內容類似如下:
即可查看namespace/asm-ingress labeledistiod-asm-*服務名稱後方附加的版本。部署 Cloud Service Mesh Ingress 閘道:
kubectl \ --context cluster-with-in-cluster-asm \ --namespace=asm-ingress \ apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/asm-gateway-deployment-svc.yaml kubectl \ --context cluster-with-in-cluster-asm \ --namespace=asm-ingress \ apply -f anthos-service-mesh-samples/docs/shared/asm-ingress-gateway/gateway.yaml輸出內容類似如下:
serviceaccount/asm-ingressgateway created service/asm-ingressgateway created deployment.apps/asm-ingressgateway created gateway.networking.istio.io/asm-ingressgateway created
部署 Online Boutique
您將 Online Boutique 部署至名為
onlineboutique的獨立命名空間。建立命名空間:kubectl \ --context cluster-with-in-cluster-asm \ create namespace onlineboutique使用
istio.io/rev=asm-1236-11標籤將onlineboutique命名空間新增至服務網格,並啟用自動 Sidecar Proxy 植入功能。kubectl \ --context cluster-with-in-cluster-asm \ label --overwrite namespace onlineboutique istio.io/rev=asm-1236-11輸出內容類似如下:
namespace/onlineboutique labeled部署 Online Boutique 的 12 項服務,包括模擬使用者流量的負載產生器:
kubectl \ --context cluster-with-in-cluster-asm \ --namespace=onlineboutique \ apply -f anthos-service-mesh-samples/docs/shared/online-boutique/kubernetes-manifests.yaml kubectl \ --context cluster-with-in-cluster-asm \ --namespace=onlineboutique \ apply -f anthos-service-mesh-samples/docs/shared/online-boutique/virtual-service.yaml取得 Cloud Service Mesh Ingress 閘道的外部 IP 位址:
kubectl \ --context cluster-with-in-cluster-asm \ --namespace asm-ingress \ get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}'複製 Ingress 閘道的外部 IP 位址,然後透過網路瀏覽器存取。您會看到 Online Boutique 範例應用程式。
使用代管 Cloud Service Mesh 設定新叢集
建立叢集並佈建代管 Cloud Service Mesh
在本節中,您將建立要遷移的叢集。您將佈建代管 Cloud Service Mesh,並部署 Online Boutique,以便從使用叢內 Cloud Service Mesh 的叢集複製部署作業。
建立新叢集:
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 測試叢集
在本節中,您將使用叢集內 Cloud Service Mesh 設定叢集,將 50% 的 Online Boutique 使用者流量轉移至叢集上採用代管 Cloud Service Mesh 的 Online Boutique 執行個體。如要達成這個目標,請將兩個 Istio 資源部署至叢集,並使用叢集內 Cloud Service Mesh:
- ServiceEntry,向叢集內 Cloud Service Mesh 說明受管理 Cloud Service Mesh 叢集的 Online Boutique 端點
- VirtualService,告知叢集內 Cloud Service Mesh Ingress 閘道將流量平均分配。
在
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部署至叢集,並使用叢集內 Cloud Service Mesh:kubectl \ --context cluster-with-in-cluster-asm \ --namespace onlineboutique \ apply -f anthos-service-mesh-samples/docs/migrate-to-managed-asm/service-entry.yaml將
VirtualService部署至叢集,並使用叢集內 Cloud Service Mesh:kubectl \ --context cluster-with-in-cluster-asm \ --namespace onlineboutique \ apply -f anthos-service-mesh-samples/docs/migrate-to-managed-asm/virtual-service-in-cluster-asm.yaml在網路瀏覽器中,前往叢集 Ingress 閘道的 IP 位址 (位於叢集內 Cloud Service Mesh 中):
kubectl \ --context cluster-with-in-cluster-asm \ --namespace asm-ingress \ get service多次重新整理 Online Boutique 首頁,並每次都檢查頁面頁尾。請注意,50% 的要求是由叢集上的 Pod 處理,並透過代管 Cloud Service Mesh 進行管理。
遷移至已啟用代管 Cloud Service Mesh 的叢集
本節假設您擁有網域名稱,且有權存取網域名稱伺服器 (DNS) 設定。
在 DNS 設定中新增 A 記錄,將網域名稱 (例如 example.com) 指向叢集上執行的 Ingress 閘道 IP 位址,並使用叢集內 Cloud Service Mesh。
在網路瀏覽器中前往網域名稱,即可存取 Online Boutique。
盡量縮短 DNS 記錄存留時間 (TTL),確保您在需要復原時,可以快速還原 DNS 項目。
將網域名稱的 A 記錄設為叢集 Ingress 閘道的外部 IP 位址,並使用代管式 Cloud Service Mesh。
遷移成功後,請刪除叢集內 Cloud Service Mesh:
gcloud container clusters delete cluster-with-in-cluster-asm \ --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