作者:MeshCloud脉时云公有云架构师陈满
一、背景
VPC Peering 可以解决同项目或跨项目下不同VPC 之间通过内部网络相互访问。VPC 是不同网络基础的边界,不同的VPC 之间在未进行互联条件下,不通过外部IP 方式,默认是不可以直接相互访问;如需在多个 VPC 之间互通,可以借助 VPN ,或者 VPC Peering 等方式。
VPC Peering后会基于GCP global VPC 网络实现内部互联;VPC Peering拓扑,如下所示:

Cloud VPN ,通过在VPC 之间建立Tunnel 建立内部互联,实现安全的网络连接;VPN 是一种可为各个 VPC 网络添加的区域级 VPN 解决方案。在为高可用性 VPN 配置两个隧道后,Cloud VPN 可以提供服务可用性达 99.99% 的正常运行时间。VPN 互联拓扑,如下图所示:

VPC Peering 存在VPC之间的路由不具有传递性,换句话说,如VPC1 Peer VPC2,VPC2 Peer VPC3 后,VPC1 和VPC2、VPC2 和 VPC3 可以直接相互访问,但VPC1 下的VM 是不能直接与VPC3 下VM 实现内部访问,如下拓扑所示:

为了解决上述问题,我们可以借助Google Cloud 的Custom routes advertisement for VPC peering,将路由在VPC Peering 中相互传递,进而实现跨VPC 相互访问,如下拓扑所示:

VPC1 和 VPC2 通过 VPC Peering 实现互联,VPC2 和VPC3通过VPN 实现互通,并通过 BGP 传递了路由。此时借助 Custom routes advertisement for VPC peering 的功能,把 VPC3 的路由传递给 VPC1,实现VPC1与VPC3实现互通。
二、实现过程
2.1 实现过程步骤
- 创建三个VPC ,用于模拟实际项目下多VPC 间借助VPC Peering 无法实现跨VPC 互通;
- 创建VPN,借助Custom routes advertisement 实现路由宣告,实现路由传递;
- 创建VPC Peering 实现网络间的互连互通;
- 验证跨 VPC 间互连互通;
2.2 具体实现步骤
2.2.1 创建三个VPC Network,如下所示:

gcloud compute networks create vpc1 --project=mec-test-344202 --subnet-mode=custom --mtu=1460 --bgp-routing-mode=regional
gcloud compute networks subnets create vpc1-subnet1 --project=mec-test-344202 --range=10.10.0.0/24 --stack-type=IPV4_ONLY --network=vpc1 --region=us-central1 --enable-private-ip-google-access
gcloud compute networks subnets create vpc1-subnet2 --project=mec-test-344202 --range=10.10.1.0/24 --stack-type=IPV4_ONLY --network=vpc1 --region=us-central1
gcloud compute networks subnets create vpc1-subnet3 --project=mec-test-344202 --range=10.10.2.0/24 --stack-type=IPV4_ONLY --network=vpc1 --region=us-central1 --enable-private-ip-google-access
2.2.2 在 VPC2 与 VPC3 之间创建Cloud VPN,如下所示:

创建HA Cloud VPN 实现VPN 的高可用性,如下所示:

验证两侧路由是否正常学习到了,可以看到通过 BGP,VPC2 和 VPC3 的l两侧的路由已经互相学习到,如下所示:


2.2.3 调整BGP 宣告自定义VPC1 路由:
在创建 VPC Peering 后,VPC1 的路由需要路由到 VPC3,因此,需要在 VPC2 和 VPC3 的 BGP 宣告中,增加 VPC1 的子网自定义路由:

此时,在vpc3 的路由表可以同时看到vpc1 和VPC2 的路由,说明VPC3 收到了custom route,如下所示:

2.2.4 创建 VPC1 与 VPC2 之间VPC Peering对等互连:
创建vpc1peer2:

创建vpc2peer1:

2.2.5 查看VPC 路由:
VPC1 的路由中,可以看到 VPC Peering 的路由包含 VPC2 的路由以及自定义路由宣告导入的VPC3 的路由,如下所示:

2.2.6 验证跨VPC之间互相访问情况:
在 VPC1 内的 VM,ping 在 VPC3 内的 VM,结果如下所示:

在 VPC3 内的 VM,ping 在 VPC1 内的 VM,结果如下所示:

参考:
- VPC Peering 概览 [https://cloud.google.com/vpc/docs/vpc-peering?hl=zh-cn#restrictions]
- 使用VPC Peering 对等连接[https://cloud.google.com/vpc/docs/using-vpc-peering?hl=zh-cn]
- Cloud HA VPN configuration [https://cloud.google.com/network-connectivity/docs/vpn/how-to/creating-ha-vpn2?hl=zh-cn]