设置 TCP 服务

本指南介绍如何设置 Cloud Service Mesh 以使用 TCP 服务和 TCPRoute 资源。

使用 TCP 服务和 TCPRoute 的 Cloud Service Mesh 类似于包含 HTTP 服务的 Envoy 边车代理配置。例外情况是后端服务提供 TCP 服务,并且路由基于 TCP/IP 参数而不是 HTTP 协议。

使用 TCPRoute 资源的 Mesh 资源
使用 TCPRoute 资源的 Mesh 资源(点击可放大)

准备工作

请务必完成准备设置 Envoy 和无代理工作负载中所述的任务。

配置 Mesh 资源

  1. 在名为 mesh.yaml 的文件中,创建 mesh 资源规范。

    name: sidecar-mesh
    
  2. 使用 mesh.yaml 文件创建 mesh 资源。

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

配置 TCP 服务器

本指南的这一部分并不特定于新的 API,而是使用现有的后端服务、健康检查和 MIG 资源。

出于演示目的,您可以使用在端口 10000 上提供测试 TCP 服务的代管式实例组来创建具有自动扩缩虚拟机的后端服务。

  1. 使用端口 10000 上的测试服务来创建 Compute Engine 虚拟机实例模板。

    gcloud compute instance-templates create tcp-td-vm-template \
      --scopes=https://www.googleapis.com/auth/cloud-platform \
      --tags=allow-health-checks \
      --image-family=debian-10 \
      --image-project=debian-cloud \
      --metadata=startup-script="#! /bin/bash
    sudo apt-get update -y
    sudo apt-get install netcat -y
    while true;
      do echo 'Hello from TCP service' | nc -l -s 0.0.0.0 -p 10000;
    done &"
    
  2. 基于该模板创建一个代管式实例组。

    gcloud compute instance-groups managed create tcp-td-mig-us-east1 \
      --zone=ZONE \
      --size=1 \
      --template=tcp-td-vm-template
    
  3. 将创建的代管式实例组上的已命名端口设置为端口 10000。

    gcloud compute instance-groups set-named-ports tcp-td-mig-us-east1 
    --zone=ZONE
    --named-ports=tcp:10000

  4. 创建一项健康检查。

    gcloud compute health-checks create tcp tcp-helloworld-health-check --port 10000
    
  5. 创建防火墙规则以允许传入的健康检查关联到您网络中的实例。

    gcloud compute firewall-rules create tcp-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:10000
    
  6. 使用负载均衡方案 INTERNAL_SELF_MANAGED 创建全局后端服务,并将健康检查关联到后端服务。该示例使用运行您之前创建的示例 TCP 服务的代管式实例组。

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

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

使用 TCPRoute 设置路由

在本部分中,您将设置路由。

  1. 在名为 tcp_route.yaml 的文件中,创建 TcpRoute 规范。

    您可以使用 $PROJECT_ID$PROJECT_NUMBER

    name: helloworld-tcp-route
    meshes:
    - projects/$PROJECT_NUMBER/locations/global/meshes/sidecar-mesh
    rules:
    - action:
        destinations:
        - serviceName: projects/$PROJECT_NUMBER/locations/global/backendServices/tcp-helloworld-service
      matches:
      - address: '10.0.0.1/32'
        port: '10000'
    
  2. 使用 tcp_route.yaml 规范来创建 TcpRoute 资源。

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

使用 Envoy 边车创建 TCP 客户端

  1. 创建实例模板,然后使用 Envoy 创建一个连接到 Cloud Service Mesh 的虚拟机。

    gcloud beta compute instance-templates create td-vm-client-template \
      --image-family=debian-10 \
      --image-project=debian-cloud \
      --service-proxy=enabled,mesh=sidecar-mesh \
      --metadata=startup-script="#! /bin/bash
    sudo apt-get update -y
    sudo apt-get install netcat -y"
    
    gcloud compute instances create td-vm-client \
      --zone=ZONE \
      --source-instance-template td-vm-client-template
    
  2. 登录您创建的虚拟机。

    gcloud compute ssh td-vm-client
    
  3. 使用 netcat 实用程序验证您创建的测试服务连接。

    echo 'Hi TCP Service' | nc 10.0.0.1 10000
    

测试服务应返回短语 Hello from TCP service。您还应能够查看在远程虚拟机上运行的 netcat 服务所返回的任何文本。

后续步骤

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