从其他 VPC 网络访问服务

本教程介绍如何以服务使用方的身份通过创建 Private Service Connect 端点来访问已发布的服务。当您向端点发送请求时,Private Service Connect 会将这些请求转发给已发布的服务。

本教程面向云架构师、网络架构师、网络管理员和 IT 管理员。

目标

  • 为服务使用方资源配置网络
  • 创建端点
  • 测试访问端点

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

如需根据您的预计使用量来估算费用,请使用价格计算器

新 Google Cloud 用户可能有资格申请免费试用

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理

准备工作

  1. 完成本系列中的前两个教程:
  2. 创建或选择一个项目以用于服务使用方资源。 此项目称为 CONSUMER_PROJECT
    1. In the Google Cloud console, go to the project selector page.

      Go to project selector

    2. Select or create a Google Cloud project.

      Roles required to select or create a project

      • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
      • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
  3. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  4. Make sure that you have the following role or roles on the project: Compute Engine > Compute Network Admin, Compute Engine > Compute Instance Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 点击选择角色,然后搜索相应角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。

配置网络

以下部分介绍了如何创建网络和子网来托管端点,以及如何创建防火墙规则以允许对用于测试端点的客户端虚拟机进行 SSH 访问。

创建网络和子网

如需创建服务使用方网络和子网,请按照以下步骤操作。

控制台

  1. 在 Google Cloud 控制台中,前往 VPC 网络页面。

    进入 VPC 网络页面

  2. 点击创建 VPC 网络

  3. 对于名称,输入 consumer-network

  4. 对于子网创建模式,选择自定义

  5. 新子网部分中,输入以下信息。

    1. 对于名称,输入 consumer-subnet
    2. 区域部分中,选择与您创建的已发布服务相同的区域。
    3. IP 栈类型字段中,选择 IPv4(单栈)
    4. 对于 IPv4 范围,输入 192.168.10.0/24
    5. 点击完成
  6. 点击创建

gcloud

  1. 创建一个自定义模式 VPC 网络:

    gcloud compute networks create consumer-network --subnet-mode=custom
    
  2. consumer-network 网络中,创建一个子网。

    gcloud compute networks subnets create consumer-subnet \
        --network=consumer-network \
        --range=192.168.10.0/24 \
        --region=REGION
    

    REGION 替换为您创建的已发布服务所在的同一区域。

配置防火墙规则

创建名为 fw-allow-ssh 的防火墙规则,以允许来自 0.0.0.0/0 的 SSH 流量到达具有 allow-ssh 网络标记的虚拟机。

控制台

  1. 在 Google Cloud 控制台中,前往防火墙政策页面。

    前往“防火墙政策”

  2. 如需允许传入 SSH 连接,请点击创建防火墙规则,然后使用以下设置:

    • 对于名称,输入 fw-allow-ssh
    • 对于网络,请选择 consumer-network
    • 优先级字段中,输入 1000
    • 对于流量方向,选择入站
    • 对于对匹配项执行的操作,选择允许
    • 对于目标,选择 Specified target tags
    • 对于目标标记,请输入 allow-ssh
    • 对于来源过滤条件,选择 IPv4 范围
    • 对于来源 IPv4 范围,输入 0.0.0.0/0
    • 协议和端口部分,选择指定的协议和端口,然后选中 TCP 复选框。对于端口,输入 22
  3. 点击创建

gcloud

  1. 创建 fw-allow-ssh 防火墙规则,允许通过 SSH 连接到网络标记为 allow-ssh 的虚拟机。

    gcloud compute firewall-rules create fw-allow-ssh \
        --network=consumer-network \
        --action=allow \
        --direction=ingress \
        --source-ranges=0.0.0.0/0 \
        --target-tags=allow-ssh \
        --rules=tcp:22
    

创建端点

创建指向您创建的已发布服务的端点。

控制台

  1. 在 Google Cloud 控制台中,前往 Private Service Connect 页面。

    转到 Private Service Connect

  2. 点击已连接的端点标签页。

  3. 点击连接端点

  4. 对于目标,选择已发布的服务

  5. 对于目标服务,请输入 projects/PRODUCER_PROJECT/regions/REGION/serviceAttachments/published-service

    PRODUCER_PROJECT 替换为已发布服务的项目 ID。将 REGION 替换为已发布服务的区域。

  6. 对于端点名称,输入 ep-1

  7. 对于网络,请选择 consumer-network

  8. 对于子网,选择 consumer-subnet

  9. 点击 IP 地址下拉菜单,然后选择创建 IP 地址

    1. 对于名称,输入 ep-ip-1
    2. 对于静态 IP 地址,选择让我选择
    3. 对于自定义 IP 地址,输入 192.168.10.5
    4. 点击保留
  10. 点击添加端点

gcloud

  1. 预留要分配给该端点的内部 IP 地址。

    gcloud compute addresses create ep-ip-1 \
        --region=REGION \
        --subnet=consumer-subnet \
        --addresses=192.168.10.5
    

    REGION 替换为您创建的已发布服务所在的同一区域。

  2. 创建转发规则,以将该端点连接到服务提供方的服务连接。

    gcloud compute forwarding-rules create ep-1 \
        --region=REGION \
        --network=consumer-network \
        --address=ep-ip-1 \
        --target-service-attachment=projects/PRODUCER_PROJECT/regions/REGION/serviceAttachments/published-service
    

    替换以下内容:

    • PRODUCER_PROJECT:服务提供方项目的项目 ID。

    • REGION:服务连接的区域。

测试访问端点

如需测试端点和已发布的服务是否正常运行,请创建一个测试虚拟机,并从该虚拟机向端点发送请求。

创建客户端虚拟机以进行测试

在端点所在的区域中创建客户端虚拟机。

控制台

  1. 在 Google Cloud 控制台中,前往虚拟机实例页面。

    转到虚拟机实例

  2. 点击创建实例

  3. 对于名称,输入 consumer-test

  4. 对于区域,选择与后端虚拟机相同的区域。

  5. 地区部分,选择该区域内的一个地区。

  6. 点击网络并配置以下字段:

    1. 对于网络标记,请输入 allow-ssh
    2. 对于网络接口,请选择以下内容:
      • 对于网络,请选择 consumer-network
      • 对于子网,请选择 consumer-subnet
  7. 点击创建

gcloud

gcloud compute instances create consumer-test \
    --zone=ZONE \
    --image-family=debian-12 \
    --image-project=debian-cloud \
    --tags=allow-ssh \
    --subnet=consumer-subnet

ZONE 替换为与后端虚拟机位于同一区域的可用区。

测试连接性

此测试从客户端虚拟机联系端点。预期行为是流量分布在负载均衡器的后端虚拟机中。

  1. 连接到客户端虚拟机实例。
    gcloud compute ssh consumer-test --zone=ZONE
    
    ZONE 替换为客户端虚拟机的可用区。
  2. 使用 curl 连接端点的 IP 地址,通过这种方式向端点发送 Web 请求。重复该请求,您可以看到响应来自不同的后端虚拟机。根据每个后端虚拟机上 /var/www/html/index.html 的内容,生成响应的虚拟机的名称会显示在 HTML 响应的文本中。例如,预期响应如 Page served from: vm-1Page served from: vm-2 所示。
    curl -s http://192.168.10.5
    

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

同时删除生产者项目 (PRODUCER_PROJECT) 和消费者项目 (CONSUMER_PROJECT)。

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

后续步骤