AWS VPC 中的第二个 ENI 无法在 Ubuntu 实例上访问

AWS VPC 中的第二个 ENI 无法在 Ubuntu 实例上访问

我刚刚开始使用 VPC,试图了解一切是如何运作的。到目前为止,我遇到的最大障碍是,每当我向一台机器添加第二个 Elastic NIC 时,VPC 中的任何其他 IP 都无法访问该第二个 IP。以下是我所做的

  • 推出了 Canonical 为 Ubuntu 12.10 x64 EBS 提供的 AMI。
  • 在启动期间,我将其配置为两个网络接口(同一子网)
  • 机器启动后,我将以下内容添加到 /etc/network/interfaces :

自动 eth1

iface eth1 inet dhcp

  • ifup eth1
  • 跑步是否配置,验证第二个地址是否启动。

在我的主要(可访问互联网)实例上:

  • ping(新实例 eth0 的 IP)- 有效
  • ping(新实例 eth1 的 IP)- 失败

没有阻止 ping 的 ACL,因为它与 eth0 一起工作。机器上没有设置防火墙。我尝试了 4 个跨多个 SG 和 AZ 的不同实例,这些实例具有多个接口,所有结果都相同。

我已经为此绞尽脑汁很久了,我都不想承认。我实在想不出错误在哪里。

答案1

路由表默认只会将流量路由到eth0。即使 ubuntu 检测到其他 ENI,您仍然必须将流量路由到它。

您必须进行一些高级路由:

1)立即暂时启用对第二个 ENI 的访问。

来源:http://www.rjsystems.nl/en/2100-adv-routing.php

# this will show your route table, i'll assume you have eth0 and eth1
# and your default is for eth0 to point to the gateway
# for this example lets assume the following:
# eth0 = 192.168.100.5 
# eth1 = 192.168.100.10
# gateway = 192.168.100.1
ip route show ;

# first step is to create a routing table for your new device
cat /etc/iproute2/rt_tables ;
echo 2 eth1_rt >> /etc/iproute2/rt_tables ;

# next add the eth1_rt route table, so by default it will also point to the gateway
ip route add default via 192.168.100.1 dev eth1 table eth1_rt ;

# next take a look at your ip rules
# i'll assume the defaults here, and things flows to default with priority 32767
ip rule;

# let's add a rule, if we see traffic from eth1's IP address,
# use its new routing table we setup, and give it higher priority than default
ip rule add from 192.168.100.10 lookup eth1_rt prio 1000 ;

# done! now check your traffic from both IPs, they should both work.

2)重启时持续启用对第二个 ENI 的访问。

来源:http://blog.bluemalkin.net/multiple-ips-and-enis-on-ec2-in-a-vpc/

此外,如果您希望此更改持久化,您可以在接口文件中进行所有这些更改,然后重新启动网络服务或重新启动即可使其生效。

# NOTE: add the eth1_rt routing table to /etc/iproute2/rt_tables as show in previous section

# original config to make dchp, I add mine to /etc/network/interfaces.d/eth1.cfg
auto eth1
iface eth1 inet dchp
    # your extra rules for eth1
    up ip route add default via 192.168.100.1 dev eth1 table eth1_rt
    up ip rule add from 192.168.100.10 lookup eth1_rt prio 1000

为了使其完全生效,请重新启动系统。

笔记:我试过了,/etc/init.d/networking restart;但它没有接受路由/规则更改,不知道为什么,所以我重新启动了。如果你想让它立即生效并持续生效,请同时使用这两种方法。

答案2

以下是我的方法。我使用静态 IP。最初我使用 dhcp 来获取 2 个项目。IP 地址和网络掩码。以下是分步说明:

启动实例

sudo cp /etc/network/interfaces.d/eth0.cfg /etc/network/interfaces.d/eth1.cfg
sudo sed -i "s/eth0/eth1/g" /etc/network/interfaces.d/eth1.cfg
sudo ifdown eth1
sudo ifup eth1
sudo ifconfig eth1 | tee /tmp/ifconfig-eth1-output.txt
sudo ifdown eth1
sudo rm -f /etc/network/interfaces.d/eth1.cfg 
ETH0_IP=$(cat /tmp/ifconfig-eth1-output.txt | grep "inet addr:" | cut -f 2 -d':' | cut -f 1 -d' ')
ETH0_NETMASK=$(cat /tmp/ifconfig-eth1-output.txt| grep "inet addr:" | cut -f 4 -d':')
ETH0_GATEWAY=$(echo ${ETH0_IP} | sed "s/[0-9]*$/1/g")
echo "auto eth1" > /tmp/eth1.cfg
echo "iface eth1 inet static" >> /tmp/eth1.cfg  
echo "address ${ETH0_IP}" >> /tmp/eth1.cfg 
echo "netmask ${ETH0_NETMASK}" >> /tmp/eth1.cfg 
echo "up ip route add default via ${ETH0_GATEWAY} dev eth1 table 1  metric 10001" >> /tmp/eth1.cfg 
echo "up ip rule add from ${ETH0_IP}/32 table 1 priority 10001" >> /tmp/eth1.cfg 
echo "up ip route flush cache" >> /tmp/eth1.cfg 
sudo cp -f /tmp/eth1.cfg /etc/network/interfaces.d/
sudo ifup eth1

这样就可以了,运行这些来检查。

curl --silent http://ipinfo.io
curl --interface eth0 --silent http://ipinfo.io
curl --interface eth1 --silent http://ipinfo.io

这些将立即生效,并在重启后持续有效。

相关内容