繁体   English   中英

如何在Google Cloud Platform重启后保持多个网络接口?

[英]How to persist multiple network interfaces across reboots on Google Cloud Platform?

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

首先,我创建多个网络:

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 )上向网站发出请求。 但是,即使其他接口存在并且通过ip link show UP也无法正常工作。 然后,我按照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 https://google.com收到以下错误: curl: (6) Could not resolve host: google.com在我所使用的任何接口上curl: (6) Could not resolve host: google.com尝试一下。 即使我重新启动而没有进行任何修改或运行任何脚本,我也遇到了同样的问题(在ens4 )。 如何保持(并在可能的情况下实现自动化)在重新启动后完成的工作?

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

--machine-type = n1-standard-1

根据创建具有多个网络接口的实例,不允许您创建具有两个以上NIC的VM。

--zone = us-central1-a

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

显然,修改命令以适合VPC和VM创建将解决此不匹配问题。 没什么大不了的。

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

请遵循GCP控制台中的路径:

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”更改是易变的。 它们将在每次重新启动后消失。 使用启动脚本可以使您在启动启动的同时运行命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM