設定 Envoy 補充服務網格

搶先體驗方案客戶可使用這項設定,但我們不建議新 Cloud Service Mesh 使用者採用。詳情請參閱 Cloud Service Mesh 總覽

本指南說明如何在 Fleet 中設定簡單的服務中介網。指南包含下列步驟:

  • 將 Envoy 附加元件注入器部署至叢集。Injector 會將 Envoy Proxy 容器插入應用程式 Pod。
  • 部署 Gateway API 資源,設定服務中繼網格中的 Envoy 補充,將要求轉送至命名空間 store 中的範例服務。
  • 部署簡單的用戶端來驗證部署作業。

下圖顯示已設定的服務網格。

機群中的 Envoy 補充服務網格
Fleet 中的 Envoy 附加服務網格 (按一下可放大)

您只能在叢集中設定一個 Mesh,因為側載注入器設定中的網格名稱和 Mesh 資源名稱必須相同。

部署 Envoy 補充注入器

如要部署補充注入器,請按照下列步驟操作:

  1. 設定專案資訊

    # The project that contains your GKE cluster.
    export CLUSTER_PROJECT_ID=YOUR_CLUSTER_PROJECT_NUMBER_HERE
    # The name of your GKE cluster.
    export CLUSTER=YOUR_CLUSTER_NAME
    # The channel of your GKE cluster. Eg: rapid, regular, stable.
    export CHANNEL=YOUR_CLUSTER_CHANNEL
    # The location of your GKE cluster, Eg: us-central1 for regional GKE cluster,
    # us-central1-a for zonal GKE cluster
    export LOCATION=ZONE
    
    # The mesh name of the traffic director load balancing API.
    export MESH_NAME=YOUR_MESH_NAME
    # The project that holds the mesh resources.
    export MESH_PROJECT_NUMBER=YOUR_PROJECT_NUMBER_HERE
    
    export TARGET=projects/${MESH_PROJECT_NUMBER}/locations/global/meshes/${MESH_NAME}
    
    gcloud config set project ${CLUSTER_PROJECT_ID}
    

    如要找出 MESH_NAME,請按照下列方式指派值,其中 MESH_NAMEMesh 資源規格中 metadata.name 欄位的值:

    gketd-MESH_NAME
    

    舉例來說,如果 Mesh 資源中的 metadata.name 值為 butterfly-mesh,請將 MESH_NAME 的值設為以下值:

    export MESH_NAME="gketd-butterfly-mesh"
    
  2. 套用 Mutating Webhook 的設定

    以下各節提供將 MutatingWebhookConfiguration 套用至叢集的操作說明。建立 Pod 時,系統會叫用叢集內的許可控制器。存取控制器會與受管理的附屬元件插入器通訊,將 Envoy 容器新增至 Pod。

    將下列變異 webhook 設定套用至叢集。

    cat <<EOF | kubectl apply -f -
    apiVersion: admissionregistration.k8s.io/v1
    kind: MutatingWebhookConfiguration
    metadata:
      labels:
        app: sidecar-injector
      name: td-mutating-webhook
    webhooks:
    - admissionReviewVersions:
      - v1beta1
      - v1
      clientConfig:
        url: https://meshconfig.googleapis.com/v1internal/projects/${CLUSTER_PROJECT_ID}/locations/${LOCATION}/clusters/${CLUSTER}/channels/${CHANNEL}/targets/${TARGET}:tdInject
      failurePolicy: Fail
      matchPolicy: Exact
      name: namespace.sidecar-injector.csm.io
      namespaceSelector:
        matchExpressions:
        - key: td-injection
          operator: Exists
      reinvocationPolicy: Never
      rules:
      - apiGroups:
        - ""
        apiVersions:
        - v1
        operations:
        - CREATE
        resources:
        - pods
        scope: '*'
      sideEffects: None
      timeoutSeconds: 30
    EOF
    

    如果您需要自訂 sidecar 插入器,請按照下列步驟為叢集自訂 sidecar 插入器:

  3. 為側載注入器設定 TLS。

  4. 啟用補充物注入功能。

  5. 自動 Envoy 插入選項

部署 store 服務

在本節中,您會在網格中部署 store 服務。

  1. store.yaml 檔案中,儲存下列資訊清單:

    kind: Namespace
    apiVersion: v1
    metadata:
      name: store
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: store
      namespace: store
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: store
          version: v1
      template:
        metadata:
          labels:
            app: store
            version: v1
        spec:
          containers:
          - name: whereami
            image: us-docker.pkg.dev/google-samples/containers/gke/whereami:v1
            ports:
            - containerPort: 8080
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: store
      namespace: store
    spec:
      selector:
        app: store
      ports:
      - port: 8080
        targetPort: 8080
    
  2. 將資訊清單套用至 gke-1

    kubectl apply -f store.yaml
    

建立服務網格

  1. mesh.yaml 檔案中,儲存下列 mesh 資訊清單。mesh 資源的名稱必須與注入器 configmap 中指定的網格名稱相符。在這個範例設定中,兩個位置都使用 td-mesh 這個名稱:

    apiVersion: net.gke.io/v1alpha1
    kind: TDMesh
    metadata:
      name: td-mesh
      namespace: default
    spec:
      gatewayClassName: gke-td
      allowedRoutes:
        namespaces:
          from: All
    
  2. mesh 資訊清單套用至 gke-1,這會建立名為 td-mesh 的邏輯網格:

    kubectl apply -f mesh.yaml
    
  3. store-route.yaml 檔案中,儲存下列 HTTPRoute 資訊清單。資訊清單定義了 HTTPRoute 資源,可將指定主機名稱 example.com 的 HTTP 流量,轉送至命名空間 store 中的 Kubernetes 服務 store

    apiVersion: gateway.networking.k8s.io/v1alpha2
    kind: HTTPRoute
    metadata:
      name: store-route
      namespace: store
    spec:
      parentRefs:
      - name: td-mesh
        namespace: default
        group: net.gke.io
        kind: TDMesh
      hostnames:
      - "example.com"
      rules:
      - backendRefs:
        - name: store
          namespace: store
          port: 8080
    
  4. 將路徑資訊清單套用至 gke-1

    kubectl apply -f store-route.yaml
    

驗證部署作業

  1. 檢查 Mesh 狀態和事件,驗證 MeshHTTPRoute 資源是否已成功部署:

    kubectl describe tdmesh td-mesh
    

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

    ...
    
    Status:
      Conditions:
        Last Transition Time:  2022-04-14T22:08:39Z
        Message:
        Reason:                MeshReady
        Status:                True
        Type:                  Ready
        Last Transition Time:  2022-04-14T22:08:28Z
        Message:
        Reason:                Scheduled
        Status:                True
        Type:                  Scheduled
    Events:
      Type    Reason  Age   From                Message
      ----    ------  ----  ----                -------
      Normal  ADD     36s   mc-mesh-controller  Processing mesh default/td-mesh
      Normal  UPDATE  35s   mc-mesh-controller  Processing mesh default/td-mesh
      Normal  SYNC    24s   mc-mesh-controller  SYNC on default/td-mesh was a success
    
  2. 如要確保在預設命名空間中啟用附屬物注入功能,請執行下列指令:

    kubectl get namespace default --show-labels
    

    如果已啟用補充容器插入功能,輸出內容會顯示以下內容:

    istio-injection=enabled
    

    如果未啟用補充容器注入功能,請參閱「啟用補充容器注入功能」。

  3. 如要驗證部署作業,請部署用戶端 Pod,做為先前定義的 store 服務的用戶端。在 client.yaml 檔案中,儲存下列內容:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        run: client
      name: client
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          run: client
      template:
        metadata:
          labels:
            run: client
        spec:
          containers:
          - name: client
            image: curlimages/curl
            command:
            - sh
            - -c
            - while true; do sleep 1; done
    
  4. 部署規格:

    kubectl apply -f client.yaml
    

    在叢集中執行的補充注入器會自動將 Envoy 容器注入用戶端 Pod。

  5. 如要確認 Envoy 容器是否已插入,請執行下列指令:

    kubectl describe pods -l run=client
    

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

    ...
    Init Containers:
      # Istio-init sets up traffic interception for the Pod.
      istio-init:
    ...
      # td-bootstrap-writer generates the Envoy bootstrap file for the Envoy container
      td-bootstrap-writer:
    ...
    Containers:
    # client is the client container that runs application code.
      client:
    ...
    # Envoy is the container that runs the injected Envoy proxy.
      envoy:
    ...
    

在用戶端 Pod 佈建完成後,請從用戶端 Pod 傳送要求給 store 服務。

  1. 取得用戶端 Pod 的名稱:

    CLIENT_POD=$(kubectl get pod -l run=client -o=jsonpath='{.items[0].metadata.name}')
    
    # The VIP where the following request will be sent. Because all requests
    # from the client container are redirected to the Envoy proxy sidecar, you
    # can use any IP address, including 10.0.0.2, 192.168.0.1, and others.
    VIP='10.0.0.1'
    
  2. 傳送要求至儲存服務,並輸出回應標頭:

    TEST_CMD="curl -v -H 'host: example.com' $VIP"
    
  3. 在用戶端容器中執行測試指令:

    kubectl exec -it $CLIENT_POD -c client -- /bin/sh -c "$TEST_CMD"
    

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

    < Trying 10.0.0.1:80...
    < Connected to 10.0.0.1 (10.0.0.1) port 80 (#0)
    < GET / HTTP/1.1
    < Host: example.com
    < User-Agent: curl/7.82.0-DEV
    < Accept: */*
    <
    < Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < content-type: application/json
    < content-length: 318
    < access-control-allow-origin: *
    < server: envoy
    < date: Tue, 12 Apr 2022 22:30:13 GMT
    <
    {
      "cluster_name": "gke-1",
      "zone": "us-west1-a",
      "host_header": "example.com",
      ...
    }
    

後續步驟