LXC。来自与主机相同网络的Container的IP

时间:2015-11-08 18:15:28

标签: ubuntu containers lxc

我正在尝试LXC。现在我想从与LXC主机相同的网络为来宾(容器)分配IP。作为主机操作系统,我使用Ubuntu 14.04.3,并作为访客 - Ubuntu 15.10。

LXC主机使用我的家用路由器进行Internet访问(默认网关),LXC主机使用IP - 192.168.1.50 (网络 - 192.168.1.0/24 ),和网关(路由器)地址 - 192.168.1.1

所以现在我想将 192.168.1.51 从同一网络分配给LXC来宾。为此,我在主机LXC机器上配置了 br0 接口:

root@lxc-host:~# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
        address 192.168.1.50
        netmask 255.255.255.0
        gateway 192.168.1.1
        dns-nameservers 8.8.8.8
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

完成此配置后,Internet和内部网络正在运行:

root@lxc-host:~# ifconfig
br0       Link encap:Ethernet  HWaddr 08:00:27:5a:39:b5
          inet addr:192.168.1.50  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe5a:39b5/64 Scope:Link
          inet6 addr: fdee:cbcd:a595:0:a00:27ff:fe5a:39b5/64 Scope:Global
          inet6 addr: fdee:cbcd:a595:0:91b8:6067:2b5c:e58d/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5001 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2094 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:613920 (613.9 KB)  TX bytes:307810 (307.8 KB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:5a:39:b5
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4964 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2109 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:681460 (681.4 KB)  TX bytes:316156 (316.1 KB)

...

root@lxc-host:~#

根据this docs,我已将容器配置(u1是我的ubuntu容器)更改为此类视图:

root@lxc-host:~# cat /var/lib/lxc/u1/config
# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template:
# For additional config options, please look at lxc.container.conf(5)

# Distribution configuration
lxc.include = /usr/share/lxc/config/ubuntu.common.conf
lxc.arch = x86_64

# Container specific configuration
lxc.rootfs = /var/lib/lxc/u1/rootfs
lxc.utsname = u1

# Network configuration
lxc.network.type = veth
lxc.network.link = br0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:a1:c2:fe
lxc.network.ipv4 = 192.168.1.51/24

# define a gateway to have access to the internet
lxc.network.ipv4.gateway = 192.168.1.1

容器的网络配置现在看起来像这样:

root@lxc-host:~# cat /var/lib/lxc/u1/rootfs/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.51
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8
root@lxc-host:~#

容器重启后,eth0确实使用 192.168.1.51 ,容器可以ping LXC主机IP 192.168.1.50 但不能ping任何其他IP,包括内部网关 192.168.1.1 等IP。等等。

root@u1:~# ip a
1: lo...
5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:a1:c2:fe brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.51/24 brd 192.168.1.255 scope global eth0
...

root@u1:~# ping 192.168.1.50
PING 192.168.1.50 (192.168.1.50) 56(84) bytes of data.
64 bytes from 192.168.1.50: icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from 192.168.1.50: icmp_seq=2 ttl=64 time=0.064 ms
^C
--- 192.168.1.50 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.064/0.064/0.064/0.000 ms

root@u1:~# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
From 192.168.1.51 icmp_seq=1 Destination Host Unreachable
From 192.168.1.51 icmp_seq=2 Destination Host Unreachable
From 192.168.1.51 icmp_seq=3 Destination Host Unreachable
^C
--- 192.168.1.1 ping statistics ---
4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2999ms
pipe 3

root@u1:~# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms

root@u1:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
root@u1:~#

我哪里错了? Apparmor已停止,LLCC主机上的iptables是干净的。

2 个答案:

答案 0 :(得分:1)

如果您是LXC的新手,我建议您阅读Stephane Graber的10部分博客系列:

https://www.stgraber.org/2013/12/20/lxc-1-0-blog-post-series/

和Flockport创建了一个很棒的网站,其中包含很多LXC主题,特别是在从MACVlan到VxLAN到VPN等的LXC网络中。

https://www.flockport.com/guides/

答案 1 :(得分:0)

这个问题与Virtualbox可视化有关(但我知道有些人在使用Hyper-V时遇到这样的问题)。

在这种情况下,看起来LXC无法共享br0接口。

使用真正的硬件我不再有任何问题了。