在 GKE Autopilot 叢集上佈建 Cloud Service Mesh


本指南說明如何在 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

初次使用 Google Cloud 的使用者可能符合免費試用資格。

完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

事前準備

Cloud Shell 已預先安裝本教學課程所需的軟體,包括 kubectlgcloud CLIHelmTerraform。如果您未使用 Cloud Shell,則必須安裝 gcloud CLI。

  1. 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.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Install the Google Cloud CLI.

  8. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  11. Make sure that billing is enabled for your Google Cloud project.

  12. 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.serviceUsageAdmin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.

設定環境

您可以使用 gcloud CLI 或 Terraform 設定環境。

gcloud

  1. 設定環境變數:

    PROJECT_ID=PROJECT_ID
    gcloud config set project ${PROJECT_ID}
    
  2. 啟用 Mesh API:

    gcloud services enable mesh.googleapis.com
    

    啟用 mesh.googleapis.com 會啟用下列 API:

    API 目的 可停用
    meshconfig.googleapis.com Cloud 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 啟用高可用性且可擴充的代管控制層。 是*

Terraform

gcloud config set project PROJECT_ID
GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project)
export GOOGLE_CLOUD_PROJECT

建立 GKE 叢集

在 Autopilot 模式中建立 GKE 叢集。

gcloud

  1. 建立叢集,註冊為機群成員

    gcloud container clusters create-auto asm-cluster \
        --location="us-central1" \
        --enable-fleet
    
  2. 確認叢集已向機群註冊:

    gcloud container fleet memberships list
    

    輸出結果會與下列內容相似:

    NAME: asm-cluster
    EXTERNAL_ID:
    LOCATION: us-central1
    

    記下成員資格名稱,因為您需要這個名稱來設定 Cloud Service Mesh。

Terraform

如要建立 GKE 叢集,可以使用 google_container_cluster 資源。設定 fleet 區塊,讓叢集在建立時新增至機群。

resource "google_container_cluster" "cluster" {
  name                = "asm-cluster"
  location            = var.region
  deletion_protection = false # Warning: Do not set deletion_protection to false for production clusters

  enable_autopilot = true
  fleet {
    project = data.google_project.project.name
  }
}

data "google_project" "project" {}

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

佈建代管 Cloud Service Mesh

您可以使用叢集機群成員資格的 servicemesh 功能,佈建代管 Cloud Service Mesh。

gcloud

  1. 在專案中啟用 Cloud Service Mesh 艦隊功能:

    gcloud container fleet mesh enable
    
  2. 啟用網狀網路的自動管理功能:

    gcloud container fleet mesh update \
        --management=automatic \
        --memberships=MEMBERSHIP_NAME \
        --location=us-central1
    

    MEMBERSHIP_NAME 替換為您驗證叢集已向車隊註冊時列出的成員名稱。

Terraform

如要啟用網格 API,可以使用 google_project_service 資源

您可以使用 google_gke_hub_featuregoogle_gke_hub_feature_membership 資源,在叢集上設定代管 Cloud Service Mesh。

resource "google_project_service" "mesh_api" {
  service = "mesh.googleapis.com"

  disable_dependent_services = true
}

resource "google_gke_hub_feature" "feature" {
  name     = "servicemesh"
  location = "global"

  depends_on = [
    google_project_service.mesh_api
  ]
}

resource "google_gke_hub_feature_membership" "feature_member" {
  location   = "global"
  feature    = google_gke_hub_feature.feature.name
  membership = google_container_cluster.cluster.fleet.0.membership
  membership_location = google_container_cluster.cluster.location
  mesh {
    management = "MANAGEMENT_AUTOMATIC"
  }
}

如要瞭解如何套用或移除 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 閘道是網格邊緣運作的負載平衡器,可接收傳入或傳出的 HTTP/TCP 連線。

您要將閘道部署至專屬命名空間,並為部署作業加上標籤,確保閘道能由 Cloud Service Mesh 控制層安全管理及自動升級。

  1. 下載憑證,以便存取叢集:

    gcloud container clusters get-credentials asm-cluster --location=us-central1
    
  2. 為閘道部署作業建立命名空間:

    kubectl create namespace bank-gateways
    
  3. 在命名空間中新增標籤,讓 Cloud Service Mesh 控制層自動將閘道設定注入部署作業。

    kubectl label namespace bank-gateways istio-injection=enabled
    
  4. 將輸入閘道部署至命名空間:

    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/gateway
    

    kubectl

    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 專案會發布效能和可擴充性相關資訊

部署範例應用程式

  1. 為部署建立 Kubernetes 命名空間:

    kubectl create namespace bank-sample
    
  2. 為命名空間新增標籤,讓 Cloud Service Mesh 自動將 Sidecar Proxy 注入範例 Pod:

    kubectl label namespace bank-sample istio-injection=enabled
    
  3. 部署範例應用程式:

    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/
    
  4. 等待應用程式準備就緒。這項作業需要幾分鐘才能完成。

    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
    
  5. 建立 Istio GatewayVirtualService 資源,公開輸入閘道後方的應用程式:

    kubectl apply -n bank-sample -f bank-of-anthos/extras/istio/frontend-ingress.yaml
    
  6. 取得範例應用程式的連結:

    INGRESS_HOST=$(kubectl -n bank-gateways get service istio-ingressgateway \
        -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo "http://$INGRESS_HOST"
    
  7. 在瀏覽器中,點選連結開啟範例應用程式。使用預設使用者名稱和密碼登入,即可查看應用程式。

強制執行雙向傳輸層安全標準 (TLS)

確認已啟用 STRICT 相互傳輸層安全標準 (mTLS) 模式。在 istio-system 命名空間中,為網格套用預設 PeerAuthentication 政策。

  1. 將下列資訊清單儲存為 mesh-peer-authn.yaml

    apiVersion: "security.istio.io/v1beta1"
    kind: "PeerAuthentication"
    metadata:
      name: "default"
      namespace: "istio-system"
    spec:
      mtls:
        mode: STRICT
    
  2. 將資訊清單套用至叢集:

    kubectl apply -f mesh-peer-authn.yaml
    

如要覆寫這項設定,請在特定命名空間中建立 PeerAuthentication 資源。

探索 Cloud Service Mesh 資訊主頁

  1. 在 Google Cloud 控制台中,前往「Cloud Service Mesh」,查看網格的資訊主頁:

    前往 Cloud Service Mesh

  2. 從選單列的下拉式清單中選取專案。

    您會看到總覽表格,其中列出網格中的所有微服務,以及微服務間連線的圖形化視覺化呈現。下表會顯示每個微服務的三個 SRE 「黃金信號」

    • 流量 - 每秒要求數
    • 錯誤率 - 百分比
    • 延遲時間 - 毫秒

    這些指標是根據微服務處理的實際流量計算。部署為範例應用程式一部分的 loadgenerator 用戶端,會自動將持續測試流量傳送至 frontend 服務。Cloud Service Mesh 會自動將指標、記錄和 (選用) 追蹤記錄傳送至 Google Cloud Observability。

  3. 按一下表格中的frontend服務,即可查看該服務的總覽資訊主頁。您會看到服務的其他指標,以及傳入和輸出連線的視覺化資料。您也可以建立服務等級目標 (SLO),監控服務並發出快訊。

確認已啟用 mTLS

按一下面板中的安全性連結,即可查看 frontend 服務的安全性總覽。表格和視覺化內容會顯示微服務之間每個傳入和傳出連線的綠色鎖頭圖示。這個圖示表示連線使用 mTLS 進行驗證和加密。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

如要避免系統向您的 Google Cloud 帳戶收取這個教學課程所用資源的費用,請執行下列動作:

刪除專案

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

刪除個別資源

如果您使用現有專案,但不想刪除專案,請刪除個別資源。

gcloud

  1. 刪除範例應用程式和閘道:

    kubectl delete namespace bank-sample
    kubectl delete namespace bank-gateways
    
  2. 按照操作說明解除安裝 Cloud Service Mesh

  3. 刪除 GKE 叢集:

    gcloud container clusters delete --region us-central1 asm-cluster --quiet
    

Terraform

刪除您使用 Terraform 建立的資源:

  terraform destroy

後續步驟