我正在尝试在Google平台上设置一个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)。但是,其他接口即使存在,也不能工作,并且是通过UP通过ip link show实现的。然后,我按照路由 for ens5、ens6和ens7的说明,使用下面的bash脚本作为root,其中包含相关的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这行得通!我可以使用所有网络接口(ens4、ens5、ens6和ens7)发出请求。但是,在我重新启动后,所有网络接口在运行上述bash脚本之前和之后都无法连接到任何网站(没有每个部分的前两行)。如果我尝试运行curl https://google.com,例如,在重新启动之前运行的命令,我会收到以下错误:在我尝试过的任何接口上都有curl: (6) Could not resolve host: google.com。即使我没有修改任何东西或运行任何脚本就重新启动,我也会遇到同样的问题(在ens4上也是)。如果可能的话,我如何持久化(如果可能的话)在重新启动过程中完成的任务?
发布于 2019-07-05 17:41:12
但是,我已经复制了您的上下文,但是创建VM的gcloud命令似乎不准确,至少对于前面定义的子网是这样的:
-机器-类型=N1-标准-1
将不允许您按照使用多个网络接口创建实例创建一个具有两个以上NIC的VM。
-区域=我们-中心1-a
这也会引发一个错误。你的子网是在美国东部地区建立的。
显然,修改命令以适应VPC和VM创建将修复这种不匹配。没什么大不了的。
如何在VM的部署/重新启动时自动生成路由的方法是配置自定义元数据,在实例重新启动时按照运行启动脚本自动创建路由。
请按照GCP控制台中的路径:
计算引擎→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/iucte2/rt_tables“文件中,这是持久的。然而,"ip路由“的变化是不稳定的。每次重新启动后,它们就会消失。使用启动脚本将允许您在启动启动的同时运行这些命令。
https://stackoverflow.com/questions/56897281
复制相似问题