本教學課程將逐步說明 Cloud Service Mesh 的常見應用情境,也就是推出初期測試版本部署作業。
什麼是初期測試部署?
初期測試部署會將一小部分的流量導向新版微服務,然後讓您逐步向全體使用者推出新版微服務,同時淘汰舊版微服務。如果這個程序發生錯誤,流量可以切換回舊版。 使用 Cloud Service Mesh 即可設定流量路徑,確保新服務安全上線。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本教學課程後,您可以刪除已建立的資源,避免持續產生費用。詳情請參閱「清除」。
事前準備
確認 Cloud 專案已啟用計費功能。瞭解如何確認專案已啟用計費功能。
在 GKE 叢集上安裝代管 Cloud Service Mesh*。
複製存放區:
git clone https://github.com/GoogleCloudPlatform/anthos-service-mesh-samples cd anthos-service-mesh-samples/docs/canary-service
部署 Online Boutique
將
kubectl
的目前內容設為部署 Online Boutique 的叢集:gcloud container clusters get-credentials CLUSTER_NAME \ --project=PROJECT_ID \ --zone=CLUSTER_LOCATION
為範例應用程式和輸入閘道建立命名空間:
kubectl create namespace onlineboutique
標記
onlineboutique
命名空間,自動插入 Envoy Proxy。 請按照這篇文章的步驟,啟用自動 Sidecar 插入功能。部署範例應用程式。在本教學課程中,您將部署 Online Boutique,這是一個微服務示範應用程式。
kubectl apply \ -n onlineboutique \ -f https://raw.githubusercontent.com/GoogleCloudPlatform/anthos-service-mesh-samples/main/docs/shared/online-boutique/kubernetes-manifests.yaml
執行下列指令,將
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 補充容器 Proxy。部署
productcatalog
第 1 版的VirtualService
和DestinationRule
:kubectl apply -f destination-vs-v1.yaml -n onlineboutique
請注意,資源中只會出現
v1
。使用 Ingress 的外部 IP 位址,在瀏覽器中前往應用程式:
kubectl get services -n GATEWAY_NAMESPACE
接下來,我們將瀏覽 Cloud Service Mesh 使用者介面,並說明如何查看指標。
在 Google Cloud 控制台中部署及查看服務
前往 Google Cloud 控制台的「GKE Enterprise Services」(GKE Enterprise 服務) 頁面。
根據預設,您會在「表格」檢視畫面中查看服務。
表格總覽可讓您一目瞭然地觀察所有服務和重要指標。
按一下右上方的「拓撲」。 您可以在這裡查看服務及其互動情形。
您可以展開服務,然後將游標懸停在服務上,查看每項服務每秒的要求數。
返回「表格檢視」。
在「服務表」中,選取
productcatalogservice
。系統會將你導向服務總覽頁面。在畫面左側,按一下「流量」。
確保傳入
productcatalogservice
的流量 100% 都會導向工作負載服務。
下節將說明如何建立 productcatalog
服務的 v2 版本。
部署服務的 v2 版本
在本教學課程中,
productcatalogservice-v2
會在含有EXTRA_LATENCY
欄位的要求中導入 3 秒延遲。將這項資源套用至
onlineboutique
命名空間。kubectl apply -f productcatalog-v2.yaml -n onlineboutique
檢查應用程式 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
。DestinationRule
是指定服務子集的方式。在這個情境中,productcatalogservice
的 v1 和 v2 都有子集。請注意
labels
欄位。流量由VirtualService
路由後,系統會區分productcatalogservice
的版本。套用
DestinationRule
:kubectl apply -f destination-v1-v2.yaml -n onlineboutique
在 v1 和 v2 之間拆分流量
VirtualService
是將一小部分流量導向productcatalogservice
v2 的方式。子集欄位會指出版本,權重欄位則會指出流量的百分比拆分。75% 的流量會導向 productcatalog 的 v1,25% 的流量則會導向 v2。
套用
VirtualService
:kubectl apply -f vs-split-traffic.yaml -n onlineboutique
如果您造訪叢集 Ingress 的 EXTERNAL_IP
,應該會發現前端載入速度會定期變慢。
下一節將說明 GKE Enterprise Google Cloud 控制台中的流量分割。
在 Google Cloud 控制台中觀察流量分配
返回 Google Cloud 控制台,然後前往 GKE Enterprise 服務頁面 前往 GKE Enterprise 服務
按一下右上方的「拓撲」。
擴展
productcatalogservice
工作負載。您會看到productcatalogservice
和productcatalogservice-v2
部署作業。返回「表格檢視」。按一下「服務」表格中的
productcatalogservice
。 返回左側導覽列的「流量」。請注意,系統會根據
VirtualService
檔案中指定的百分比,將傳入流量分配給 v1 和 v2,且 productcatalog 服務有 2 個工作負載。畫面右側會顯示「要求」、「錯誤率」和「延遲時間指標」。使用 Cloud Service Mesh 時,每項服務都會列出這些指標,方便您進行觀測。
推出或復原至某個版本
在 Canary 部署期間觀察指標後,您可以利用 VirtualService
資源推出新服務,或回復舊服務。
推出作業
確認第 2 版服務的行為符合預期後,請逐步增加第 2 版服務的流量行為。最終,所有流量都會導向新服務。
如要將所有流量導向 productcatalogservice
的 v2:
kubectl apply -f vs-v2.yaml -n onlineboutique
復原
如要復原為 v1 服務,只要套用先前的 destination-vs-v1.yaml
即可。這樣一來,流量只會導向 productcatalogservice
的 v1 版本。
如要將所有流量導向 productcatalogservice
的 v1:
kubectl apply -f vs-v1.yaml -n onlineboutique
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
如要避免系統持續向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除專案或個別資源。
刪除專案
在 Cloud Shell 中刪除專案:
gcloud projects delete PROJECT_ID
刪除資源
如要避免產生額外費用,請刪除叢集:
gcloud container clusters delete CLUSTER_NAME \ --project=PROJECT_ID \ --zone=CLUSTER_LOCATION
如要保留叢集並移除 Online Boutique 範例,請按照下列步驟操作:
刪除應用程式命名空間:
kubectl delete -f namespace onlineboutique
預期輸出內容:
namespace "onlineboutique" deleted
刪除服務項目:
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
後續步驟
- 如需設定
PeerAuthentication
政策的一般指南,請參閱「設定傳輸層安全防護」。