1. 简介
静态自定义路由会影响 VPC 中的默认路由行为。IPv6 自定义路由现在支持新的下一个跃点属性:next-hop-gateway、next-hop-instance 和 next-hop-address。本 Codelab 介绍了如何使用由多 NIC 虚拟机实例连接的两个 VPC 将 IPv6 自定义路由与这些新的下一个跃点选项搭配使用。您还将演示如何混合使用 ULA 和 GUA 地址,以及如何使用新的自定义路由功能将 ULA VPC 的网络可访问性扩展到公共互联网。
学习内容
- 如何创建具有下一个跃点实例下一个跃点的 IPv6 自定义路由。
- 如何创建具有下一个跃点网关下一个跃点的 IPv6 自定义路由。
- 如何创建具有 next-hop-address 下一个跃点的 IPv6 自定义路由。
所需条件
- Google Cloud 项目
2. 准备工作
更新项目以支持该 Codelab
此 Codelab 使用 $variables 来帮助在 Cloud Shell 中实现 gcloud 配置。
在 Cloud Shell 中,执行以下操作
gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
export projectname=$(gcloud config list --format="value(core.project)")
实验室整体架构
为了演示这两种类型的自定义路由下一跳,您将创建 3 个 VPC:一个使用 GUA 地址的客户端 VPC、一个使用 ULA 地址的服务器 VPC,以及一个使用 GUA 地址的第二个服务器 VPC。
为了让客户端 VPC 能够访问 ULA 服务器,您将使用同时指向多 NIC 网关实例的 next-hop-instance 和 next-hop-address 的自定义路由。如需提供对 GUA 服务器的访问权限(在删除默认的 ::/0 路由后),您将使用下一个跃点网关指向默认互联网网关的自定义路由,以便在互联网上提供路由。
3. 客户端 VPC 设置
创建客户端 VPC
在 Cloud Shell 中,执行以下操作:
gcloud compute networks create client-vpc \
--project=$projectname \
--subnet-mode=custom \
--mtu=1500 --bgp-routing-mode=regional
创建客户端子网
在 Cloud Shell 中,执行以下操作:
gcloud compute networks subnets create client-subnet \
--network=client-vpc \
--project=$projectname \
--range=192.168.1.0/24 \
--stack-type=IPV4_IPV6 \
--ipv6-access-type=external \
--region=us-central1
使用以下命令在环境变量中记录分配的 GUA 子网
export client_subnet=$(gcloud compute networks subnets \
describe client-subnet \
--project $projectname \
--format="value(externalIpv6Prefix)" \
--region us-central1)
启动客户端实例
在 Cloud Shell 中,执行以下操作:
gcloud compute instances create client-instance \
--subnet client-subnet \
--stack-type IPV4_IPV6 \
--zone us-central1-a \
--project=$projectname
为客户端 VPC 流量添加防火墙规则
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules create allow-gateway-client \
--direction=INGRESS --priority=1000 \
--network=client-vpc --action=ALLOW \
--rules=tcp --source-ranges=$client_subnet \
--project=$projectname
添加防火墙规则以允许客户端实例使用 IAP
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules create allow-iap-client \
--direction=INGRESS --priority=1000 \
--network=client-vpc --action=ALLOW \
--rules=tcp:22 --source-ranges=35.235.240.0/20 \
--project=$projectname
确认对客户端实例的 SSH 访问权限
在 Cloud Shell 中,登录客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
如果成功,您会看到客户端实例中的终端窗口。退出 SSH 会话,继续完成此 Codelab。
4. ULA 服务器 VPC 设置
创建 ULA 服务器 VPC
在 Cloud Shell 中,执行以下操作:
gcloud compute networks create server-vpc1 \
--project=$projectname \
--subnet-mode=custom --mtu=1500 \
--bgp-routing-mode=regional \
--enable-ula-internal-ipv6
创建 ULA 服务器子网
在 Cloud Shell 中,执行以下操作:
gcloud compute networks subnets create server-subnet1 \
--network=server-vpc1 \
--project=$projectname \
--range=192.168.0.0/24 \
--stack-type=IPV4_IPV6 \
--ipv6-access-type=internal \
--region=us-central1
使用以下命令在环境变量中记录分配的 ULA 子网
export server_subnet1=$(gcloud compute networks subnets \
describe server-subnet1 \
--project $projectname \
--format="value(internalIpv6Prefix)" \
--region us-central1)
启动具有 ULA 内部 IPv6 地址的服务器虚拟机
在 Cloud Shell 中,执行以下操作:
gcloud compute instances create server-instance1 \
--subnet server-subnet1 \
--stack-type IPV4_IPV6 \
--zone us-central1-a \
--project=$projectname
添加防火墙规则以允许从客户端访问服务器
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules create allow-client-server1 \
--direction=INGRESS --priority=1000 \
--network=server-vpc1 --action=ALLOW \
--rules=tcp --source-ranges=$client_subnet \
--project=$projectname
添加防火墙规则以允许 IAP
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules create allow-iap-server1 \
--direction=INGRESS --priority=1000 \
--network=server-vpc1 --action=ALLOW \
--rules=tcp:22 \
--source-ranges=35.235.240.0/20 \
--project=$projectname
在 ULA 服务器实例中安装 Apache
在 Cloud Shell 中,登录客户端实例:
gcloud compute ssh server-instance1 \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在服务器虚拟机 Shell 中,运行以下命令
sudo apt update && sudo apt -y install apache2
验证 Apache 是否正在运行
sudo systemctl status apache2
覆盖默认网页
echo '<!doctype html><html><body><h1>Hello World! From Server1!</h1></body></html>' | sudo tee /var/www/html/index.html
退出 SSH 会话,继续完成此 Codelab。
5. GUA 服务器 VPC 设置
创建 GUA 服务器 VPC
在 Cloud Shell 中,执行以下操作:
gcloud compute networks create server-vpc2 \
--project=$projectname \
--subnet-mode=custom --mtu=1500 \
--bgp-routing-mode=regional
创建 GUA 服务器子网
在 Cloud Shell 中,执行以下操作:
gcloud compute networks subnets create server-subnet2 \
--network=server-vpc2 \
--project=$projectname \
--range=192.168.0.0/24 \
--stack-type=IPV4_IPV6 \
--ipv6-access-type=external \
--region=us-central1
使用以下命令将分配的 GUA 子网记录在环境变量中
export server_subnet2=$(gcloud compute networks subnets \
describe server-subnet2 \
--project $projectname \
--format="value(externalIpv6Prefix)" \
--region us-central1)
启动具有 GUA IPv6 地址的服务器虚拟机
在 Cloud Shell 中,执行以下操作:
gcloud compute instances create server-instance2 \
--subnet server-subnet2 \
--stack-type IPV4_IPV6 \
--zone us-central1-a \
--project=$projectname
添加防火墙规则以允许在子网内访问
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules create allow-client-server2 \
--direction=INGRESS \
--priority=1000 \
--network=server-vpc2 \
--action=ALLOW \
--rules=tcp --source-ranges=$client_subnet \
--project=$projectname
添加防火墙规则以允许 IAP
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules create allow-iap-server2 \
--direction=INGRESS \
--priority=1000 \
--network=server-vpc2 \
--action=ALLOW \
--rules=tcp:22 \
--source-ranges=35.235.240.0/20 \
--project=$projectname
确认对 GUA 服务器实例的 SSH 访问权限,并安装 Apache
在 Cloud Shell 中,登录客户端实例:
gcloud compute ssh server-instance2 \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在服务器虚拟机 Shell 中,运行以下命令
sudo apt update && sudo apt -y install apache2
验证 Apache 是否正在运行
sudo systemctl status apache2
覆盖默认网页
echo '<!doctype html><html><body><h1>Hello World! From Server2!</h1></body></html>' | sudo tee /var/www/html/index.html
退出 SSH 会话,继续完成此 Codelab。
6. 创建网关实例
删除客户端 VPC 的默认路由
为将 ULA v6 流量重定向到多 NIC 实例并停用互联网出站路由做准备。删除指向默认互联网网关的默认 ::/0 路由。
在 Cloud Shell 中,执行以下操作:
export client_defroutename=$(gcloud compute routes list \
--project $projectname \
--format='value(name)' \
--filter="network:client-vpc AND destRange~'::/0'")
gcloud compute routes delete $client_defroutename \
--project $projectname \
--quiet
启动网关多 NIC 虚拟机
在 Cloud Shell 中,执行以下操作:在 Cloud Shell 中,执行以下操作:
gcloud compute instances create gateway-instance \
--project=$projectname \
--zone=us-central1-a \
--network-interface=stack-type=IPV4_IPV6,subnet=client-subnet,no-address \
--network-interface=stack-type=IPV4_IPV6,subnet=server-subnet1,no-address \
--can-ip-forward
配置网关实例
在 Cloud Shell 中,登录网关实例(在实例启动期间,可能需要几分钟才能成功进行 SSH 连接):
gcloud compute ssh gateway-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在网关虚拟机 shell 中,运行以下命令以启用 IPv6 转发,并在启用转发的情况下继续接受 RA(accept_ra = 2)
sudo sysctl -w net.ipv6.conf.ens4.accept_ra=2
sudo sysctl -w net.ipv6.conf.ens5.accept_ra=2
sudo sysctl -w net.ipv6.conf.ens4.accept_ra_defrtr=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1
验证实例上的 IPv6 路由表
ip -6 route show
示例输出,显示 ULA 和 GUA 子网路由,默认路由指向 GUA 接口。
::1 dev lo proto kernel metric 256 pref medium
2600:1900:4000:7a7f:0:1:: dev ens4 proto kernel metric 256 expires 83903sec pref medium
2600:1900:4000:7a7f::/65 via fe80::4001:c0ff:fea8:101 dev ens4 proto ra metric 1024 expires 88sec pref medium
fd20:3df:8d5c::1:0:0 dev ens5 proto kernel metric 256 expires 83904sec pref medium
fd20:3df:8d5c::/64 via fe80::4001:c0ff:fea8:1 dev ens5 proto ra metric 1024 expires 84sec pref medium
fe80::/64 dev ens5 proto kernel metric 256 pref medium
fe80::/64 dev ens4 proto kernel metric 256 pref medium
default via fe80::4001:c0ff:fea8:101 dev ens4 proto ra metric 1024 expires 88sec pref medium
退出 SSH 会话,继续完成此 Codelab。
7. 创建并测试指向网关实例的路由(使用实例的名称)
在本部分中,您将使用网关实例名称作为下一个跃点,向客户端和服务器 VPC 添加路由。
记下服务器地址
在 Cloud Shell 中,执行以下操作:
gcloud compute instances list \
--project $projectname \
--filter="name~server-instance" \
--format='value[separator=","](name,networkInterfaces[0].ipv6Address,networkInterfaces[0].ipv6AccessConfigs[0].externalIpv6)'
这应该会输出服务器实例名称及其 IPv6 前缀。示例输出
server-instance1,fd20:3df:8d5c:0:0:0:0:0,
server-instance2,,2600:1900:4000:71fd:0:0:0:0
记下这两个地址,因为您稍后将在客户端实例的 curl 命令中使用它们。遗憾的是,环境变量无法轻松用于存储这些信息,因为它们不会通过 SSH 会话传输。
从客户端向 ULA 服务器实例运行 curl 命令
在添加任何新路线之前,先查看行为。从客户端实例向 server-instance1 运行 curl 命令。
在 Cloud Shell 中,登录客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,使用 server1 实例的 ULA IPv6 地址执行 curl 命令(该命令设置了 5 秒的短超时,以避免 curl 等待时间过长)
curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'
此 curl 命令应会超时,因为客户端 VPC 尚无指向服务器 VPC 的路由。
我们来尝试解决这个问题!现在退出 SSH 会话。
在客户端 VPC 中添加自定义路由
因为客户端 VPC 缺少指向 ULA 前缀的路由。现在,我们来添加它。
在 Cloud Shell 中,执行以下操作:
gcloud compute routes create client-to-server1-route \
--project=$projectname \
--destination-range=$server_subnet1 \
--network=client-vpc \
--next-hop-instance=gateway-instance \
--next-hop-instance-zone=us-central1-a
通过 SSH 返回客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,再次尝试 curl 到服务器实例。(该命令设置了 5 秒的短超时,以避免 curl 等待时间过长)
curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'
此 curl 命令仍会超时,因为 server1 VPC 尚无通过网关实例返回客户端 VPC 的路由。
退出 SSH 会话,继续完成此 Codelab。
在 ULA 服务器 VPC 中添加自定义路由
在 Cloud Shell 中,执行以下操作:
gcloud compute routes create server1-to-client-route \
--project=$projectname \
--destination-range=$client_subnet \
--network=server-vpc1 \
--next-hop-instance=gateway-instance \
--next-hop-instance-zone=us-central1-a
通过 SSH 返回客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,再次尝试对服务器实例执行 curl 命令。
curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'
此 curl 命令现在会成功运行,表明您已实现从客户端实例到 ULA 服务器实例的端到端可访问性。目前,只有使用将 next-hop-instance 作为下一个跃点的 IPv6 自定义路由,才能实现这种连接。
输出示例
<user id>@client-instance:~$ curl -m 5.0 -g -6 'http://[fd20:3df:8d5c:0:0:0:0:0]:80/'
<!doctype html><html><body><h1>Hello World! From Server1!</h1></body></html>
退出 SSH 会话,继续完成此 Codelab。
8. 创建并测试指向网关实例的路由(使用实例的地址)
在本部分中,您将使用网关实例 IPv6 地址作为下一个跃点,向客户端和服务器 VPC 添加路由。
删除之前的路线
我们来删除使用实例名称的自定义路由,将环境恢复到添加任何自定义路由之前的状态。
在 Cloud Shell 中,执行以下操作:
gcloud compute routes delete client-to-server1-route --quiet --project=$projectname
gcloud compute routes delete server1-to-client-route --quiet --project=$projectname
从客户端向 ULA 服务器实例运行 curl 命令
如需确认之前的路由是否已成功删除,请从客户端实例向 server-instance1 运行 curl 命令。
在 Cloud Shell 中,登录客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,使用 server1 实例的 ULA IPv6 地址执行 curl 命令(该命令设置了 5 秒的短超时,以避免 curl 等待时间过长)
curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'
此 curl 命令应超时,因为客户端 VPC 不再有指向服务器 VPC 的路由。
获取网关实例 IPv6 地址
我们需要先获取网关实例的 IPv6 地址,然后才能编写使用 next-hop-address 的路由。
在 Cloud Shell 中,执行以下操作:
export gateway_ula_address=$(gcloud compute instances \
describe gateway-instance \
--project $projectname \
--format='value(networkInterfaces[1].ipv6Address)')
export gateway_gua_address=$(gcloud compute instances \
describe gateway-instance \
--project $projectname \
--format='value(networkInterfaces[0].ipv6AccessConfigs[0].externalIpv6)')
在客户端 VPC 中添加自定义路由
现在,我们可以在客户端 VPC 中重新添加 ULA 前缀路由,但改用网关 GUA 地址作为下一个跃点。
在 Cloud Shell 中,执行以下操作:
gcloud compute routes create client-to-server1-route \
--project=$projectname \
--destination-range=$server_subnet1 \
--network=client-vpc \
--next-hop-address=$gateway_gua_address
通过 SSH 返回客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,再次尝试 curl 到服务器实例。
curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'
正如预期,此 curl 命令仍会超时,因为 server1 VPC 尚无通过网关实例返回客户端 VPC 的路由。
退出 SSH 会话,继续完成此 Codelab。
在 ULA 服务器 VPC 中添加自定义路由
在 Cloud Shell 中,执行以下操作:
gcloud compute routes create server1-to-client-route \
--project=$projectname \
--destination-range=$client_subnet \
--network=server-vpc1 \
--next-hop-address=$gateway_ula_address
通过 SSH 返回客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,再次尝试对服务器实例执行 curl 命令。
curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'
此 curl 命令现在会成功运行,表明您已实现从客户端实例到 ULA 服务器实例的端到端可访问性。目前,只有使用将 next-hop-address 用作下一个跃点的 IPv6 自定义路由,才能实现这种连接。
输出示例
<user id>@client-instance:~$ curl -m 5.0 -g -6 'http://[fd20:3df:8d5c:0:0:0:0:0]:80/'
<!doctype html><html><body><h1>Hello World! From Server1!</h1></body></html>
退出 SSH 会话,继续完成此 Codelab。
9. 创建并测试通往互联网网关的路由
在完成此实验室设置后,我们还可以测试新的下一跳属性:next-hop-gateway 的功能。
从客户端到 GUA 服务器实例运行 curl 命令
在添加任何新路线之前,先查看行为。从客户端实例向 server2 的 IP 地址运行 curl 命令。
在 Cloud Shell 中,登录客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,对 IPv6 端点执行 curl 命令
curl -m 5.0 -g -6 'http://[GUA-ipv6-address-of-server2]:80/'
此 curl 命令应会超时,因为客户端 VPC 只有自己的子网路由和指向 server1 的 VPC 的路由。如需能够访问 server2 VPC 的 GUA 范围,您需要通过自定义路由使用默认互联网网关。
退出 SSH 会话,继续完成此 Codelab。
在客户端 VPC 中添加自定义网关路由
在 Cloud Shell 中,执行以下操作:
gcloud compute routes create client-to-server2-route \
--project=$projectname \
--destination-range=$server_subnet2 \
--network=client-vpc \
--next-hop-gateway=default-internet-gateway
通过 SSH 返回客户端实例:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在客户端实例中,重复相同的 curl
curl -m 5.0 -g -6 'http://[GUA-ipv6-address-of-server2]:80/'
现在,此 curl 命令应该可以成功返回自定义 Hello 消息,这表示您可以通过默认互联网网关成功访问另一个服务器的 IPv6 地址。
示例输出:
<user id>@client-instance:~$ curl -m 5.0 -g -6 'http://[2600:1900:4000:71fd:0:0:0:0]:80/'
<!doctype html><html><body><h1>Hello World! From Server2!</h1></body></html>
退出 SSH 会话,然后完成实验的“清理”部分。
10. 清理
清理实例
在 Cloud Shell 中,执行以下操作:
gcloud compute instances delete client-instance --zone us-central1-a --quiet --project=$projectname
gcloud compute instances delete server-instance1 --zone us-central1-a --quiet --project=$projectname
gcloud compute instances delete server-instance2 --zone us-central1-a --quiet --project=$projectname
gcloud compute instances delete gateway-instance --zone us-central1-a --quiet --project=$projectname
清理子网
在 Cloud Shell 中,执行以下操作:
gcloud compute networks subnets delete client-subnet --region=us-central1 --quiet --project=$projectname
gcloud compute networks subnets delete server-subnet1 --region=us-central1 --quiet --project=$projectname
gcloud compute networks subnets delete server-subnet2 --region=us-central1 --quiet --project=$projectname
清理防火墙规则
在 Cloud Shell 中,执行以下操作:
gcloud compute firewall-rules delete allow-iap-client --quiet --project=$projectname
gcloud compute firewall-rules delete allow-iap-server1 --quiet --project=$projectname
gcloud compute firewall-rules delete allow-iap-server2 --quiet --project=$projectname
gcloud compute firewall-rules delete allow-gateway-client --quiet --project=$projectname
gcloud compute firewall-rules delete allow-client-server1 --quiet --project=$projectname
gcloud compute firewall-rules delete allow-client-server2 --quiet --project=$projectname
清理自定义路由
在 Cloud Shell 中,执行以下操作:
gcloud compute routes delete client-to-server1-route --quiet --project=$projectname
gcloud compute routes delete client-to-server2-route --quiet --project=$projectname
gcloud compute routes delete server1-to-client-route --quiet --project=$projectname
清理 VPC
在 Cloud Shell 中,执行以下操作:
gcloud compute networks delete client-vpc --quiet --project=$projectname
gcloud compute networks delete server-vpc1 --quiet --project=$projectname
gcloud compute networks delete server-vpc2 --quiet --project=$projectname
11. 恭喜
您已成功使用将下一个跃点设置为 next-hop-gateway、next-hop-instance 和 next-hop-address 的自定义 IPv6 静态路由。您还使用这些路由验证了端到端 IPv6 通信。
后续操作
查看下列 Codelab…