1. 简介
借助 Private Service Connect,服务提供方可以通过服务连接在 VPC 环境中公开服务,并允许其他 VPC 环境中的使用方通过 Private Service Connect 端点访问这些服务。有时,这些生产者服务会设计为虚拟机集群,每个虚拟机在相同的端口号上公开相同的服务。以前,这些服务设计需要在使用方部署多个 Private Service Connect 端点,或者在提供方使用 IP 转发,以确保定位到正确的提供方虚拟机。
Private Service Connect 现在可以使用端口映射以原生方式定位到正确的目标。在本实验中,您将了解需要使用此功能的用例,以及如何将端口映射 NEG 部署到 Private Service Connect 工作负载中。
学习内容
- Private Service Connect 端口映射用例
- PSC 端口映射的主要优势
- 网络要求
- 使用端口映射创建 Private Service Connect 提供方服务。
- 创建 Private Service Connect 端点
- 通过 Private Service Connect 端点向提供方服务发出调用
所需条件
- 具有所有者权限的 Google Cloud 项目
2. Private Service Connect 端口映射用例
端口映射功能使用专门针对 PSC 用例的端口映射 NEG(网络端点组)。
最常见的可以受益于使用端口映射的生产方类型是 NoSQL 数据库生产方和 Kafka 生产方。不过,如果生产者需要一组虚拟机在相同的端口上公开相同的服务,并且具有特定的虚拟机映射要求,则可以使用此功能。
提供方定义客户端端口与提供方虚拟机 + 目标端口之间的映射。然后,提供方需要与使用方共享此信息。使用方使用预定义的端口来唯一标识他们需要连接到的提供方虚拟机 + 目标端口。使用方使用的端口与提供方使用的端口不同。
PSC 端口映射的主要优势
- 简单:提供方部署具有端口映射的 PSC 组件,使用方部署 PSC 端点。PSC 会自动处理网络地址转换。
- 经济高效:无需额外的 PSC 资源或提供方虚拟机 CPU 周期。价格与其他类型的 PSC 部署相同
- 高性能:端口映射可提供与其他 PSC 模式相同的线路速率吞吐量和低延迟
- 可扩缩且 IP 高效:使用方 VPC 中的一个 IP 地址最多可以访问 1,000 个提供方虚拟机和 1,000 个端口映射
3. 网络要求
- 端口映射需要使用内部网络直通负载平衡器作为生产者负载平衡器。
- 只有 PSC 端点可以与端口映射搭配使用(而非 PSC 后端或 PSC 接口)。
- 端口映射 NEG 是区域性结构。
- 端口映射 NEG 只能在 PSC 连接中使用。如果客户端虚拟机直接调用生产者负载平衡器转发规则,这些规则将不起作用。这反映在本 Codelab 中对提供方服务的测试方式中。
- PSC 端点和提供方服务堆栈必须位于不同的 VPC 中。
4. Codelab 拓扑
在生产者 VPC 中,系统会创建两个虚拟机,每个虚拟机上都运行两个 Web 服务器,一个在端口 1000 上运行,另一个在端口 2000 上运行。我们将先测试每项服务,然后再设置 Portmap NEG、内部网络直通负载平衡器和服务附加项。
在使用方 VPC 中,我们将设置 PSC 端点,并通过客户端虚拟机测试与提供方服务的连接。
5. 设置和要求
自定进度的环境设置
- 登录 Google Cloud 控制台,然后创建一个新项目或重复使用现有项目。如果您还没有 Gmail 或 Google Workspace 账号,则必须创建一个。
- 项目名称是此项目参与者的显示名称。它是 Google API 尚未使用的字符串。您可以随时对其进行更新。
- 项目 ID 在所有 Google Cloud 项目中是唯一的,并且是不可变的(一经设置便无法更改)。Cloud 控制台会自动生成一个唯一字符串;通常情况下,您无需关注该字符串。在大多数 Codelab 中,您都需要引用项目 ID(通常用
PROJECT_ID
标识)。如果您不喜欢生成的 ID,可以再随机生成一个 ID。或者,您也可以尝试自己的项目 ID,看看是否可用。完成此步骤后便无法更改该 ID,并且此 ID 在项目期间会一直保留。 - 此外,还有第三个值,即部分 API 使用的项目编号,供您参考。如需详细了解所有这三个值,请参阅文档。
- 接下来,您需要在 Cloud 控制台中启用结算功能,以便使用 Cloud 资源/API。运行此 Codelab 应该不会产生太多的费用(如果有的话)。若要关闭资源以避免产生超出本教程范围的结算费用,您可以删除自己创建的资源或删除项目。Google Cloud 新用户符合参与 300 美元免费试用计划的条件。
启动 Cloud Shell
虽然可以通过笔记本电脑对 Google Cloud 进行远程操作,但在此 Codelab 中,您将使用 Google Cloud Shell,这是一个在云端运行的命令行环境。
在 Google Cloud 控制台 中,点击右上角工具栏中的 Cloud Shell 图标:
预配和连接到环境应该只需要片刻时间。完成后,您应该会看到如下内容:
这个虚拟机已加载了您需要的所有开发工具。它提供了一个持久的 5 GB 主目录,并且在 Google Cloud 中运行,大大增强了网络性能和身份验证功能。您在此 Codelab 中的所有工作都可以在浏览器中完成。您无需安装任何程序。
6. 准备工作
启用 API
在 Cloud Shell 中,确保项目 ID 已设置
gcloud config list project gcloud config set project [YOUR-PROJECT-ID] project=[YOUR-PROJECT-ID] region=us-central1 zone=us-central1-a echo $project echo $region echo $zone
启用所有必要的服务
gcloud services enable compute.googleapis.com
7. 创建提供方 VPC 网络
VPC 网络
通过 Cloud Shell
gcloud compute networks create producer-vpc --subnet-mode custom
创建子网
通过 Cloud Shell
gcloud compute networks subnets create producer-service-subnet --network producer-vpc --range 10.0.0.0/24 --region $region --enable-private-ip-google-access gcloud compute networks subnets create psc-nat-subnet --network producer-vpc --range 10.100.100.0/24 --region $region --purpose=PRIVATE_SERVICE_CONNECT
PSC 子网将与 PSC 服务连接相关联,以实现网络地址转换。对于生产用例,此子网的大小需要适当调整,以支持来自所有已附加 PSC 端点的入站流量。如需了解详情,请参阅 PSC NAT 子网大小调整文档。
创建网络防火墙政策和防火墙规则
通过 Cloud Shell
gcloud compute network-firewall-policies create producer-vpc-policy --global gcloud compute network-firewall-policies associations create --firewall-policy producer-vpc-policy --network producer-vpc --name network-producer-vpc --global-firewall-policy
如需允许 IAP 连接到您的虚拟机实例,请创建一个防火墙规则,该规则:
- 适用于您希望使用 IAP 可访问的所有 VM 实例。
- 允许来自 IP 地址范围 35.235.240.0/20 的入站流量。此范围包含 IAP 用于 TCP 转发的所有 IP 地址。
通过 Cloud Shell
gcloud compute network-firewall-policies rules create 1000 --action ALLOW --firewall-policy producer-vpc-policy --description "SSH with IAP" --direction INGRESS --src-ip-ranges 35.235.240.0/20 --layer4-configs tcp:22 --global-firewall-policy
以下防火墙规则允许从 PSC 子网的 TCP 端口 1000-2000 将流量传输到网络中的所有实例。在生产环境中,此防火墙规则应仅限于与特定生产方服务关联的实例。
通过 Cloud Shell
gcloud compute network-firewall-policies rules create 2000 --action ALLOW --firewall-policy producer-vpc-policy --description "allow traffic from PSC NAT subnet" --direction INGRESS --src-ip-ranges 10.100.100.0/24 --layer4-configs tcp:1000-2000 --global-firewall-policy
以下防火墙规则允许服务子网中的所有流量通过 TCP 端口 1000-2000 进行传输。此规则将用于测试我们的提供方服务是否正常运行。
通过 Cloud Shell
gcloud compute network-firewall-policies rules create 2001 --action ALLOW --firewall-policy producer-vpc-policy --description "allow traffic within the service subnet" --direction INGRESS --src-ip-ranges 10.0.0.0/24 --layer4-configs tcp:1000-2000 --global-firewall-policy
创建和配置生产者虚拟机
创建虚拟机
通过 Cloud Shell
gcloud compute instances create portmap-vm1 --zone=$zone --subnet=producer-service-subnet --no-address gcloud compute instances create portmap-vm2 --zone=$zone --subnet=producer-service-subnet --no-address gcloud compute instances create test-client-vm --zone=$zone --subnet=producer-service-subnet --no-address
在下一部分中,在每个生产者虚拟机上在端口 1000 和 2000 上启动 http 服务器。
配置虚拟机
通过 Cloud Shell
gcloud compute ssh --zone $zone "portmap-vm1" --tunnel-through-iap --project $project
在 Cloud Shell 的 portmap-vm1 会话中
mkdir 1000 cd 1000 echo "portmap-vm1 1000">index.html sudo python3 -m http.server 1000 & cd .. mkdir 2000 cd 2000 echo "portmap-vm1 2000">index.html sudo python3 -m http.server 2000 &
打开新的 Cloud Shell 窗口
首先重置变量。在 Cloud Shell 中
project=[YOUR-PROJECT-ID] region=us-central1 zone=us-central1-a echo $project echo $region echo $zone gcloud compute ssh --zone $zone "portmap-vm2" --tunnel-through-iap --project $project
在 Cloud Shell 的 portmap-vm2 会话中
mkdir 1000 cd 1000 echo "portmap-vm2 1000">index.html sudo python3 -m http.server 1000 & cd .. mkdir 2000 cd 2000 echo "portmap-vm2 2000">index.html sudo python3 -m http.server 2000 &
8. 测试提供方服务
首先,我们需要获取端口映射实例的 IP 地址。记下这两个 IP 地址。
打开新的 Cloud Shell 窗口
首先重置变量。在 Cloud Shell 中
project=[YOUR-PROJECT-ID] region=us-central1 zone=us-central1-a echo $project echo $region echo $zone gcloud compute instances describe portmap-vm1 \ --format='get(networkInterfaces[0].networkIP)' --zone $zone gcloud compute instances describe portmap-vm2\ --format='get(networkInterfaces[0].networkIP)' --zone $zone
登录测试实例。在 Cloud Shell 中
gcloud compute ssh --zone $zone "test-client-vm" --tunnel-through-iap --project $project curl [portmap-vm1 IP]:1000
预期输出
portmap-vm1 1000
在 Cloud Shell 中
curl [portmap-vm1 IP]:2000
预期输出
portmap-vm1 2000
在 Cloud Shell 中
curl [portmap-vm2 IP]:1000
预期输出
portmap-vm2 1000
在 Cloud Shell 中
curl [portmap-vm2 IP]:2000
预期输出
portmap-vm2 2000
退出 test-client-vm
9. 使用 Portmap NEG 创建提供方服务
创建负载平衡器组件
通过 Cloud Shell
gcloud compute network-endpoint-groups create portmap-neg --region=$region --network=producer-vpc --subnet=producer-service-subnet --network-endpoint-type=GCE_VM_IP_PORTMAP
将端点添加到 Portmap NEG,以创建从客户端端口到提供方端口的映射。提供方会创建此映射,并将拥有自己的方法来将此信息传达给使用方。特定的端口映射不会通过 PSC 共享。
在 Cloud Shell 中
gcloud compute network-endpoint-groups update portmap-neg --region=$region --add-endpoint=client-destination-port=1001,instance=projects/$project/zones/$zone/instances/portmap-vm1,port=1000 --add-endpoint=client-destination-port=1002,instance=projects/$project/zones/$zone/instances/portmap-vm1,port=2000 --add-endpoint=client-destination-port=1003,instance=projects/$project/zones/$zone/instances/portmap-vm2,port=1000 --add-endpoint=client-destination-port=1004,instance=projects/$project/zones/$zone/instances/portmap-vm2,port=2000
完成负载平衡器的构建。
在 Cloud Shell 中
gcloud compute backend-services create portmap-bes --load-balancing-scheme=internal --region=$region --network=producer-vpc gcloud compute backend-services add-backend portmap-bes --network-endpoint-group=portmap-neg --network-endpoint-group-region=$region gcloud compute forwarding-rules create portmap-fr --load-balancing-scheme=INTERNAL --network=producer-vpc --subnet=producer-service-subnet --ports=ALL --region=$region --backend-service=portmap-bes
创建服务连接
通过 Cloud Shell
gcloud compute service-attachments create portmap-service-attachment --region=$region --producer-forwarding-rule=portmap-fr --connection-preference=ACCEPT_AUTOMATIC --nat-subnets=psc-nat-subnet
接下来,检索并记下服务连接 URI,以便在使用方环境中配置 PSC 端点。
在 Cloud Shell 中
gcloud compute service-attachments describe portmap-service-attachment --region=$region
预期输出示例
connectionPreference: ACCEPT_AUTOMATIC creationTimestamp: '2024-07-19T10:02:29.432-07:00' description: '' enableProxyProtocol: false fingerprint: LI8D6JNQsLA= id: '6207474793859982026' kind: compute#serviceAttachment name: portmap-service-attachment natSubnets: - https://www.googleapis.com/compute/v1/projects/$project/regions/$zone/subnetworks/psc-nat-subnet pscServiceAttachmentId: high: '94288091358954472' low: '6207474793859982026' reconcileConnections: false region: https://www.googleapis.com/compute/v1/projects/$project/regions/$region selfLink: https://www.googleapis.com/compute/v1/projects/$project/regions/$region/serviceAttachments/portmap-service-attachment targetService: https://www.googleapis.com/compute/v1/projects/$project/regions/$region/forwardingRules/portmap-fr
10. 创建使用方 VPC 网络
VPC 网络
通过 Cloud Shell
gcloud compute networks create consumer-vpc --subnet-mode custom
创建子网
通过 Cloud Shell
gcloud compute networks subnets create consumer-client-subnet --network consumer-vpc --range=10.0.0.0/24 --region $region --enable-private-ip-google-access
创建网络防火墙政策和防火墙规则
通过 Cloud Shell
gcloud compute network-firewall-policies create consumer-vpc-policy --global gcloud compute network-firewall-policies associations create --firewall-policy consumer-vpc-policy --network consumer-vpc --name network-consumer-vpc --global-firewall-policy gcloud compute network-firewall-policies rules create 1000 --action ALLOW --firewall-policy consumer-vpc-policy --description "SSH with IAP" --direction INGRESS --src-ip-ranges 35.235.240.0/20 --layer4-configs tcp:22 --global-firewall-policy
使用方广告资源网只需通过 IAP 访问权限使用 SSH 即可。
11. 创建虚拟机、PSC 端点并测试连接
此时,应该有三个 Cloud Shell 窗口打开。其中一个会话应与 portmap-vm1 处于打开状态。其中一个会话应与 portmap-vm2 建立,另一个应为工作会话。
创建测试虚拟机
通过 Cloud Shell
gcloud compute instances create consumer-client-vm --zone $zone --subnet=consumer-client-subnet --no-address
创建 PSC 端点
通过 Cloud Shell
gcloud compute addresses create psc-endpoint-ip --region=$region --subnet=consumer-client-subnet --addresses 10.0.0.10 gcloud compute forwarding-rules create psc-portmap-endpoint --region=$region --network=consumer-vpc --address=psc-endpoint-ip --target-service-attachment=[SERVICE ATTACHMENT URI]
测试连接性
通过 Cloud Shell
gcloud compute ssh --zone $zone "consumer-client-vm" --tunnel-through-iap --project $project curl 10.0.0.10:1001
预期输出
portmap-vm1 1000
通过 Cloud Shell
curl 10.0.0.10:1002
预期输出
portmap-vm1 2000
通过 Cloud Shell
curl 10.0.0.10:1003
预期输出
portmap-vm2 1000
通过 Cloud Shell
curl 10.0.0.10:1004
预期输出
portmap-vm2 2000
12. 清理步骤
退出虚拟机实例(所有窗口)
exit
在一个 Cloud Shell 终端中删除实验组件
gcloud compute forwarding-rules delete psc-portmap-endpoint --region=$region -q gcloud compute addresses delete psc-endpoint-ip --region=$region -q gcloud compute instances delete consumer-client-vm --zone=$zone -q gcloud compute network-firewall-policies rules delete 1000 --firewall-policy=consumer-vpc-policy --global-firewall-policy -q gcloud compute network-firewall-policies associations delete --firewall-policy=consumer-vpc-policy --name=network-consumer-vpc --global-firewall-policy -q gcloud compute network-firewall-policies delete consumer-vpc-policy --global -q gcloud compute networks subnets delete consumer-client-subnet --region=$region -q gcloud compute networks delete consumer-vpc -q gcloud compute service-attachments delete portmap-service-attachment --region=$region -q gcloud compute forwarding-rules delete portmap-fr --region=$region -q gcloud compute backend-services delete portmap-bes --region=$region -q gcloud compute network-endpoint-groups delete portmap-neg --region=$region -q gcloud compute instances delete test-client-vm --zone=$zone -q gcloud compute instances delete portmap-vm2 --zone=$zone -q gcloud compute instances delete portmap-vm1 --zone=$zone -q gcloud compute network-firewall-policies rules delete 2001 --firewall-policy producer-vpc-policy --global-firewall-policy -q gcloud compute network-firewall-policies rules delete 2000 --firewall-policy producer-vpc-policy --global-firewall-policy -q gcloud compute network-firewall-policies rules delete 1000 --firewall-policy producer-vpc-policy --global-firewall-policy -q gcloud compute network-firewall-policies associations delete --firewall-policy=producer-vpc-policy --name=network-producer-vpc --global-firewall-policy -q gcloud compute network-firewall-policies delete producer-vpc-policy --global -q gcloud compute networks subnets delete psc-nat-subnet --region $region -q gcloud compute networks subnets delete producer-service-subnet --region $region -q gcloud compute networks delete producer-vpc -q
13. 恭喜!
恭喜您完成此 Codelab。
所学内容
- Private Service Connect 端口映射用例
- PSC 端口映射的主要优势
- 网络要求
- 使用端口映射创建 Private Service Connect 提供方服务。
- 创建 Private Service Connect 端点
- 通过 Private Service Connect 端点向提供方服务发出调用