Private Service Connect:从 VPC 对等互连迁移到 Private Service Connect

1. 简介

VPC 对等互连是提供方为其用户提供服务使用权限的常用方法。不过,使用 VPC 对等互连会带来许多复杂的路由问题,例如非传递性 VPC 对等互连、IP 地址消耗量大以及对等互连 VPC 中的资源过度暴露。

Private Service Connect (PSC) 是一种连接方法,可让提供方通过单个端点公开服务,而使用方可在工作负载 VPC 中预配该端点。这消除了用户在使用 VPC 对等互连时遇到的许多问题。虽然许多新服务都是使用 PSC 创建的,但仍有许多服务以 VPC 对等互连服务的形式存在。

Google Cloud 很高兴推出服务迁移途径,让您能够将通过 VPC 对等互连创建的服务迁移到基于 PSC 的架构。使用此迁移方法,通过 VPC 对等互连公开的提供方服务的 IP 地址会保留到基于 PSC 的架构,因此使用方只需进行极少的更改。请跟随本 Codelab 学习执行此迁移的技术步骤。

注意:此迁移路径仅适用于生产者和消费者位于同一 Google Cloud 组织中的服务。对于使用 VPC 对等互连的任何 Google Cloud 服务或第三方服务,它们将采用类似的迁移方法,但会针对服务本身进行自定义。请与相关方联系,咨询这些类型服务的迁移途径。

学习内容

  • 如何设置基于 VPC 对等互连的服务
  • 如何设置基于 PSC 的服务
  • 使用 Internal-Ranges API 通过 VPC 对等互连执行子网迁移,以实现从 VPC 对等互连到 PSC 服务的迁移。
  • 了解服务迁移何时需要停机
  • 迁移清理步骤

所需条件

  • 具有“所有者”权限的 Google Cloud 项目

2. Codelab 拓扑

为简单起见,此 Codelab 将所有资源集中在一个项目中。如果生产者和使用者位于不同的项目中,Codelab 中会注明需要在生产者端执行哪些操作,以及需要在使用者端执行哪些操作。

此 Codelab 将有 4 个状态。

7dbf27cf215f9703.png

状态 1 是 VPC 对等互连状态。将有两个 VPC(consumer-vpc 和 producer-vpc)相互对等互连。提供方 VPC 将通过内部直通式网络负载平衡器公开简单的 Apache 服务。consumer-vpc 将包含一个用于测试的 consumer-vm。

7f64427c0e59d417.png

状态 2 是 PSC 测试状态。我们将创建一个新的转发规则,并使用此规则与我们的服务连接相关联。然后,我们将在 consumer-vpc 中创建一个测试 PSC 端点,以测试我们的 PSC 服务是否按预期运行。

98c324c59c1fbf68.png

状态 3 是迁移状态。我们将预留提供方 VPC 中部署了基于 VPC 对等互连的服务的子网范围,以供使用方 VPC 使用。然后,我们将创建一个新的 PSC 端点,其 IP 地址与 producer-vpc 中预先存在的转发规则相同。

a64ab7b69132c722.png

状态 4 是最终 PSC 状态。我们将清理测试 PSC 端点,并删除 consumer-vpc 与 producer-vpc 之间的 VPC 对等互连。

3. 设置和要求

自定进度的环境设置

  1. 登录 Google Cloud 控制台,然后创建一个新项目或重复使用现有项目。如果您还没有 Gmail 或 Google Workspace 账号,则必须创建一个

fbef9caa1602edd0.png

a99b7ace416376c4.png

5e3ff691252acf41.png

  • 项目名称是此项目参与者的显示名称。它是 Google API 尚未使用的字符串。您可以随时对其进行更新。
  • 项目 ID 在所有 Google Cloud 项目中是唯一的,并且是不可变的(一经设置便无法更改)。Cloud 控制台会自动生成一个唯一字符串;通常情况下,您无需关注该字符串。在大多数 Codelab 中,您都需要引用项目 ID(通常用 PROJECT_ID 标识)。如果您不喜欢生成的 ID,可以再随机生成一个 ID。或者,您也可以尝试自己的项目 ID,看看是否可用。完成此步骤后便无法更改该 ID,并且此 ID 在项目期间会一直保留。
  • 此外,还有第三个值,即部分 API 使用的项目编号,供您参考。如需详细了解所有这三个值,请参阅文档
  1. 接下来,您需要在 Cloud 控制台中启用结算功能,以便使用 Cloud 资源/API。运行此 Codelab 应该不会产生太多的费用(如果有的话)。若要关闭资源以避免产生超出本教程范围的结算费用,您可以删除自己创建的资源或删除项目。Google Cloud 新用户符合参与 300 美元免费试用计划的条件。

启动 Cloud Shell

虽然可以通过笔记本电脑对 Google Cloud 进行远程操作,但在此 Codelab 中,您将使用 Google Cloud Shell,这是一个在云端运行的命令行环境。

Google Cloud 控制台 中,点击右上角工具栏中的 Cloud Shell 图标:

55efc1aaa7a4d3ad.png

预配和连接到环境应该只需要片刻时间。完成后,您应该会看到如下内容:

7ffe5cbb04455448.png

这个虚拟机已加载了您需要的所有开发工具。它提供了一个持久的 5 GB 主目录,并且在 Google Cloud 中运行,大大增强了网络性能和身份验证功能。您在此 Codelab 中的所有工作都可以在浏览器中完成。您无需安装任何程序。

4. 准备工作

启用 API

在 Cloud Shell 中,确保您的项目已设置并配置变量。

gcloud auth login
gcloud config list project
gcloud config set project [YOUR-PROJECT-ID]
export projectid=[YOUR-PROJECT-ID]
export region=us-central1
export zone=$region-a
echo $projectid
echo $region
echo $zone

启用所有必要的服务

gcloud services enable compute.googleapis.com
gcloud services enable networkconnectivity.googleapis.com
gcloud services enable dns.googleapis.com

5. 创建提供方 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/28 \
    --region=$region

gcloud compute networks subnets create producer-fr-subnet \
        --network=producer-vpc \
        --range=192.168.0.0/28 \
        --region=$region

创建生产者 Cloud Router 和 Cloud NAT

从 Cloud Shell

gcloud compute routers create $region-cr \
   --network=producer-vpc \
   --region=$region

gcloud compute routers nats create $region-nat \
    --router=$region-cr \
    --region=$region \
    --nat-all-subnet-ip-ranges \
    --auto-allocate-nat-external-ips

创建 Producer 网络防火墙政策和防火墙规则

从 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 可访问的所有虚拟机实例。
  • 允许来自 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

我们还将创建另外两条规则,以允许负载平衡器对服务进行健康检查,并允许来自将从 consumer-vpc 连接的虚拟机的网络流量。

从 Cloud Shell

gcloud compute network-firewall-policies rules create 2000 \
    --action ALLOW \
    --firewall-policy producer-vpc-policy \
    --description "LB healthchecks" \
    --direction INGRESS \
    --src-ip-ranges 130.211.0.0/22,35.191.0.0/16 \
    --layer4-configs tcp:80  \
    --global-firewall-policy


gcloud compute network-firewall-policies rules create 3000 \
    --action ALLOW \
    --firewall-policy producer-vpc-policy \
    --description "allow access from consumer-vpc" \
    --direction INGRESS \
    --src-ip-ranges 10.0.1.0/28 \
    --layer4-configs tcp:80  \
    --global-firewall-policy

6. 提供方服务设置(提供方活动)

我们将创建一个生产者服务,其中包含一个运行 Apache Web 服务器的虚拟机,该虚拟机将添加到由区域级内部网络直通式负载平衡器提供前端服务的非代管式实例组。

创建虚拟机和非托管式实例组

从 Cloud Shell

gcloud compute instances create producer-service-vm \
    --network producer-vpc \
    --subnet producer-service-subnet \
    --zone $zone \
    --no-address \
    --metadata startup-script='#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    a2enmod ssl
    sudo a2ensite default-ssl
    echo "I am a Producer Service." | \
    tee /var/www/html/index.html
    systemctl restart apache2'

从 Cloud Shell

gcloud compute instance-groups unmanaged create prod-uig \
  --zone=$zone

gcloud compute instance-groups unmanaged add-instances prod-uig \
  --zone=$zone \
  --instances=producer-service-vm

创建区域级内部直通式网络负载平衡器

从 Cloud Shell

gcloud compute health-checks create http producer-hc \
        --region=$region

gcloud compute backend-services create producer-bes \
  --load-balancing-scheme=internal \
  --protocol=tcp \
  --region=$region \
  --health-checks=producer-hc \
  --health-checks-region=$region

gcloud compute backend-services add-backend producer-bes \
  --region=$region \
  --instance-group=prod-uig \
  --instance-group-zone=$zone

gcloud compute addresses create producer-fr-ip\
  --region $region \
  --subnet producer-fr-subnet \
  --addresses 192.168.0.2

gcloud compute forwarding-rules create producer-fr \
  --region=$region \
  --load-balancing-scheme=internal \
  --network=producer-vpc \
  --subnet=producer-fr-subnet \
  --address=producer-fr-ip \
  --ip-protocol=TCP \
  --ports=80 \
  --backend-service=producer-bes \
  --backend-service-region=$region

7. 创建使用方 VPC 网络(使用方活动)

VPC 网络

从 Cloud Shell

gcloud compute networks create consumer-vpc \
    --subnet-mode=custom

创建子网

从 Cloud Shell

gcloud compute networks subnets create consumer-vm-subnet \
    --network=consumer-vpc \
    --range=10.0.1.0/28 \
    --region=$region

创建消费者网络防火墙政策和防火墙规则

我们将为 consumer-vpc 创建另一个网络防火墙政策。

从 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

8. 创建 VPC 对等互连

制作方活动

从 Cloud Shell

gcloud compute networks peerings create producer-vpc-peering \
    --network=producer-vpc \
    --peer-project=$projectid \
    --peer-network=consumer-vpc

消费者活动

从 Cloud Shell

gcloud compute networks peerings create consumer-vpc-peering \
    --network=consumer-vpc \
    --peer-project=$projectid \
    --peer-network=producer-vpc

通过检查 consumer-vpc 中的路由列表,确认对等互连已建立。您应该会看到 consumer-vpc 和 producer-vpc 的路由。

消费者活动

从 Cloud Shell

gcloud compute routes list --filter="network=consumer-vpc"

预期输出

NAME: default-route-49dda7094977e231
NETWORK: consumer-vpc
DEST_RANGE: 0.0.0.0/0
NEXT_HOP: default-internet-gateway
PRIORITY: 1000

NAME: default-route-r-10d65e16cc6278b2
NETWORK: consumer-vpc
DEST_RANGE: 10.0.1.0/28
NEXT_HOP: consumer-vpc
PRIORITY: 0

NAME: peering-route-496d0732b4f11cea
NETWORK: consumer-vpc
DEST_RANGE: 192.168.0.0/28
NEXT_HOP: consumer-vpc-peering
PRIORITY: 0

NAME: peering-route-b4f9d3acc4c08d55
NETWORK: consumer-vpc
DEST_RANGE: 10.0.0.0/28
NEXT_HOP: consumer-vpc-peering
PRIORITY: 0

9. 创建 DNS 区域(消费者活动)

我们将创建一个 Cloud DNS 专用区域,以便通过 DNS 而不是专用 IP 地址来调用提供方服务,从而展示一个更真实的示例。

我们将向 example.com 网域添加一条 A 记录,将 service.example.com 指向我们之前创建的网络直通负载平衡器转发规则 IP 地址。相应转发规则的 IP 地址为 192.168.0.2。

从 Cloud Shell

gcloud dns managed-zones create "producer-service" \
   --dns-name=example.com \
   --description="producer service dns" \
   --visibility=private \
   --networks=consumer-vpc

gcloud dns record-sets transaction start \
   --zone="producer-service"

gcloud dns record-sets transaction add 192.168.0.2 \
   --name=service.example.com \
   --ttl=300 \
   --type=A \
   --zone="producer-service"

gcloud dns record-sets transaction execute \
   --zone="producer-service"

10. 通过 VPC 对等互连测试提供方服务(使用方活动)

至此,状态 1 架构已创建完毕。

创建消费类客户端虚拟机

从 Cloud Shell

gcloud compute instances create consumer-client \
   --zone=$zone \
   --subnet=consumer-vm-subnet \
   --no-address

测试连接

从 Cloud Shell

gcloud compute ssh \
    --zone "$zone" "consumer-client" \
    --tunnel-through-iap \
    --project $projectid

从消费类客户端虚拟机

curl service.example.com

预期输出

I am a Producer Service. 

从消费类客户端虚拟机

exit

11. 为 Private Service Connect 准备服务(提供方活动)

现在,我们已完成所有初始设置步骤,接下来将开始准备将 VPC 对等互连服务迁移到 Private Service Connect。在本部分中,我们将通过配置服务以通过服务连接公开,来更改 producer-vpc。我们需要在该子网中创建一个新的子网和新的转发规则,以便将现有子网迁移到使用方 VPC,从而保持服务的现有 IP 地址不变。

创建将托管新负载平衡器转发规则 IP 的子网。

从 Cloud Shell

gcloud compute networks subnets create producer-psc-fr-subnet \
    --network=producer-vpc \
    --range=10.0.2.64/28 \
    --region=$region

创建负载平衡器转发规则内部 IP 地址。

从 Cloud Shell

gcloud compute addresses create producer-psc-ip \
  --region $region \
  --subnet producer-psc-fr-subnet \
  --addresses 10.0.2.66

创建新的负载平衡器转发规则。此规则配置为使用我们之前配置的同一后端服务和健康检查。

从 Cloud Shell

gcloud compute forwarding-rules create psc-service-fr \
  --region=$region \
  --load-balancing-scheme=internal \
  --network=producer-vpc \
  --subnet=producer-psc-fr-subnet \
  --address=producer-psc-ip \
  --ip-protocol=TCP \
  --ports=80 \
  --backend-service=producer-bes \
  --backend-service-region=$region

psc-nat-subnet 将与 PSC 服务连接相关联,以用于网络地址转换。对于生产用例,此子网需要适当调整大小,以支持所连接的端点数量。如需了解详情,请参阅 PSC NAT 子网大小调整文档。

从 Cloud Shell

gcloud compute networks subnets create psc-nat-subnet \
    --network=producer-vpc \
    --range=10.100.100.0/28 \
    --region=$region \
    --purpose=PRIVATE_SERVICE_CONNECT

我们必须向网络防火墙政策添加额外的防火墙规则,以允许来自 psc-nat-subnet 的流量。通过 PSC 访问服务时,流量将来源于 psc-nat-subnet。

从 Cloud Shell

gcloud compute network-firewall-policies rules create 2001 \
    --action ALLOW \
    --firewall-policy producer-vpc-policy \
    --description "allow PSC NAT subnet" \
    --direction INGRESS \
    --src-ip-ranges 10.100.100.0/28 \
    --layer4-configs tcp:80  \
    --global-firewall-policy

创建服务连接,并记下服务连接 URI,以便在下一部分中配置 PSC 端点。

从 Cloud Shell

gcloud compute service-attachments create producer-sa \
    --region=$region \
    --producer-forwarding-rule=psc-service-fr  \
    --connection-preference=ACCEPT_MANUAL \
    --consumer-accept-list=$projectid=5 \
    --nat-subnets=psc-nat-subnet

从 Cloud Shell

gcloud compute service-attachments describe producer-sa --region=$region

输出示例

connectionPreference: ACCEPT_MANUAL
consumerAcceptLists:
- connectionLimit: 5
  projectIdOrNum: $projectid
creationTimestamp: '2025-04-24T11:23:09.886-07:00'
description: ''
enableProxyProtocol: false
fingerprint: xxx
id: 'xxx'
kind: compute#serviceAttachment
name: producer-sa
natSubnets:
- https://www.googleapis.com/compute/v1/projects/$projectid/regions/$region/subnetworks/psc-nat-subnet
pscServiceAttachmentId:
  high: 'xxx'
  low: 'xxx'
reconcileConnections: false
region: https://www.googleapis.com/compute/v1/projects/$projectid/regions/$region
selfLink: https://www.googleapis.com/compute/v1/projects/$projectid/regions/$region/serviceAttachments/producer-sa
targetService: https://www.googleapis.com/compute/v1/projects/$projectid/regions/$region/forwardingRules/psc-service-fr

12. 将“测试”使用方 PSC 端点连接到提供方服务并进行测试(使用方活动)

架构现在处于状态 2。

此时,通过 VPC 对等互连公开的现有提供方服务在生产环境场景中仍处于有效状态并正常运行。我们将创建一个“测试”PSC 端点,以确保公开的服务附件正常运行,然后再启动中断期,将当前的 VPC 对等互连子网迁移到使用方 VPC。最终状态的连接将是一个 PSC 端点,其 IP 地址与基于 VPC 对等互连的服务的当前转发规则相同。

创建 PSC 端点

从 Cloud Shell

gcloud compute addresses create test-psc-endpoint-ip \
    --region=$region \
    --subnet=consumer-vm-subnet \
    --addresses 10.0.1.3

以下目标服务将是您在上一步中记下的服务连接 URI。

从 Cloud Shell

gcloud compute forwarding-rules create test-psc-endpoint \
  --region=$region \
  --network=consumer-vpc \
  --address=test-psc-endpoint-ip \
  --target-service-attachment=projects/$projectid/regions/$region/serviceAttachments/producer-sa

测试“测试”PSC 端点

从 Cloud Shell

gcloud compute ssh \
    --zone "$zone" "consumer-client" \
    --tunnel-through-iap \
    --project $projectid

来自消费者客户

curl 10.0.1.3

预期输出

I am a Producer Service. 

来自消费者客户

exit

13. 迁移现有提供方转发规则子网

执行这些步骤将导致基于实时 VPC 对等互连的提供方服务中断。现在,我们将使用 Internal Ranges API 将转发规则子网从提供方 VPC 迁移到使用方 VPC。这会锁定子网,使其在以下过渡期内无法使用:我们在提供方 VPC 中删除子网,并指定该子网仅用于在使用方 VPC 中创建迁移。

内部范围 API 要求您预留现有的 VPC 对等互连转发规则子网 (producer-fr-subnet, 192.168.0.0/28),并在 consumer-vpc (consumer-psc-subnet) 中指定目标子网名称。我们将在几个步骤中在 consumer-vpc 中创建一个具有此名称的新子网。

预留提供方 FR 子网以进行迁移

制作方活动

从 Cloud Shell

gcloud network-connectivity internal-ranges create producer-peering-internal-range \
    --ip-cidr-range=192.168.0.0/28 \
    --network=producer-vpc \
    --usage=FOR_MIGRATION \
    --migration-source=projects/$projectid/regions/$region/subnetworks/producer-fr-subnet \
    --migration-target=projects/$projectid/regions/$region/subnetworks/consumer-psc-subnet

对我们创建的内部范围运行 describe 命令,以查看子网的状态。

制作方活动

从 Cloud Shell

gcloud network-connectivity internal-ranges describe producer-peering-internal-range

输出示例

createTime: '2025-04-24T19:26:10.589343291Z'
ipCidrRange: 192.168.0.0/28
migration:
  source: projects/$projectid/regions/$region/subnetworks/producer-fr-subnet
  target: projects/$projectid/regions/$region/subnetworks/consumer-psc-subnet
name: projects/$projectid/locations/global/internalRanges/producer-peering-internal-range
network: https://www.googleapis.com/compute/v1/projects/$project/global/networks/producer-vpc
peering: FOR_SELF
updateTime: '2025-04-24T19:26:11.521960016Z'
usage: FOR_MIGRATION

删除基于 VPC 对等互连的转发规则和子网

制作方活动

从 Cloud Shell

gcloud compute forwarding-rules delete producer-fr --region=$region

gcloud compute addresses delete producer-fr-ip --region=$region

gcloud compute networks subnets delete producer-fr-subnet --region=$region

迁移子网

使用我们之前创建的内部范围创建一个新的子网,将该子网迁移到使用方 VPC。此子网的名称必须与我们之前指定的目标名称 (consumer-psc-subnet) 相同。PEER_MIGRATION 的具体用途表明该子网预留用于对等互连的 VPC 之间的子网迁移。设置此用途标志后,相应子网只能包含预留的静态 IP 地址和 PSC 端点。

消费者活动

从 Cloud Shell

gcloud compute networks subnets create consumer-psc-subnet \
  --purpose=PEER_MIGRATION \
  --network=consumer-vpc \
  --range=192.168.0.0/28 \
  --region=$region

14. 创建最终状态 PSC 端点(使用方活动)

此时,Producer 服务仍处于关闭状态。我们刚刚创建的子网仍处于锁定状态,只能用于迁移这一特定用途。您可以尝试在此子网中创建虚拟机,以此来测试此问题。虚拟机创建将失败。

从 Cloud Shell

gcloud compute instances create test-consumer-vm \
    --zone=$zone \
    --subnet=consumer-psc-subnet \
    --no-address

预期输出

ERROR: (gcloud.compute.instances.create) Could not fetch resource:
 - Subnetwork must have purpose=PRIVATE.

我们只能使用此子网创建 PSC 端点。请注意,我们创建的 IP 地址与提供方服务通过 VPC 对等互连使用的转发规则的 IP 地址相同。

从 Cloud Shell

gcloud compute addresses create psc-endpoint-ip \
    --region=$region \
    --subnet=consumer-psc-subnet \
    --addresses 192.168.0.2

您必须再次使用之前记下的服务连接 URI,该 URI 也曾用于创建“测试”PSC 端点。

从 Cloud Shell

gcloud compute forwarding-rules create psc-endpoint \
    --region=$region \
    --network=consumer-vpc \
    --address=psc-endpoint-ip \
    --target-service-attachment=projects/$projectid/regions/$region/serviceAttachments/producer-sa

15. 测试最终状态 PSC 端点(使用方 activity)

此时,您处于状态 3 架构。

从 Cloud Shell

gcloud compute ssh \
    --zone "$zone" "consumer-client" \
    --tunnel-through-iap \
    --project $projectid

从消费类客户端虚拟机

curl service.example.com

预期输出

I am a Producer Service. 

从消费类客户端虚拟机

exit

此时,服务中断已结束,服务已恢复正常。请注意,我们无需对现有 DNS 进行任何更改。无需进行任何客户端更改。应用只需恢复对迁移后服务的操作即可。

16. 迁移清理

如需完成迁移,我们需要执行一些清理步骤。我们必须删除并解锁资源。

解锁内部范围子网

此操作将解锁迁移的子网,以便将其用途从“PEER_MIGRATION”更改为“PRIVATE”。

制作方活动

从 Cloud Shell

gcloud network-connectivity internal-ranges delete producer-peering-internal-range

消费者活动

从 Cloud Shell

gcloud compute networks subnets update consumer-psc-subnet \
    --region=$region \
    --purpose=PRIVATE

gcloud compute networks subnets describe consumer-psc-subnet --region=$region

输出示例

creationTimestamp: '2025-04-24T12:29:33.883-07:00'
fingerprint: xxx
gatewayAddress: 192.168.0.1
id: 'xxx'
ipCidrRange: 192.168.0.0/28
kind: compute#subnetwork
name: consumer-psc-subnet
network: https://www.googleapis.com/compute/v1/projects/$projectid/global/networks/consumer-vpc
privateIpGoogleAccess: false
privateIpv6GoogleAccess: DISABLE_GOOGLE_ACCESS
purpose: PRIVATE
region: https://www.googleapis.com/compute/v1/projects/$projectid/regions/$region
selfLink: https://www.googleapis.com/compute/v1/projects/$projectid/regions/$region/subnetworks/consumer-psc-subnet

删除 VPC 对等互连

制作方活动

从 Cloud Shell

gcloud compute networks peerings delete producer-vpc-peering \
    --network=producer-vpc

消费者活动

从 Cloud Shell

gcloud compute networks peerings delete consumer-vpc-peering \
        --network=consumer-vpc

删除“测试”PSC 端点

Consumer-Activity

从 Cloud Shell

gcloud compute forwarding-rules delete test-psc-endpoint --region=$region
gcloud compute addresses delete test-psc-endpoint-ip --region=$region

17. 迁移清理后的最终测试(消费者活动)

此时,已实现状态 4 架构(最终状态)。

再次测试 PSC 端点连接,确保迁移清理未产生任何不利影响。

从 Cloud Shell

gcloud compute ssh \
    --zone "$zone" "consumer-client" \
    --tunnel-through-iap \
    --project $projectid

从消费类客户端虚拟机

curl service.example.com

预期输出

I am a Producer Service. 

从消费类客户端虚拟机

exit

大功告成!

18. 清理步骤

从 Cloud Shell

gcloud compute forwarding-rules delete psc-endpoint --region=$region -q

gcloud compute addresses delete psc-endpoint-ip --region=$region -q

gcloud compute instances delete consumer-client --zone=$zone --project=$projectid -q

gcloud dns record-sets delete service.example.com --zone="producer-service" --type=A -q

gcloud dns managed-zones delete "producer-service" -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-psc-subnet --region=$region -q

gcloud compute networks subnets delete consumer-vm-subnet --region=$region -q

gcloud compute networks delete consumer-vpc -q

gcloud compute service-attachments delete producer-sa --region=$region -q

gcloud compute forwarding-rules delete psc-service-fr --region=$region -q

gcloud compute addresses delete producer-psc-ip --region=$region -q

gcloud compute backend-services delete producer-bes --region=$region -q

gcloud compute health-checks delete producer-hc --region=$region -q

gcloud compute instance-groups unmanaged delete prod-uig --zone=$zone -q

gcloud compute instances delete producer-service-vm --zone=$zone --project=$projectid -q

gcloud compute network-firewall-policies rules delete 3000 --firewall-policy producer-vpc-policy --global-firewall-policy -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 routers nats delete $region-nat --router=$region-cr --region=$region -q

gcloud compute routers delete $region-cr --region=$region -q

gcloud compute networks subnets delete psc-nat-subnet --region=$region -q

gcloud compute networks subnets delete producer-psc-fr-subnet --region=$region -q

gcloud compute networks subnets delete producer-service-subnet --region=$region -q

gcloud compute networks delete producer-vpc -q

19. 恭喜!

恭喜您完成此 Codelab。

所学内容

  • 如何设置基于 VPC 对等互连的服务
  • 如何设置基于 PSC 的服务
  • 使用 Internal-Ranges API 通过 VPC 对等互连执行子网迁移,以实现从 VPC 对等互连到 PSC 服务的迁移。
  • 了解服务迁移何时需要停机
  • 迁移清理步骤