需要配置第二块网卡才能连接LXC
我在装有4块网卡的计算机上安装了Ubuntu 16.04服务器。我有连接到同一个交换机的接口eth0
和eth1
。接口eth0
用于远程SSH连接来管理服务器。我想用eth1
桥接br0
。这座桥我想用于LXC容器。在DHCP环境中这种设置不会导致任何问题。挑战在于该服务器安装的网络完全是静态的。我收到了具有相同子网掩码和网关的此服务器的IP范围。需要配置第二块网卡才能连接LXC
设置eth0
是没有问题的:
auto eth0
iface eth0 inet static
address 195.x.x.2
network 195.x.x.0
netmask 255.255.255.0
gateway 195.x.x.1
broadcast 195.x.x.255
dns-nameservers 150.x.x.105 150.x.x.106
这个问题是与第二接口eth1
,因为它具有相同的网关eth0
Ubuntu的警告,只有一个默认网关设置(这是合乎逻辑) 。为此我已成立eth1
如下:
auto eth1
iface eth1 net static
address 195.x.x.3
network 195.x.x.0
netmask 255.255.255.0
broadcast 195.x.x.255
问题与此设置是,我可以在外部的ping eth0的IP处,但195.x.x.2
eth1
不能ping或者通过SSH访问。我设法使它适用于很多路由技巧,但许多文章都写道,如果你有静态网桥和容器的话,这种方式会变得更深。
我的问题是:有没有人对我的问题有直接的方法?我应该如何配置eth0
和eth1
以将静态IP号码的容器桥接到eth1
?
好吧,我按照以下方式解决了这个问题,仍然按照问题描述的方式继续使用网关路由解决方案。也许有同样问题的人也可以使用这种方法,或者如果有人知道更好的解决方案可以随意发表评论。
在主机:
我启用ARP过滤:
sysctl -w net.ip4.conf.all.arp_filter=1
echo "net.ipv4.conf.all.arp_filter = 1" >> /etc/sysctl.conf
配置/etc/network/interfaces
:
auto lo
iface lo net loopback
# The primary network interface
auto etc0
iface eth0 inet static
address 195.x.x.2
network 195.x.x.0
netmask 255.255.255.0
gateway 195.x.x.1
broadcast 195.x.x.255
up ip route add 195.x.x.0/24 dev eth0 src 195.x.x.2 table eth0table
up ip route add default via 195.x.x.1 dev eth0 table eth0table
up ip rule add from 195.x.x.2 table eth0table
up ip route add 195.x.x.0/24 dev eth0 src 195.0.0.2
dns-nameservers 150.x.x.105 150.x.x.106
# The secondary network interface
auto eth1
iface eth1 net manual
# LXC bridge interface
auto br0
iface br0 inet static
address 195.x.x.3
network 195.x.x.0
netmask 255.255.255.0
bridge_ifaces eth1
bridge_ports eth1
bridge_stp off
bridge_fd 0
bridge_maxwait 0
up ip route add 195.x.x.0/24 dev br0 src 195.x.x.3 table br0table
up ip route add default via 195.x.x.1 dev br0 table br0table
up ip rule add from 195.x.x.3 table br0table
up ip route add 195.x.x.0/24 dev br0 src 195.0.0.3
添加以下行/etc/iproute2/rt_tables
:
...
10 et0table
20 br0table
在容器配置文件(/var/lib/lxc/[container name]/config
):
...
lxc.network.type = vets
lxc.network.link = br0
lxc.network.flags = up
lxc.network.hwadr = [auto create when bringing up container]
lxc.network.ipv4 = 195.x.x.4/24
lxc.network.ipv4.gateway = 195.x.x.1
lxc.network.veth.pair = [readable server name] (when using ifconfig)
lxc.start.auto = 0 (1 if you want the server to autostart)
lxc.start.delay = 0 (fill in seconds you want the container to wait before start)
我通过使在容器上的apache2测试了它和来自网络外部访问的网页。希望它能帮助任何遇到我所遇到的同样挑战的人。注意:如果您选择让容器的配置文件分配IP,请在容器本身的界面文件中将其禁用,否则请不要忘记。
auto lo
iface lo inet loopback
auto eth0
iface eth0 net manual