【问题标题】:How to persist multiple network interfaces across reboots on Google Cloud Platform?如何在 Google Cloud Platform 上重新启动时保留多个网络接口?
【发布时间】:2019-07-05 06:00:09
【问题描述】:

我正在尝试在 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)上向网站发出请求。但是,其他接口即使存在并且通过ip link showUP 也不起作用。然后我按照https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#configuring_policy_routing 的说明操作ens5ens6ens7,以root 身份使用以下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

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

【问题讨论】:

    标签: networking google-cloud-platform gcloud google-cloud-networking


    【解决方案1】:

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

    --machine-type=n1-standard-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”的变化是不稳定的。每次重启后它们都会消失。使用启动脚本可以让您在启动启动的同时运行命令。

    【讨论】:

    • 感谢您的回答。 create 命令确实是一个错字,我已经修复了这个问题以反映正确的信息。我已按照您的指示进行操作,但仍然遇到相同的 curl: (6) Could not resolve host: google.com 错误。即使我在不​​修改任何内容或运行任何脚本的情况下重新启动上面创建的新 VM 实例,我也会在所有接口上遇到相同的问题!
    • 嗯,看来问题不在于额外的网卡本身,而在于DNS服务。您是否能够执行 CURL 命令,但 使用 IP 地址 而不是域名?例如。 '卷曲 74.125.141.94'。此外,尝试从您的 VM ping 外部主机,但使用 IP 地址而不是域名。您需要隔离问题以了解需要验证的内容。
    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多