啟用 Kubernetes 工作負載

本頁面說明如何使用 Cloud Service Mesh 建立 Kubernetes 工作負載。

部署 Kubernetes Service

如要使用 Cloud Service Mesh 將 Kubernetes 服務部署至叢集,請務必執行下列操作:

  • 為所有容器建立 Kubernetes Service。所有部署作業都應附加 Kubernetes 服務。

  • 設定服務通訊埠名稱。雖然 GKE 允許定義未命名的 Service 通訊埠,但 Cloud Service Mesh 要求您提供通訊埠名稱,且該名稱必須與通訊埠的通訊協定相符。

  • 為部署作業加上標籤。這樣一來,您就能使用 Cloud Service Mesh 流量管理功能,例如在相同服務的不同版本之間分配流量。

下列 Deployment 和 Service 範例說明瞭這些需求:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloserver
  template:
    metadata:
      labels:
        app: helloserver
    spec:
      containers:
      - image: gcr.io/google-samples/istio/helloserver:v0.0.1
        imagePullPolicy: Always
        name: main
      restartPolicy: Always
      terminationGracePeriodSeconds: 5
apiVersion: v1
kind: Service
metadata:
  name: hellosvc
spec:
  ports:
  - name: http
    port: 80
    targetPort: 8080
  selector:
    app: helloserver
  type: LoadBalancer

在具備 Cloud Service Mesh 的叢集上部署服務後,請務必插入 Sidecar Proxy。

範例:部署 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. 啟用要插入的命名空間。步驟取決於控制層實作。

    代管 (TD)

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

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

    代管 (Istiod)

    建議做法:執行下列指令,將預設插入標籤套用至命名空間:

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

    如果您是使用代管 Istiod 控制層的現有使用者:建議您使用預設插入,但系統也支援修訂版本插入。請按照下列操作說明進行:

    1. 執行下列指令,找出可用的發布管道:

      kubectl -n istio-system get controlplanerevision
      

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

      NAME                AGE
      asm-managed-rapid   6d7h
      

      在輸出內容中,NAME 欄下方的值是修訂版本標籤,對應至 Cloud Service Mesh 版本的可用發布管道。

    2. 將修訂版本標籤套用至命名空間:

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

    叢集內

    建議做法:執行下列指令,將預設插入標籤套用至命名空間:

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

    建議您使用預設插入,但我們也支援修訂版本插入: 請按照下列操作說明:

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

      kubectl get deploy -n istio-system -l app=istiod -o \
         jsonpath={.items[*].metadata.labels.'istio\.io\/rev'}'{"\n"}'
      
    2. 將修訂版本標籤套用至命名空間。在下列指令中,REVISION_LABEL 代表您在上一步記下的 istiod 修訂版本標籤值。

      for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation shipping; do
        kubectl label namespace $ns \
            istio-injection- istio.io/rev=REVISION_LABEL --overwrite
      done;
      
  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
      

為服務通訊埠命名

如要納入 Cloud Service Mesh,服務通訊埠必須命名,且名稱必須包含通訊埠的通訊協定,例如:

apiVersion: v1
kind: Service
metadata:
  name: ratings
  labels:
    app: ratings
    service: ratings
spec:
  ports:
  - port: 9080
    name: http

服務通訊埠名稱可包含後置字串,語法如下: name: protocol[-suffix] 其中方括號表示選用的後置字串,開頭必須是破折號,例如:

kind: Service
metadata:
  name: myservice
spec:
  ports:
  - number: 3306
    name: mysql
  - number: 80
    name: http-web

如要在 Google Cloud 控制台中顯示指標,Service 通訊埠必須使用下列其中一種通訊協定命名:http、http2 或 grpc。以 https 通訊協定命名的 Service 通訊埠會視為 tcp,且不會顯示這些 Service 的指標。

插入 Sidecar Proxy

本節說明如何使用 Cloud Service Mesh 設定 Sidecar Proxy 插入功能,強化網路的安全、穩定和可觀測性。這些功能會從應用程式的主要容器中抽象化,並在通用的獨立處理程序 Proxy (sidecar) 中實作,以獨立容器的形式在同一個 Pod 中提供。您可以使用 Cloud Service Mesh 的功能,不必重新設計正式版應用程式,即可加入服務網格。

Cloud Service Mesh 偵測到您為工作負載 Pod 設定的命名空間標籤時,會自動插入 Sidecar Proxy (自動插入)。Proxy 會攔截所有傳入和傳出工作負載的流量,並與 Cloud Service Mesh 通訊。

啟用自動加入邊車容器

  1. 啟用命名空間,以便插入。步驟取決於控制層實作。

    代管 (TD)

    1. 將預設插入標籤套用至命名空間:
    kubectl label namespace NAMESPACE
        istio.io/rev- istio-injection=enabled --overwrite
    

    代管 (Istiod)

    建議做法:執行下列指令,將預設插入標籤套用至命名空間:

      kubectl label namespace NAMESPACE \
          istio.io/rev- istio-injection=enabled --overwrite
    

    如果您是使用代管 Istiod 控制層的現有使用者:建議您使用預設插入,但系統也支援修訂版本插入。請按照下列操作說明進行:

    1. 執行下列指令,找出可用的發布管道:

      kubectl -n istio-system get controlplanerevision
      

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

      NAME                AGE
      asm-managed-rapid   6d7h
      

      注意:如果上述清單顯示兩個控制層修訂版本,請移除其中一個。叢集不支援多個控制層管道。

      在輸出內容中,NAME 欄下方的值是修訂版本標籤,對應至 Cloud Service Mesh 版本的可用發布管道。

    2. 將修訂版本標籤套用至命名空間:

      kubectl label namespace NAMESPACE \
          istio-injection- istio.io/rev=REVISION_LABEL --overwrite
      

    叢集內

    建議做法:執行下列指令,將預設插入標籤套用至命名空間:

      kubectl label namespace NAMESPACE \
          istio.io/rev- istio-injection=enabled --overwrite
    

    建議您使用預設插入,但我們也支援修訂版本插入: 請按照下列操作說明:

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

      kubectl get deploy -n istio-system -l app=istiod -o \
         jsonpath={.items[*].metadata.labels.'istio\.io\/rev'}'{"\n"}'
      
    2. 將修訂版本標籤套用至命名空間。在下列指令中,REVISION_LABEL 代表您在上一步記下的 istiod 修訂版本標籤值。

      kubectl label namespace NAMESPACE \
          istio-injection- istio.io/rev=REVISION_LABEL --overwrite
      
  2. 按照下一節的步驟,重新啟動受影響的 Pod。

  3. 請按照下列方式為 demo 命名空間加上註解:

    kubectl annotate --overwrite namespace NAMESPACE \
    mesh.cloud.google.com/proxy='{"managed":"true"}'
    

重新啟動 Pod 以更新 Sidecar Proxy

使用自動加入邊車容器功能時,您可以重新啟動 Pod,更新現有 Pod 的邊車容器:

重新啟動 Pod 的方式取決於 Pod 是否為Deployment 的一部分。

  1. 如果您使用 Deployment,請重新啟動 Deployment,這會重新啟動所有含有 sidecar 的 Pod:

    kubectl rollout restart deployment -n NAMESPACE

    如果您未使用 Deployment,請刪除 Pod,系統會自動使用 sidecar 重新建立 Pod:

    kubectl delete pod -n NAMESPACE --all
  2. 確認命名空間中的所有 Pod 都已加入邊車容器:

    kubectl get pod -n NAMESPACE

    在下列前一個指令的輸出範例中,請注意 READY 欄指出每個工作負載都有兩個容器:主要容器和 Sidecar Proxy 的容器。

    NAME                    READY   STATUS    RESTARTS   AGE
    WORKLOAD           2/2     Running   0          20s
    ...