本指南說明如何在 Google Kubernetes Engine (GKE) Autopilot 叢集上設定代管的 Cloud Service Mesh。Cloud Service Mesh 是以 Istio 為基礎的全代管服務網格。
本教學課程說明如何設定可在單一 GKE Autopilot 叢集上執行的正式版服務網格,並使用預設設定。設計環境時,建議您一併參閱完整的 Cloud Service Mesh 佈建指南。
使用 GKE Autopilot 執行代管 Cloud Service Mesh 的優點
在 Autopilot 模式下使用 GKE 時,Google 會自動設定及管理叢集。Autopilot 模式可簡化叢集作業體驗,讓您專心處理應用程式。同樣地,您只需幾個步驟,就能佈建全代管的受管理 Cloud Service Mesh。
- 您可以使用 Fleet API 佈建代管 Cloud Service Mesh,不需要
istioctl等用戶端工具。 - Cloud Service Mesh 會自動將補充 Proxy 插入工作負載,不需要授予容器更高的權限。
- 您不必進行任何額外設定,即可查看網格和服務的豐富資訊主頁,然後使用這些指標設定服務等級目標 (SLO) 和快訊,監控應用程式的健康狀態
- 系統會自動升級代管 Cloud Service Mesh 控制層,確保您取得最新的安全性修補程式和功能
- Cloud Service Mesh 代管資料平面會自動升級工作負載中的 Sidecar Proxy,因此當 Proxy 升級和安全性修補程式可用時,您不需要自行重新啟動服務
- Cloud Service Mesh 是受支援的產品,可使用標準開放原始碼 Istio API 進行設定。請參閱「支援的功能」。
目標
- 建立 GKE Autopilot 叢集
- 使用 Fleet API 佈建代管 Cloud Service Mesh
- 將網格 Ingress 閘道部署至專屬命名空間
- 部署範例應用程式
- 設定 Cloud Service Mesh,對服務間的通訊強制執行嚴格的相互傳輸層安全性 (mTLS) 驗證
- 查看 Cloud Service Mesh 資訊主頁,並確認服務透過 mTLS 連線
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
Cloud Shell 已預先安裝本教學課程所需的軟體,包括 kubectl、gcloud CLI、Helm 和 Terraform。如果您未使用 Cloud Shell,則必須安裝 gcloud CLI。
- 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.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/container.admin, roles/gkehub.admin, roles/serviceusage.serviceUsageAdmingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
設定環境變數:
PROJECT_ID=PROJECT_ID gcloud config set project ${PROJECT_ID}啟用 Mesh API:
gcloud services enable mesh.googleapis.com啟用 mesh.googleapis.com 會啟用下列 API:
API 目的 可停用 meshconfig.googleapis.comCloud Service Mesh 會使用 Mesh Configuration API,將網格中的設定資料轉送至 Google Cloud。此外,啟用 Mesh Configuration API 後,您就能在 Google Cloud 控制台中存取 Cloud Service Mesh 頁面,並使用 Cloud Service Mesh 憑證授權單位。 否 meshca.googleapis.com與代管 Cloud Service Mesh 使用的 Cloud Service Mesh 憑證授權單位相關。 否 container.googleapis.com建立 Google Kubernetes Engine (GKE) 叢集時必須啟用。 否 gkehub.googleapis.com如要以機群形式管理網格,必須提供這項資訊。 否 monitoring.googleapis.com擷取網格工作負載的遙測資料時,必須使用這項服務。 否 stackdriver.googleapis.com使用服務 UI 時必須提供。 否 opsconfigmonitoring.googleapis.com必須使用 Services UI 才能關閉叢集。Google Cloud 否 connectgateway.googleapis.com這是必要步驟,代管 Cloud Service Mesh 控制層才能存取網格工作負載。 是* trafficdirector.googleapis.com啟用高可用性且可擴充的代管控制層。 是* networkservices.googleapis.com啟用高可用性且可擴充的代管控制層。 是* networksecurity.googleapis.com啟用高可用性且可擴充的代管控制層。 是* 建立叢集,註冊為機群成員:
gcloud container clusters create-auto asm-cluster \ --location="us-central1" \ --enable-fleet確認叢集已向機群註冊:
gcloud container fleet memberships list輸出結果會與下列內容相似:
NAME: asm-cluster EXTERNAL_ID: LOCATION: us-central1記下成員名稱,因為您需要這個名稱來設定 Cloud Service Mesh。
在專案中啟用 Cloud Service Mesh 艦隊功能:
gcloud container fleet mesh enable啟用網狀網路的自動管理功能:
gcloud container fleet mesh update \ --management=automatic \ --memberships=MEMBERSHIP_NAME \ --location=us-central1將
MEMBERSHIP_NAME替換為驗證叢集是否已註冊至車隊時列出的成員名稱。下載憑證,以便存取叢集:
gcloud container clusters get-credentials asm-cluster --location=us-central1為閘道部署作業建立命名空間:
kubectl create namespace bank-gateways在命名空間中新增標籤,讓 Cloud Service Mesh 控制層自動將閘道設定注入部署作業。
kubectl label namespace bank-gateways istio-injection=enabled將輸入閘道部署至命名空間:
Helm
helm repo add istio https://istio-release.storage.googleapis.com/charts helm repo update helm install --wait --namespace bank-gateways \ --set resources.requests.cpu=250m \ --set resources.requests.memory=512Mi \ --set resources.requests.ephemeral-storage=1Gi \ --set resources.limits.cpu=250m \ --set resources.limits.memory=512Mi \ --set resources.limits.ephemeral-storage=1Gi \ istio-ingressgateway istio/gatewaykubectl
git clone https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages kubectl apply -n bank-gateways \ -f ./anthos-service-mesh-packages/samples/gateways/istio-ingressgateway kubectl -n bank-gateways wait "deployment/istio-ingressgateway" \ --for=condition=available --timeout=240s部署至正式環境時,請務必設定足夠的資源要求。GKE Autopilot 只會考量
requests中設定的資源值,而不會考量limits。Istio 專案會發布效能和可擴充性相關資訊。為部署作業建立 Kubernetes 命名空間:
kubectl create namespace bank-sample為命名空間新增標籤,讓 Cloud Service Mesh 自動將 Sidecar Proxy 注入範例 Pod:
kubectl label namespace bank-sample istio-injection=enabled部署範例應用程式:
git clone https://github.com/GoogleCloudPlatform/bank-of-anthos.git kubectl apply -n bank-sample -f bank-of-anthos/extras/jwt/jwt-secret.yaml kubectl apply -n bank-sample -f bank-of-anthos/kubernetes-manifests/等待應用程式準備就緒。這項作業需要幾分鐘才能完成。
watch kubectl -n bank-sample get pods應用程式就緒時,輸出內容會類似於下列內容:
NAME READY STATUS RESTARTS AGE accounts-db-0 2/2 Running 0 2m16s balancereader-5c695f78f5-x4wlz 2/2 Running 0 3m8s contacts-557fc79c5-5d7fg 2/2 Running 0 3m7s frontend-7dd589c5d7-b4cgq 2/2 Running 0 3m7s ledger-db-0 2/2 Running 0 3m6s ledgerwriter-6497f5cf9b-25c6x 2/2 Running 0 3m5s loadgenerator-57f6896fd6-lx5df 2/2 Running 0 3m5s transactionhistory-6c498965f-tl2sk 2/2 Running 0 3m4s userservice-95f44b65b-mlk2p 2/2 Running 0 3m4s建立 Istio
Gateway和VirtualService資源,公開輸入閘道後方的應用程式:kubectl apply -n bank-sample -f bank-of-anthos/extras/istio/frontend-ingress.yaml取得範例應用程式的連結:
INGRESS_HOST=$(kubectl -n bank-gateways get service istio-ingressgateway \ -o jsonpath='{.status.loadBalancer.ingress[0].ip}') echo "http://$INGRESS_HOST"在瀏覽器中開啟連結,即可開啟範例應用程式。使用預設使用者名稱和密碼登入,即可查看應用程式。
將下列資訊清單儲存為
mesh-peer-authn.yaml:apiVersion: "security.istio.io/v1beta1" kind: "PeerAuthentication" metadata: name: "default" namespace: "istio-system" spec: mtls: mode: STRICT將資訊清單套用至叢集:
kubectl apply -f mesh-peer-authn.yaml在 Google Cloud 控制台中,前往「Cloud Service Mesh」,查看網格的資訊主頁:
從選單列的下拉式清單中選取專案。
您會看到總覽表格,其中列出網格中的所有微服務,以及微服務間連線的圖形化視覺化內容。下表會顯示每個微服務的三個 SRE 「黃金信號」:
- 流量 - 每秒要求數
- 錯誤率 - 百分比
- 延遲時間 - 毫秒
這些指標是根據微服務處理的實際流量計算。部署為範例應用程式一部分的
loadgenerator用戶端,會自動將持續測試流量傳送至frontend服務。Cloud Service Mesh 會自動將指標、記錄和 (選用) 追蹤記錄傳送至 Google Cloud Observability。按一下表格中的
frontend服務,即可查看服務總覽資訊主頁。您會看到服務的其他指標,以及傳入和輸出連線的視覺化資料。您也可以建立服務等級目標 (SLO),監控服務並發出快訊。刪除範例應用程式和閘道:
kubectl delete namespace bank-sample kubectl delete namespace bank-gateways刪除 GKE 叢集:
gcloud container clusters delete --location us-central1 asm-cluster --quiet
設定環境
您可以使用 gcloud CLI 或 Terraform 設定環境。
gcloud
Terraform
gcloud config set project PROJECT_ID
GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project)
export GOOGLE_CLOUD_PROJECT
建立 GKE 叢集
在 Autopilot 模式中建立 GKE 叢集。
gcloud
Terraform
如要建立 GKE 叢集,可以使用 google_container_cluster 資源。設定 fleet 區塊,讓叢集在建立時新增至機群。
如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
佈建代管 Cloud Service Mesh
您可以使用叢集機群成員資格的 servicemesh 功能,佈建代管 Cloud Service Mesh。
gcloud
Terraform
如要啟用網格 API,可以使用 google_project_service 資源。
您可以使用 google_gke_hub_feature 和 google_gke_hub_feature_membership 資源,在叢集上設定代管 Cloud Service Mesh。
如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。
確認控制層處於有效狀態
等待 controlPlaneManagement.state ACTIVE。這項作業最多可能需要 15 分鐘才能完成。
watch -n 30 gcloud container fleet mesh describe
輸出內容類似如下:
membershipSpecs:
projects/746296320118/locations/us-central1/memberships/asm-cluster:
mesh:
management: MANAGEMENT_AUTOMATIC
membershipStates:
projects/746296320118/locations/us-central1/memberships/asm-cluster:
servicemesh:
controlPlaneManagement:
details:
- code: REVISION_READY
details: 'Ready: asm-managed'
state: ACTIVE
dataPlaneManagement:
details:
- code: PROVISIONING
details: Service is provisioning.
state: PROVISIONING
state:
code: OK
description: 'Revision(s) ready for use: asm-managed.'
在您部署 Ingress 閘道之前,dataPlaneManagement 區段會維持 PROVISIONING 狀態,因為 Autopilot 叢集不會佈建任何節點,直到您部署工作負載為止。
部署網格 Ingress 閘道
在本節中,您將部署網格 Ingress 閘道,處理範例應用程式的傳入流量。Ingress 閘道是網格邊緣運作的負載平衡器,可接收傳入或傳出的 HTTP/TCP 連線。
您要將閘道部署至專屬命名空間,並為部署作業加上標籤,確保閘道能由 Cloud Service Mesh 控制層安全管理及自動升級。
部署範例應用程式
強制執行雙向傳輸層安全標準 (TLS)
確認已啟用 STRICT 相互傳輸層安全性 (mTLS) 模式。在 istio-system 命名空間中,為網格套用預設 PeerAuthentication 政策。
如要覆寫這項設定,請在特定命名空間中建立 PeerAuthentication 資源。
探索 Cloud Service Mesh 資訊主頁
確認已啟用 mTLS
按一下面板中的安全性連結,即可查看 frontend 服務的安全性總覽。表格和視覺化內容會顯示微服務之間傳入和輸出連線的綠色鎖頭圖示。這個圖示表示連線使用 mTLS 進行驗證和加密。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
如要避免系統向您的 Google Cloud 帳戶收取這個教學課程所用資源的費用,請執行下列動作:
刪除專案
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
刪除個別資源
如果您使用現有專案,但不想刪除專案,請刪除個別資源。
gcloud
Terraform
刪除您使用 Terraform 建立的資源:
terraform destroy