首页 > 解决方案 > 如何在 Google Cloud Platform 上重新启动时保留多个网络接口?

问题描述

我正在尝试在 Google Cloud Platform(启用免费试用和计费)上设置一个 Compute Engine VM 实例,该实例具有多个(超过 2 个)网络接口,即使在重新启动后也可以全部连接到互联网。到目前为止,我尝试过的所有操作都在重新启动后失败了。

首先,我创建多个网络:

gcloud compute networks create vpc-1 --subnet-mode custom && gcloud compute firewall-rules create vpc-1-firewall --network vpc-1 --allow tcp:22,tcp:80,tcp:443,icmp && gcloud compute networks subnets create vpc-1-subnet --network vpc-1 --range 10.0.1.0/24 --region us-east4

gcloud compute networks create vpc-2 --subnet-mode custom && gcloud compute firewall-rules create vpc-2-firewall --network vpc-2 --allow tcp:22,tcp:80,tcp:443,icmp && gcloud compute networks subnets create vpc-2-subnet --network vpc-2 --range 10.0.2.0/24 --region us-east4

gcloud compute networks create vpc-3 --subnet-mode custom && gcloud compute firewall-rules create vpc-3-firewall --network vpc-3 --allow tcp:22,tcp:80,tcp:443,icmp && gcloud compute networks subnets create vpc-3-subnet --network vpc-3 --range 10.0.3.0/24 --region us-east4

gcloud compute networks create vpc-4 --subnet-mode custom && gcloud compute firewall-rules create vpc-4-firewall --network vpc-4 --allow tcp:22,tcp:80,tcp:443,icmp && gcloud compute networks subnets create vpc-4-subnet --network vpc-4 --range 10.0.4.0/24 --region us-east4

然后,我使用这些网络创建一个 VM 实例:

gcloud compute instances create test-1 --boot-disk-device-name=test-1 --image=ubuntu-1804-bionic-v20190628 --image-project=ubuntu-os-cloud --boot-disk-size=10GB --boot-disk-type=pd-ssd --zone=us-east4-c --machine-type=n1-standard-16 --network-interface subnet=vpc-1-subnet --network-interface subnet=vpc-2-subnet --network-interface subnet=vpc-3-subnet --network-interface subnet=vpc-4-subnet --network-tier=PREMIUM --min-cpu-platform='Intel Skylake'

到目前为止,这工作得很好。当我通过 SSH 连接到 VM 实例时,我可以在第一个界面(ens4在我的例子中)上向网站发出请求。UP但是,其他接口即使存在并且是via也不起作用ip link show。然后,我按照https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#configuring_policy_routing中的说明进行操作ens5ens6ens7使用以下 bash 脚本作为具有相关 IP 地址的根:

ifconfig ens5 10.0.2.6 netmask 255.255.255.255 broadcast 10.0.2.6 mtu 1460
echo "1 rt1" | tee -a /etc/iproute2/rt_tables
ip route add 10.0.2.1 src 10.0.2.6 dev ens5 table rt1
ip route add default via 10.0.2.1 dev ens5 table rt1
ip rule add from 10.0.2.6/32 table rt1
ip rule add to 10.0.2.6/32 table rt1

ifconfig ens6 10.0.3.6 netmask 255.255.255.255 broadcast 10.0.3.6 mtu 1460
echo "2 rt2" | tee -a /etc/iproute2/rt_tables
ip route add 10.0.3.1 src 10.0.3.6 dev ens6 table rt2
ip route add default via 10.0.3.1 dev ens6 table rt2
ip rule add from 10.0.3.6/32 table rt2
ip rule add to 10.0.3.6/32 table rt2

ifconfig ens7 10.0.4.6 netmask 255.255.255.255 broadcast 10.0.4.6 mtu 1460
echo "3 rt3" | tee -a /etc/iproute2/rt_tables
ip route add 10.0.4.1 src 10.0.4.6 dev ens7 table rt3
ip route add default via 10.0.4.1 dev ens7 table rt3
ip rule add from 10.0.4.6/32 table rt3
ip rule add to 10.0.4.6/32 table rt3

这行得通!我可以使用所有网络接口(ens4ens5ens6ens7)发出请求。但是,在我重新启动后,在运行上述 bash 脚本之前和之后,所有网络接口都无法连接到任何网站(没有每个部分的前两行)。例如,如果我尝试运行curl https://google.com在重新启动之前有效的命令,我会收到以下错误:curl: (6) Could not resolve host: google.com在我尝试使用的任何接口上。即使我在没有修改任何内容或运行任何脚本的情况下重新启动,我也会遇到同样的问题(在 上ens4也是如此)。我怎样才能坚持(如果可能的话,自动化)我在重新启动后完成的事情?

标签: networkinggoogle-cloud-platformgcloudgoogle-cloud-networking

解决方案


我已经复制了您的上下文,但是创建 VM 的 gcloud 命令似乎不准确,至少对于前面定义的子网:

--机器类型=n1-标准-1

根据Creating Instances with Multiple Network Interfaces,不允许您创建具有两个以上 NIC 的 VM 。

--zone=us-central1-a

这也会引发错误。您的子网已在 us-east4 区域中创建。

显然,修改命令以适应 VPC 和 VM 创建将修复这种不匹配。没什么大不了的。

如何在 VM 的部署/重新启动时自动生成路由的方法是配置自定义元数据,根据Running Startup Scripts每次重新启动实例时自动创建路由。

请按照 GCP Console 中的路径进行操作:

Compute Engine → VM 实例 → 点击实例名称 → 编辑 → 自定义元数据 → 添加项目

并添加以下数据:

Key=       startup-script

Value=     ip route add 10.0.2.1 src 10.0.2.6 dev ens5 table rt1

           ip route add default via 10.0.2.1 dev ens5 table rt1

           ip rule add from 10.0.2.6/32 table rt1

           ip rule add to 10.0.2.6/32 table rt1

对所有其他 NIC 执行相同操作。“echo”命令不是必需的,因为它会将寄存器写入“/etc/iproute2/rt_tables”文件并且这是持久的。然而,“ip route”的变化是不稳定的。每次重启后它们都会消失。使用启动脚本可以让您在启动启动的同时运行命令。


推荐阅读