Cloud Service Mesh 範例:初期測試部署

在本教學課程中,您將逐步瞭解如何使用 Cloud Service Mesh 推出初期測試部署,這是常見的用途。

什麼是初期測試部署?

初期測試部署會將一小部分流量轉送至新版微服務,然後逐步向全體使用者族群推出,同時逐步淘汰舊版本。如果過程中發生問題,流量可以切換回舊版本。Cloud Service Mesh 可讓您轉送流量,確保安全導入新服務。

費用

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

您可以使用 Pricing Calculator,根據預測用量估算費用。

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

完成本教學課程後,您可以刪除建立的資源,以免持續產生費用。詳情請參閱「清除所用資源」一節。

事前準備

部署 Online Boutique

  1. 將 kubectl 的目前環境設為部署 Online Boutique 的叢集:

    gcloud container clusters get-credentials CLUSTER_NAME  \
    --project=PROJECT_ID \
    --zone=CLUSTER_LOCATION 
    
  2. 為範例應用程式和 ingress 閘道建立命名空間:

    kubectl create namespace onlineboutique
    
  3. 為 onlineboutique 命名空間加上標籤,自動插入 Envoy Proxy。請按照這篇文章的步驟,啟用自動加入邊車容器功能。

  4. 部署範例應用程式。在本教學課程中,您將部署微服務示範應用程式 Online Boutique。

    kubectl apply \
    -n onlineboutique \
    -f https://raw.githubusercontent.com/GoogleCloudPlatform/anthos-service-mesh-samples/main/docs/shared/online-boutique/kubernetes-manifests.yaml
    
  5. 執行下列指令,將標籤 version=v1 新增至 productcatalog 部署作業:

    kubectl patch deployments/productcatalogservice -p '{"spec":{"template":{"metadata":{"labels":{"version":"v1"}}}}}' \
    -n onlineboutique
    

    查看已部署的服務:

    kubectl get pods -n onlineboutique
    

    預期輸出內容:

    NAME                                     READY   STATUS    RESTARTS   AGE
    adservice-85598d856b-m84m6               2/2     Running   0          2m7s
    cartservice-c77f6b866-m67vd              2/2     Running   0          2m8s
    checkoutservice-654c47f4b6-hqtqr         2/2     Running   0          2m10s
    currencyservice-59bc889674-jhk8z         2/2     Running   0          2m8s
    emailservice-5b9fff7cb8-8nqwz            2/2     Running   0          2m10s
    frontend-77b88cc7cb-mr4rp                2/2     Running   0          2m9s
    loadgenerator-6958f5bc8b-55q7w           2/2     Running   0          2m8s
    paymentservice-68dd9755bb-2jmb7          2/2     Running   0          2m9s
    productcatalogservice-84f95c95ff-c5kl6   2/2     Running   0          114s
    recommendationservice-64dc9dfbc8-xfs2t   2/2     Running   0          2m9s
    redis-cart-5b569cd47-cc2qd               2/2     Running   0          2m7s
    shippingservice-5488d5b6cb-lfhtt         2/2     Running   0          2m7s
    

    應用程式的所有 Pod 都應啟動並執行,且 READY 欄中會顯示 2/2。這表示 Pod 已成功插入 Envoy Sidecar Proxy。

  6. 部署 productcatalog 第 1 版的 VirtualService 和 DestinationRule:

    kubectl apply -f destination-vs-v1.yaml -n onlineboutique
    
    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:
      name: productcatalogservice
    spec:
      host: productcatalogservice
      subsets:
      - labels:
          version: v1
        name: v1
    ---
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: productcatalogservice
    spec:
      hosts:
      - productcatalogservice
      http:
      - route:
        - destination:
            host: productcatalogservice
            subset: v1

    請注意,資源中只有 v1。

  7. 在瀏覽器中使用 ingress 的外部 IP 位址,前往應用程式:

    kubectl get services -n GATEWAY_NAMESPACE
    

下一節將介紹 Cloud Service Mesh UI,並說明如何查看指標。

在 Google Cloud 控制台部署及查看服務

  1. 前往 Google Cloud 控制台的「GKE Enterprise Services」頁面。

    前往 GKE Enterprise 服務

  2. 根據預設,系統會以「Table」(表格) 檢視畫面顯示服務。

    「表格總覽」可讓您一目瞭然地觀察所有服務和重要指標。

    所有服務工作負載

  3. 按一下右上方的「拓撲」。 您可以在這裡查看服務以及服務之間的互動。

    將游標懸停在服務上,即可展開服務並查看每項服務每秒的要求數。

    所有服務工作負載拓撲

  4. 返回「Table View」。

  5. 在「Services Table」(服務表格) 中,選取 productcatalogservice。系統會將您導向服務總覽。

  6. 按一下畫面左側的「流量」。

  7. 確保傳入 productcatalogservice 的流量 100% 傳送至工作負載服務。

    productcatalog svc 流量

下一節將說明如何建立 productcatalog 服務的第 2 版。

部署服務的第 2 版

  1. 在本教學課程中,productcatalogservice-v2 會在含有 EXTRA_LATENCY 欄位的要求中,導入 3 秒的延遲時間。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: productcatalogservice-v2
    spec:
      selector:
        matchLabels:
          app: productcatalogservice
      template:
        metadata:
          labels:
            app: productcatalogservice
            version: v2
        spec:
          containers:
          - env:
            - name: PORT
              value: '3550'
            - name: EXTRA_LATENCY
              value: 3s
            name: server
            image: gcr.io/google-samples/microservices-demo/productcatalogservice:v0.3.6
            livenessProbe:
              exec:
                command: ["/bin/grpc_health_probe", "-addr=:3550"]
            ports:
            - containerPort: 3550
            readinessProbe:
              exec:
                command: ["/bin/grpc_health_probe", "-addr=:3550"]
            resources:
              limits:
                cpu: 200m
                memory: 128Mi
              requests:
                cpu: 100m
                memory: 64Mi
          terminationGracePeriodSeconds: 5

    將這項資源套用至 onlineboutique 命名空間。

    kubectl apply -f productcatalog-v2.yaml -n onlineboutique
    
  2. 檢查應用程式 Pod。

    kubectl get pods -n onlineboutique
    

    預期輸出內容:

    NAME                                     READY   STATUS    RESTARTS   AGE
    adservice-85598d856b-8wqfd                  2/2     Running   0          25h
    cartservice-c77f6b866-7jwcr                 2/2     Running   0          25h
    checkoutservice-654c47f4b6-n8c6x            2/2     Running   0          25h
    currencyservice-59bc889674-l5xw2            2/2     Running   0          25h
    emailservice-5b9fff7cb8-jjr89               2/2     Running   0          25h
    frontend-77b88cc7cb-bwtk4                   2/2     Running   0          25h
    loadgenerator-6958f5bc8b-lqmnw              2/2     Running   0          25h
    paymentservice-68dd9755bb-dckrj             2/2     Running   0          25h
    productcatalogservice-84f95c95ff-ddhjv      2/2     Running   0          25h
    productcatalogservice-v2-6df4cf5475-9lwjb   2/2     Running   0          8s
    recommendationservice-64dc9dfbc8-7s7cx      2/2     Running   0          25h
    redis-cart-5b569cd47-vw7lw                  2/2     Running   0          25h
    shippingservice-5488d5b6cb-dj5gd            2/2     Running   0          25h
    

    請注意,現在會列出兩個 productcatalogservices。

  3. DestinationRule 是指定服務子集的方式。在這個情境中,productcatalogservice 的 v1 和 v2 各有一個子集。

    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:
      name: productcatalogservice
    spec:
      host: productcatalogservice
      subsets:
      - labels:
          version: v1
        name: v1
      - labels:
          version: v2
        name: v2

    請留意 labels 欄位。VirtualService 轉送流量後,系統會區分 productcatalogservice 的版本。

    套用 DestinationRule:

    kubectl apply -f destination-v1-v2.yaml -n onlineboutique
    

將流量分配給 v1 和 v2

  1. VirtualService是指將一小部分流量導向 productcatalogservice 的第 2 版。

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: productcatalogservice
    spec:
      hosts:
      - productcatalogservice
      http:
      - route:
        - destination:
            host: productcatalogservice
            subset: v1
          weight: 75
        - destination:
            host: productcatalogservice
            subset: v2
          weight: 25

    subset 欄位代表版本,weight 欄位則代表流量分配百分比。75% 的流量會傳送至 productcatalog 的 v1,25% 則會傳送至 v2。

    套用 VirtualService:

    kubectl apply -f vs-split-traffic.yaml -n onlineboutique
    

如果前往叢集 Ingress 的 EXTERNAL_IP,應該會發現前端載入速度有時會變慢。

在下一節中,您將在 GKE Enterprise Google Cloud 控制台查看流量拆分。

在 Google Cloud 控制台觀察流量分配

  1. 返回 Google Cloud 控制台,然後前往「GKE Enterprise Services」頁面 前往「GKE Enterprise Services」

  2. 按一下右上方的「拓撲」。

    展開 productcatalogservice 工作負載。您會看到 productcatalogservice 和 productcatalogservice-v2 部署作業。

    productcatalog svc v1 v2 流量拓撲

  3. 返回「Table View」(表格檢視)。按一下「服務」表格中的「productcatalogservice」。返回左側導覽列的「流量」。

  4. 請注意,傳入流量會依 VirtualService 檔案中指定的百分比,分配給 v1 和 v2,且 productcatalog 服務有 2 個工作負載。

    畫面右側會顯示「要求」、「錯誤率」和「延遲時間」指標。Cloud Service Mesh 會列出每項服務的這些指標,讓您掌握可觀測性。

    productcatalog svc v1 v2 traffic

推出或復原至特定版本

在初期測試部署期間觀察指標後,您可以利用 VirtualService 資源,推出新服務或復原至舊服務。

推出作業

確認第 2 版服務的行為符合預期後,即可逐步增加傳送至第 2 版服務的流量。最後,流量可以 100% 導向新服務。

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: productcatalogservice
spec:
  hosts:
  - productcatalogservice
  http:
  - route:
    - destination:
        host: productcatalogservice
        subset: v2

如要將所有流量導向 productcatalogservice 的第 2 版,請執行下列操作:

kubectl apply -f vs-v2.yaml -n onlineboutique

復原

如要復原至 v1 服務,只要套用先前的 destination-vs-v1.yaml 即可。這項操作只會將流量導向 productcatalogservice 的 v1。

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: productcatalogservice
spec:
  hosts:
  - productcatalogservice
  http:
  - route:
    - destination:
        host: productcatalogservice
        subset: v1

如要將所有流量導向 productcatalogservice 的第 1 版:

kubectl apply -f vs-v1.yaml -n onlineboutique

清除所用資源

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

如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,您可以刪除專案或個別資源。

刪除專案

  1. 在 Cloud Shell 刪除專案:

    gcloud projects delete PROJECT_ID
    

刪除資源

  • 如要避免產生額外費用,請刪除叢集:

    gcloud container clusters delete  CLUSTER_NAME  \
    --project=PROJECT_ID \
    --zone=CLUSTER_LOCATION 
    
  • 如要保留叢集並移除 Online Boutique 範例:

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

      kubectl delete -f namespace onlineboutique
      

      預期輸出內容:

      namespace "onlineboutique" deleted
      
    2. 刪除服務項目:

      kubectl delete -f https://raw.githubusercontent.com/GoogleCloudPlatform/microservices-demo/main/istio-manifests/frontend.yaml -n onlineboutique
      kubectl delete -f https://raw.githubusercontent.com/GoogleCloudPlatform/microservices-demo/main/istio-manifests/frontend-gateway.yaml -n onlineboutique
      

      預期輸出內容:

      serviceentry.networking.istio.io "allow-egress-googleapis" deleted
      serviceentry.networking.istio.io "allow-egress-google-metadata" deleted
      

後續步驟