设置无代理 gRPC 服务

本指南演示了如何使用 MeshGRPCRoute 资源设置无代理 gRPC 服务网格。

使用 GRPCRoute 和 Mesh 资源的无代理 gRPC 服务
使用 GRPCRouteMesh 资源的无代理 gRPC 服务(点击可放大)

准备工作

请务必阅读准备设置 Envoy 和无代理工作负载,并满足所述的前提条件 文档。

配置 Mesh 资源

当无代理 gRPC 应用连接到 xds://hostname 时,gRPC 会 客户端库与 Cloud Service Mesh 建立连接。客户端库使用连接来获取路由主机名请求所需的路由配置。

请务必记下 Mesh 资源的名称, 无代理 gRPC 应用用于请求与 这个网格

  1. 创建 Mesh 规范并将其保存在名为 mesh.yaml 的文件中。

    name: grpc-mesh
    
  2. 使用 mesh.yaml 规范创建 Mesh 资源:

    gcloud network-services meshes import grpc-mesh \
      --source=mesh.yaml \
      --location=global
    

创建 Mesh 资源后,Cloud Service Mesh 即可传送 但由于尚未定义任何服务,因此该配置 为空。在下一部分中,您将定义这些服务,并将其关联到 Mesh 资源。

配置 gRPC 服务器

出于演示目的,您可以在代管式实例组中创建包含自动扩缩虚拟机的后端服务。虚拟机在端口 50051 上使用 gRPC 协议提供短语 hello world

  1. 使用端口 50051 上公开的 helloworld gRPC 服务创建 Compute Engine 虚拟机实例模板:

    gcloud compute instance-templates create grpc-td-vm-template \
       --scopes=https://www.googleapis.com/auth/cloud-platform \
       --tags=allow-health-checks \
       --image-family=debian-10 \
       --image-project=debian-cloud \
       --metadata-from-file=startup-script=<(echo '#! /bin/bash
     set -e
     cd /root
     sudo apt-get update -y
     sudo apt-get install -y openjdk-11-jdk-headless
     curl -L https://github.com/grpc/grpc-java/archive/v1.38.0.tar.gz | tar -xz
     cd grpc-java-1.38.0/examples/example-hostname
     ../gradlew --no-daemon installDist
     sudo systemd-run ./build/install/hostname-server/bin/hostname-server')
     
  2. 基于该模板创建一个代管式实例组:

    gcloud compute instance-groups managed create grpc-td-mig-us-east1 \
      --zone=ZONE \
      --size=2 \
      --template=grpc-td-vm-template
    
  3. 为 gRPC 服务创建已命名端口。已命名端口是 gRPC 服务在其中侦听请求的端口。在以下示例中,已命名端口为 50051

    gcloud compute instance-groups set-named-ports grpc-td-mig-us-east1 \
     --named-ports=grpc-helloworld-port:50051 \
     --zone=ZONE
    
  4. 创建 gRPC 健康检查。这些服务必须实现 gRPC 健康检查协议 确保 gRPC 健康检查正常运行如需了解详情,请参阅健康检查

    gcloud compute health-checks create grpc grpc-helloworld-health-check \
      --use-serving-port
    
  5. 创建防火墙规则以允许健康检查关联到网络中的实例:

    gcloud compute firewall-rules create grpc-vm-allow-health-checks \
      --network=default \
      --action=ALLOW \
      --direction=INGRESS \
      --source-ranges=35.191.0.0/16,130.211.0.0/22 \
      --target-tags allow-health-checks \
      --rules=tcp:50051
    
  6. 使用负载均衡方案 INTERNAL_SELF_MANAGED 创建全局后端服务,并将健康检查添加到后端服务。此处指定的端口用于连接到代管式实例组内的虚拟机。

    gcloud compute backend-services create grpc-helloworld-service \
      --global \
      --load-balancing-scheme=INTERNAL_SELF_MANAGED \
      --protocol=GRPC \
      --port-name=grpc-helloworld-port \
      --health-checks grpc-helloworld-health-check
    
  7. 将代管实例组添加到后端服务。

    gcloud compute backend-services add-backend \
      grpc-helloworld-service \
      --instance-group=grpc-td-mig-us-east1 \
      --instance-group-zone=ZONE \
      --global
    

已配置 Mesh 资源和服务。在下一部分中,您将设置路由。

使用 GRPCRoute 设置路由

请按照以下说明设置路由。

  1. 创建 GRPCRoute 规范并将其保存到名为 grpc_route.yaml 的文件中。

    您可以使用 PROJECT_IDPROJECT_NUMBER

    name: helloworld-grpc-route
    hostnames:
    - helloworld-gce
    meshes:
    - projects/PROJECT_NUMBER/locations/global/meshes/grpc-mesh
    rules:
    - action:
        destinations:
        - serviceName: projects/PROJECT_NUMBER/locations/global/backendServices/grpc-helloworld-service
    
  2. 使用 grpc_route.yaml 规范创建 GrpcRoute 资源:

    gcloud network-services grpc-routes import helloworld-grpc-route \
      --source=grpc_route.yaml \
      --location=global
    

Cloud Service Mesh 现已配置为对服务的流量进行负载均衡 在托管实例的各个后端的 GRPCRoute 资源中指定 。

创建 gRPC 客户端

您可以通过实例化无代理 gRPC 来验证配置 并将其连接到 Cloud Service Mesh。在其引导文件中,应用必须指定 Mesh 中指示的 VPC 网络。

配置完成后,应用可以使用 xds:///helloworld-gce 服务 URI 向与 helloworld-gce 关联的实例或端点发送请求。

在以下示例中,您将使用 grpcurl 工具测试 gRPC 服务。

  1. 创建客户端虚拟机。

    gcloud compute instances create grpc-client \
      --zone=ZONE\
      --scopes=https://www.googleapis.com/auth/cloud-platform \
      --image-family=debian-10 \
      --image-project=debian-cloud \
      --metadata-from-file=startup-script=<(echo '#! /bin/bash
    set -e
    export GRPC_XDS_BOOTSTRAP=/run/td-grpc-bootstrap.json
    echo export GRPC_XDS_BOOTSTRAP=$GRPC_XDS_BOOTSTRAP | sudo tee /etc/profile.d/grpc-xds-bootstrap.sh
    curl -L https://storage.googleapis.com/traffic-director/td-grpc-bootstrap-0.14.0.tar.gz | tar -xz
    ./td-grpc-bootstrap-0.14.0/td-grpc-bootstrap --config-mesh-experimental grpc-mesh | tee $GRPC_XDS_BOOTSTRAP')
    

设置引导文件

客户端应用必须具有引导配置文件。上一部分中的启动脚本会设置 GRPC_XDS_BOOTSTRAP 环境变量,并使用帮助程序脚本生成引导文件。所生成引导文件中 TRAFFICDIRECTOR_GCP_PROJECT_NUMBER 和 zone 的值将从知道虚拟机实例这些详细信息的元数据服务器获取。您可以使用 --gcp-project-number 选项手动将这些值提供给帮助程序脚本。您必须使用 --config-mesh-experimental 选项提供与 Mesh 资源匹配的 Mesh 名称。

要验证配置,请登录客户端虚拟机并运行 关注。

  1. 通过 SSH 连接到客户端虚拟机。

    gcloud compute ssh grpc-client
    
  2. 下载并安装 grpcurl 工具。

    curl -L https://github.com/fullstorydev/grpcurl/releases/download/v1.8.1/grpcurl_1.8.1_linux_x86_64.tar.gz | tar -xz
    
  3. 运行 grpcurl 工具,将 xds:///helloworld-gce 作为服务 URI,将 helloworld.Greeter/SayHello 作为服务名称和要调用的方法。使用 -d 选项传递 SayHello 方法的参数。

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

您应该会看到如下所示的输出,其中 INSTANCE_HOSTNAME 是虚拟机实例的名称:

   Greeting: Hello world, from INSTANCE_HOSTNAME
   

输出结果会验证无代理 gRPC 客户端是否已成功连接到 Cloud Service Mesh 并已了解 使用 xds 名称解析器的 helloworld-gce 服务。 客户端向服务的其中一个后端发送请求,无需了解 IP 地址或执行 DNS 解析。

后续步骤

  • 如需了解如何列出与 MeshGateway 资源,请参阅列出 Route 资源。 此功能处于预览阶段。