使用 gcloud CLI 透過 Cloud Service Mesh 建立 GKE 叢集

在本教學課程中,您將使用 Google Kubernetes Engine (GKE) Fleet API,在新的 GKE 公開叢集上佈建代管 Cloud Service Mesh。本教學課程會逐步引導您完成下列操作:

  1. 設定專案。 Google Cloud
  2. 建立 GKE 叢集,並使用 Cloud Service Mesh 要求的最低 vCPU 數量。
  3. 將 GKE 叢集註冊至專案的機群
  4. 使用 Fleet API 在叢集上佈建代管 Cloud Service Mesh。
  5. 部署輸入閘道,公開應用程式。
  6. 部署範例應用程式,以便在 Google Cloud 控制台的 Cloud Service Mesh 資訊主頁上查看遙測資料。
  7. 公開及存取範例應用程式。

Fleet API

本指南假設您已熟悉機群,也就是 GKE 叢集和其他資源的邏輯分組,可一起管理。機群是 GKE 的概念,而非 Kubernetes 的概念。將叢集註冊至機群後,您就能使用 gcloud container fleet mesh update 指令,在該叢集上佈建受管理 Cloud Service Mesh。 Fleet API (gkehub.googleapis.com) 可啟用車隊功能,您會在開始本教學課程時啟用這項功能。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

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

完成本快速入門後,您可以刪除叢集,避免系統繼續計費。詳情請參閱「清除」。

事前準備

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the GKE, Fleet (GKE Hub), and Cloud Service Mesh APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the GKE, Fleet (GKE Hub), and Cloud Service Mesh APIs.

    Enable the APIs

  8. 記下專案 ID
  9. 安裝必要工具

    您可以在 Cloud Shell 或本機電腦上執行這項工具。Cloud Shell 會預先安裝所有必要工具。

    Cloud Shell

    Cloud Shell 會佈建g1-small Compute Engine 虛擬機器 (VM),執行以 Debian 為基礎的 Linux 作業系統。使用 Cloud Shell 的優點包括:

    • Cloud Shell 包含 gcloudkubectlgit 和其他您需要的指令列工具。

    • Cloud Shell 的 $HOME 目錄有 5 GB 的永久儲存空間。

    • 您可以選擇使用下列文字編輯器

      • 程式碼編輯器:按一下 Cloud Shell 視窗頂端的 即可存取。

      • Emacs、Vim 或 Nano,可透過 Cloud Shell 的指令列存取。

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

    本機電腦

    1. 請確認您已安裝下列工具:

    2. 使用 Google Cloud CLI 進行驗證:

      gcloud auth login --project PROJECT_ID
      
    3. 更新元件:

      gcloud components update
      

    建立 GKE 叢集

    1. 執行下列指令,建立叢集時,請確保 vCPU 數量符合 Cloud Service Mesh 的最低要求。在指令中,將預留位置替換為下列資訊:

      • CLUSTER_NAME:叢集名稱。名稱只能包含小寫英數字元和 -,開頭須為英文字母,結尾須為英數字元,且長度不得超過 40 個半形字元。
      • PROJECT_ID:叢集建立所在的專案 ID。
      • CLUSTER_LOCATION:叢集的區域,例如 us-central1-a
      gcloud container clusters create CLUSTER_NAME \
          --project=PROJECT_ID \
          --zone=CLUSTER_LOCATION \
          --machine-type=e2-standard-4 \
          --num-nodes=2 \
          --workload-pool=PROJECT_ID.svc.id.goog
      

      建立叢集需要幾分鐘的時間。叢集建立期間,gcloud 指令會顯示下列內容:

      Creating cluster CLUSTER_NAME in CLUSTER_LOCATION...working...
      

      成功建立後,預期輸出內容如下所示:

      Creating cluster CLUSTER_NAME in CLUSTER_LOCATION...done.
      Created [https://container.googleapis.com/v1/projects/PROJECT_ID/zones/CLUSTER_LOCATION/clusters/CLUSTER_NAME].
      To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/CLUSTER_LOCATION/CLUSTER_NAME?project=PROJECT_ID
      kubeconfig entry generated for CLUSTER_NAME.
      NAME: CLUSTER_NAME
      LOCATION: CLUSTER_LOCATION
      MASTER_VERSION: 1.20.10-gke.1600
      MASTER_IP: 198.51.100.1
      MACHINE_TYPE: e2-standard-4
      NODE_VERSION: 1.20.10-gke.1600
      NUM_NODES: 2
      STATUS: RUNNING
      
    2. 取得驗證憑證,以便與叢集互動。

      gcloud container clusters get-credentials CLUSTER_NAME \
          --project=PROJECT_ID \
          --zone=CLUSTER_LOCATION
      

      預期輸出內容:

      Fetching cluster endpoint and auth data.
      kubeconfig entry generated for CLUSTER_NAME.
      
    3. kubectl 的目前環境設為叢集。

      kubectl config set-context CLUSTER_NAME
      

      預期輸出內容:

      Context "CLUSTER_NAME" created.
      

    佈建 Cloud Service Mesh

    如果您在建立叢集後未關閉這個頁面,預留位置會顯示您為 gcloud container clusters create 指令輸入的值。

    1. 在專案的 Fleet 上啟用 Cloud Service Mesh。

      gcloud container fleet mesh enable --project PROJECT_ID
      

      輸出內容類似如下:

      Waiting for Feature Service Mesh to be created...done.
      
    2. 將叢集註冊至專案的機群:

      gcloud container fleet memberships register CLUSTER_NAME-membership \
        --gke-cluster=CLUSTER_LOCATION/CLUSTER_NAME \
        --enable-workload-identity \
        --project PROJECT_ID
      

      輸出內容類似如下:

       Waiting for membership to be created...done.
       Finished registering to the Fleet.
      
    3. 使用 Fleet API 在叢集上佈建代管 Cloud Service Mesh:

      gcloud container fleet mesh update \
        --management automatic \
        --memberships CLUSTER_NAME-membership \
        --project PROJECT_ID
      

      輸出內容類似如下:

      Waiting for Feature Service Mesh to be updated...done.
      
    4. 確認叢集已啟用受管理 Cloud Service Mesh,且可供使用:

      gcloud container fleet mesh describe --project PROJECT_ID
      

      Cloud Service Mesh 大約需要 10 分鐘才能完成佈建,並在叢集上準備就緒。如果看到 controlPlaneManagement.state: DISABLEDcontrolPlaneManagement.state: PROVISIONING,請每隔幾分鐘重新執行上一個指令,直到看到 controlPlaneManagement.state: ACTIVE 為止。

      輸出內容類似如下:

      createTime: '2022-07-06T01:05:39.110120474Z'
      membershipSpecs:
        projects/123456789123/locations/global/memberships/your-cluster-membership:
          mesh:
            management: MANAGEMENT_AUTOMATIC
      membershipStates:
        projects/123456789123/locations/global/memberships/your-cluster-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'
      

    下載程式碼範例

    複製包含本教學課程所用範例程式碼的 git 存放區:

       git clone https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages.git
    

    本教學課程的後續章節會使用 DIR_PATH 變數。將這個變數設為您複製的 anthos-service-mesh-packages 存放區路徑 (例如 ./anthos-service-mesh-packages)。

    部署 Ingress 閘道

    您可以選擇在服務網格中部署及管理閘道。閘道說明在網格邊緣運作的負載平衡器,可接收傳入或傳出的 HTTP/TCP 連線。閘道是 Envoy 代理程式,可讓您精細控管進出網格的流量。

    1. 如果還沒有 Ingress 閘道的命名空間,請建立一個。閘道是使用者工作負載,最佳做法是不應部署在控制層命名空間中。將 GATEWAY_NAMESPACE 替換成命名空間名稱。

      kubectl create namespace GATEWAY_NAMESPACE
      

      預期輸出內容:

      namespace/GATEWAY_NAMESPACE created
      
    2. 在閘道上啟用自動插入功能。所需步驟取決於您要使用預設插入標籤 (例如 istio-injection=enabled),還是閘道命名空間中的修訂版本標籤。預設修訂版本標記和修訂版本標籤會由 Sidecar 注入器 Webhook 用來將注入的 Proxy 與特定控制層修訂版本建立關聯。

      預設插入標籤

      將預設插入標籤套用至命名空間。

      kubectl label namespace GATEWAY_NAMESPACE istio-injection=enabled istio.io/rev-
      

      修訂版本標籤

      1. 使用下列指令找出 istiod 上的修訂版本標籤:

        kubectl get deploy -n istio-system -l app=istiod -o \
          "jsonpath={.items[*].metadata.labels['istio\.io/rev']}{'\n'}"
        

        這項指令會輸出與 Cloud Service Mesh 版本對應的修訂版本標籤,例如:asm-1264-1

      2. 將修訂版本標籤套用至命名空間。在下列指令中,REVISION 是您在上一步記下的 istiod 修訂版本標籤值。

        kubectl label namespace GATEWAY_NAMESPACE \
          istio.io/rev=REVISION --overwrite
        

        預期輸出內容:

        namespace/GATEWAY_NAMESPACE labeled
        

      您可以忽略輸出內容中的 "istio.io/rev" not found 訊息。也就是說,命名空間先前沒有 istio.io/rev 標籤,這應該是 Cloud Service Mesh 新安裝或新部署作業的預期情況。如果命名空間同時具有 istio.io/revistio-injection 標籤,自動插入作業就會失敗,因此 Cloud Service Mesh 文件中的所有 kubectl label 指令都會明確指定這兩個標籤。

      如果閘道命名空間未加上標籤,閘道嘗試拉取 auto 映像檔時,istio-ingressgateway Pod 會失敗並顯示 ImagePullBackOff 錯誤。這張圖片應由 Webhook 取代。

    3. anthos-service-mesh-packages 存放區下載 Ingress 閘道 .yaml 設定檔範例。

    4. 直接套用範例 Ingress 閘道 .yaml 設定,或視需要修改。

      kubectl apply -n GATEWAY_NAMESPACE \
        -f CONFIG_PATH/istio-ingressgateway
      

      預期輸出內容:

      deployment.apps/istio-ingressgateway created
      poddisruptionbudget.policy/istio-ingressgateway created
      horizontalpodautoscaler.autoscaling/istio-ingressgateway created
      role.rbac.authorization.k8s.io/istio-ingressgateway created
      rolebinding.rbac.authorization.k8s.io/istio-ingressgateway created
      service/istio-ingressgateway created
      serviceaccount/istio-ingressgateway created
      

    進一步瞭解閘道的最佳做法

    部署 Online Boutique 範例

    anthos-service-mesh-packages 存放區中的 Online Boutique 範例應用程式,是根據 microservices-demo 存放區中的原始資訊清單集修改而成。根據最佳做法,每項服務都會部署在獨立的命名空間中,並使用專屬的服務帳戶。

    1. 為應用程式建立命名空間:

      kubectl apply -f \
        DIR_PATH/samples/online-boutique/kubernetes-manifests/namespaces
      

      預期輸出內容:

      namespace/ad created
      namespace/cart created
      namespace/checkout created
      namespace/currency created
      namespace/email created
      namespace/frontend created
      namespace/loadgenerator created
      namespace/payment created
      namespace/product-catalog created
      namespace/recommendation created
      namespace/shipping created
      
    2. 啟用自動補充植入功能 (自動植入)。所需指令取決於您要使用預設插入標籤 (例如 istio-injection=enabled),還是用於註解 Ingress 閘道命名空間的相同修訂版本標籤

      預設插入標籤

      將預設插入標籤套用至命名空間。在下列指令中,GATEWAY_NAMESPACE 與您用於註解 Ingress 閘道命名空間的值相同。

      for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation shipping; do
        kubectl label namespace $ns istio-injection=enabled istio.io/rev-
      done;
      

      預期輸出內容:

      namespace/ad labeled
      namespace/cart labeled
      namespace/checkout labeled
      namespace/currency labeled
      namespace/email labeled
      namespace/frontend labeled
      namespace/loadgenerator labeled
      namespace/payment labeled
      namespace/product-catalog labeled
      namespace/recommendation labeled
      namespace/shipping labeled
      

      修訂版本標籤

      將修訂版本標籤套用至應用程式命名空間。 在下列指令中,REVISION 是您用來為 Ingress 閘道命名空間加註的相同值。

      for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation shipping; do
        kubectl label namespace $ns istio.io/rev=REVISION --overwrite
      done;
      

      預期輸出內容:

      namespace/ad labeled
      namespace/cart labeled
      namespace/checkout labeled
      namespace/currency labeled
      namespace/email labeled
      namespace/frontend labeled
      namespace/loadgenerator labeled
      namespace/payment labeled
      namespace/product-catalog labeled
      namespace/recommendation labeled
      namespace/shipping labeled
      
    3. 將範例應用程式部署至叢集。

      1. 建立服務帳戶和部署作業:

        kubectl apply -f \
         DIR_PATH/samples/online-boutique/kubernetes-manifests/deployments
        

        預期輸出內容:

        serviceaccount/ad created
        deployment.apps/adservice created
        serviceaccount/cart created
        deployment.apps/cartservice created
        serviceaccount/checkout created
        deployment.apps/checkoutservice created
        serviceaccount/currency created
        deployment.apps/currencyservice created
        serviceaccount/email created
        deployment.apps/emailservice created
        serviceaccount/frontend created
        deployment.apps/frontend created
        serviceaccount/loadgenerator created
        deployment.apps/loadgenerator created
        serviceaccount/payment created
        deployment.apps/paymentservice created
        serviceaccount/product-catalog created
        deployment.apps/productcatalogservice created
        serviceaccount/recommendation created
        deployment.apps/recommendationservice created
        serviceaccount/shipping created
        deployment.apps/shippingservice created
        
      2. 建立服務:

        kubectl apply -f \
         DIR_PATH/samples/online-boutique/kubernetes-manifests/services
        

        預期輸出內容:

        service/adservice created
        service/cartservice created
        service/checkoutservice created
        service/currencyservice created
        service/emailservice created
        service/frontend created
        service/frontend-external created
        service/paymentservice created
        service/productcatalogservice created
        service/recommendationservice created
        service/shippingservice created
        
      3. 建立服務項目:

        kubectl apply -f \
         DIR_PATH/samples/online-boutique/istio-manifests/allow-egress-googleapis.yaml
        

        預期輸出內容:

        serviceentry.networking.istio.io/allow-egress-googleapis created
        serviceentry.networking.istio.io/allow-egress-google-metadata created
        

    公開及存取應用程式

    您可以透過多種方式公開應用程式。在本指南中,我們會使用上述部署的輸入閘道來執行這項操作。如要瞭解公開 Online Boutique 應用程式的其他方式,請參閱「部署 Online Boutique 範例應用程式」指南中的「公開及存取應用程式」一節。

    1. 為前端服務部署 GatewayVirtualService

      kubectl apply -f \
          DIR_PATH/samples/online-boutique/istio-manifests/frontend-gateway.yaml
      

      預期輸出內容:

      gateway.networking.istio.io/frontend-gateway created
      virtualservice.networking.istio.io/frontend-ingress created
      
    2. 取得Ingress 閘道的外部 IP 位址,然後將預留位置替換為下列資訊:

      • GATEWAY_SERVICE_NAME:Ingress 閘道服務的名稱。如果您部署範例閘道時未進行任何修改,則會是 istio-ingressgateway
      • GATEWAY_NAMESPACE:部署 Ingress 閘道的命名空間:
      kubectl get service GATEWAY_SERVICE_NAME \
          -n GATEWAY_NAMESPACE
      

      輸出內容類似如下:

      NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                                      AGE
      istio-ingressgateway   LoadBalancer   10.19.247.233   35.239.7.64   80:31380/TCP,443:31390/TCP,31400:31400/TCP   27m

      在本範例中,Ingress 閘道的 IP 位址為 35.239.7.64

    3. 在瀏覽器中前往應用程式,確認安裝是否成功:

      http://EXTERNAL_IP/
      

    查看 Service Mesh 資訊主頁

    在叢集上部署工作負載並注入 Sidecar Proxy 後,您可以在 Google Cloud 控制台中瀏覽 Cloud Service Mesh 頁面,查看 Cloud Service Mesh 提供的所有可觀測性功能。請注意,部署工作負載後,遙測資料大約需要一到兩分鐘才會顯示在Google Cloud 控制台中。

    Google Cloud 控制台中 Cloud Service Mesh 的存取權由身分與存取權管理 (IAM) 控管。如要存取 Cloud Service Mesh 頁面,專案擁有者必須授予使用者專案編輯者或檢視者角色,或是在 Google Cloud 控制台中控管 Cloud Service Mesh 存取權 Google Cloud 一文所述的限制較嚴格角色。

    1. 前往 Google Cloud 控制台的「Cloud Service Mesh」

      前往 Cloud Service Mesh

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

    3. 如果有多個服務網格,請從「Service Mesh」下拉式清單中選取網格。

    詳情請參閱「在 Google Cloud 控制台中探索 Cloud Service Mesh」。

    清除所用資源

    如要進一步瞭解相互傳輸層安全標準 (TLS),請參閱Anthos 服務網格範例:mTLS

    • 如要保留叢集並移除 Online Boutique 範例,請按照下列步驟操作:

      1. 刪除應用程式命名空間:

        kubectl delete -f DIR_PATH/samples/online-boutique/kubernetes-manifests/namespaces
        

        預期輸出內容:

        namespace "ad" deleted
        namespace "cart" deleted
        namespace "checkout" deleted
        namespace "currency" deleted
        namespace "email" deleted
        namespace "frontend" deleted
        namespace "loadgenerator" deleted
        namespace "payment" deleted
        namespace "product-catalog" deleted
        namespace "recommendation" deleted
        namespace "shipping" deleted
        
      2. 刪除服務項目:

        kubectl delete -f DIR_PATH/samples/online-boutique/istio-manifests/allow-egress-googleapis.yaml
        

        預期輸出內容:

        serviceentry.networking.istio.io "allow-egress-googleapis" deleted
        serviceentry.networking.istio.io "allow-egress-google-metadata" deleted
        
    • 如要避免產生額外費用,請刪除叢集:

      1. 執行下列指令:

        gcloud container clusters delete CLUSTER_NAME \
            --project=PROJECT_ID \
            --zone=CLUSTER_LOCATION
        
      2. 在「Do you want to continue (Y/n)?」提示中,輸入 y

        幾分鐘後,您會看到下列輸出內容:

        Deleting cluster CLUSTER_NAME...done.
        Deleted [https://container.googleapis.com/v1/projects/PROJECT_ID/zones/CLUSTER_LOCATION/clusters/CLUSTER_NAME].
        

    後續步驟