設定無 Proxy gRPC 服務網格

本指南將說明如何設定無 Proxy gRPC 服務網格。

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

事前準備

請勿在已啟用 Envoy 補充資訊自動注入功能的命名空間中,建立無 Proxy gRPC 服務網格。如要判斷是否已啟用 sidecar 注入功能,請執行下列指令:

kubectl get namespace default --show-labels

如果已啟用側邊車自動插入功能,請執行下列指令來移除標籤:

kubectl label namespace default istio-injection-

部署 gRPC 服務

在本節中,您將部署 gRPC helloworld 範例服務。helloworld 範例服務是 gRPC 伺服器應用程式,會根據 gRPC 用戶端的要求傳回簡單訊息。helloworld 服務會在叢集中的通訊埠 8080 上公開 gRPC 服務。

  1. grpc-td-helloworld.yaml 檔案中,儲存以下內容:

    apiVersion: v1
    kind: Service
    metadata:
      name: helloworld
      namespace: default
    spec:
      ports:
      - port: 8080
        name: helloworld
        protocol: TCP
        targetPort: 50051
      selector:
        run: app1
      type: ClusterIP
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        run: app1
      name: app1
      namespace: default
    spec:
      selector:
        matchLabels:
          run: app1
      replicas: 2
      template:
        metadata:
          labels:
            run: app1
          annotations:
            sidecar.istio.io/inject: "false"
        spec:
          containers:
          - image: grpc/java-example-hostname:1.37.0
            name: app1
            ports:
            - protocol: TCP
              containerPort: 50051
    
  2. 套用檔案 grpc-td-helloworld.yaml

    kubectl apply -f grpc-td-helloworld.yaml
    
  3. 確認已建立新的 helloworld 服務:

    kubectl get svc -n default
    

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

    NAME           TYPE        CLUSTER-IP   EXTERNAL-IP   PORT (S)   AGE
    helloworld     ClusterIP   10.71.9.71   <none>        8080/TCP  41m
    [..skip..]
    
  4. 確認應用程式 Pod 是否正在執行:

    kubectl get pods -n default
    
  5. 輸出 Pod 應類似以下內容:

    NAME                        READY     STATUS    RESTARTS   AGE
    app1-6db459dcb9-zvfg2   1/1       Running   0          6m
    app1-6db459dcb9-hlvhj   1/1       Running   0          6m
    [..skip..]
    

設定 gRPC 服務網格

在本節中,您將設定簡單的 gRPC 服務網格。

  1. td-grpc-mesh.yaml 檔案中,儲存下列 mesh 資訊清單:

    kind: TDMesh
    apiVersion: net.gke.io/v1alpha1
    metadata:
      name: td-grpc-mesh
      namespace: default
    spec:
      gatewayClassName: gke-td
      allowedRoutes:
        namespaces:
          from: All
        kinds:
        - group: net.gke.io
          kind: TDGRPCRoute
    
  2. 將網格資訊清單套用至 gke-1

    kubectl apply -f td-grpc-mesh.yaml
    

    範例 TDMesh 只允許附加 TDGRPCRoute 資源。資訊清單會定義允許的路徑類型。

  3. 確認是否已建立新的 td-grpc-mesh 網格:

    kubectl describe tdmesh td-grpc-mesh -n default
    

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

    ...
    Status:
      Conditions:
        Last Transition Time:  2022-04-14T22:22:09Z
        Message:
        Reason:                MeshReady
        Status:                True
        Type:                  Ready
        Last Transition Time:  2022-04-14T22:21:41Z
        Message:
        Reason:                Scheduled
        Status:                True
        Type:                  Scheduled
    Events:
      Type    Reason  Age   From                Message
      ----    ------  ----  ----                -------
      Normal  ADD     79s   mc-mesh-controller  Processing mesh default/td-grpc-mesh
      Normal  UPDATE  79s   mc-mesh-controller  Processing mesh default/td-grpc-mesh
      Normal  SYNC    50s   mc-mesh-controller  SYNC on default/td-grpc-mesh was a success
    
  4. helloworld-route.yaml 檔案中儲存下列資訊清單:

    kind: TDGRPCRoute
    apiVersion: net.gke.io/v1alpha1
    metadata:
      name: helloworld-route
      namespace: default
    spec:
      parentRefs:
      - name: td-grpc-mesh
        namespace: default
        group: net.gke.io
        kind: TDMesh
      hostnames:
      - helloworld
      rules:
      - backendRefs:
        - name: helloworld
          port: 8080
          namespace: default
    
  5. helloworld-route.yaml 資訊清單套用至 gke-1

    kubectl apply -f helloworld-route.yaml
    
  6. 確認已建立新的 helloworld-route GRPCRoute 資源:

    kubectl get tdgrpcroute -n default
    

驗證設定

設定程序完成後,請確認您可以使用無 Proxy gRPC 用戶端存取 helloworld gRPC 伺服器。這個用戶端會連線至 Cloud Service Mesh,取得 helloworld 服務的相關資訊,並使用這些資訊將流量傳送至服務的後端。

在以下範例中,您會使用 grpcurl 工具,確認 Cloud Service Mesh 是否正確地在網格中轉送流量。您會建立用戶端 Pod,然後開啟殼層並在殼層中執行驗證指令。

設定環境變數和啟動檔案

  1. grpc-client.yaml 檔案中儲存以下 Pod 資訊清單:

    apiVersion: v1
    kind: Pod
    metadata:
      name: static-sleeper
      namespace: default
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      containers:
      - image: curlimages/curl:7.82.0
        imagePullPolicy: IfNotPresent
        name: sleeper
        command:
        - sleep
        - 365d
        env:
        - name: GRPC_XDS_BOOTSTRAP
          value: "/tmp/grpc-xds/td-grpc-bootstrap.json"
        volumeMounts:
        - name: grpc-td-conf
          mountPath: /tmp/grpc-xds/
      initContainers:
      - args:
        - --config-mesh-experimental
        - "gketd-td-grpc-mesh"
        - --output
        - "/tmp/bootstrap/td-grpc-bootstrap.json"
        image: gcr.io/trafficdirector-prod/td-grpc-bootstrap:0.16.0
        imagePullPolicy: IfNotPresent
        name: grpc-td-init
        volumeMounts:
        - name: grpc-td-conf
          mountPath: /tmp/bootstrap/
      volumes:
      - name: grpc-td-conf
        emptyDir:
          medium: Memory
    
  2. gke-1 中套用 Pod 資訊清單:

    kubectl apply -f grpc-client.yaml
    
  3. Pod 準備就緒後,請開啟一個通往用戶端 Pod 的殼層:

    kubectl exec -it static-sleeper -- /bin/sh
    

您可以使用 grpcurl 工具做為無 Proxy gRPC 用戶端。grpcurl 工具會使用環境變數和 Bootstrap 資訊,連線至 Cloud Service Mesh。接著,這項工具會瞭解使用 Cloud Service Mesh 設定的 helloworld 服務。

如要使用 grpcurl 工具驗證設定,請按照下列步驟操作:

  1. 下載並安裝 grpcurl 工具:

    cd /home/curl_user
    curl -L https://github.com/fullstorydev/grpcurl/releases/download/v1.8.1/grpcurl_1.8.1_linux_x86_64.tar.gz | tar -xz
    
  2. 請使用 xds:///helloworld 做為服務 URI,並將 helloworld.Greeter/SayHello 做為要叫用的服務名稱和方法,執行 grpcurl 工具。SayHello 方法的參數會透過 -d 選項傳遞:

    ./grpcurl --plaintext \
      -d '{"name": "world"}' \
      xds:///helloworld helloworld.Greeter/SayHello
    

    輸出結果會與下列內容相似,其中 INSTANCE_HOST_NAME 是 Pod 的主機名稱:

    Greetings: Hello world, from INSTANCE_HOST_NAME
    

輸出內容會驗證無 Proxy gRPC 用戶端是否已成功連線至 Cloud Service Mesh,並使用 xds 名稱解析工具瞭解 helloworld 服務的後端。用戶端向其中一項服務的後端傳送要求,不必知道 IP 位址或執行 DNS 解析。

後續步驟