Linux – Configuring bridging between a linux containers (LXC) interface and the hosts

linuxlxcnetworking

I want to run 2 LXC containers under a host, and I want each to have their own IP on the network, let's say the host has 10.10.1.10 and the containers have 10.10.1.11 and 10.10.1.12.

Does my setup require that I configure 2 bridged interfaces in the hosts /etc/network/interfaces file, something like this:

auto br0
iface br0 inet static
   address 10.10.1.11 #ip of our host
   netmask 255.255.255.0 #subnet of our host
   network 10.10.1.0 #network of our host
   broadcast 10.10.1.255 #our broadcast address
   gateway 10.10.1.1 #default gw for our host to access the internet
   bridge_ports eth0
   bridge_fd 9
   bridge_hello 2
   bridge_maxage 12
   bridge_stp off

auto br1
iface br0 inet static
   address 10.10.1.12 #ip of our host
   netmask 255.255.255.0 #subnet of our host
   network 10.10.1.0 #network of our host
   broadcast 10.10.1.255 #our broadcast address
   gateway 10.0.1.1 #default gw for our host to access the internet
   bridge_ports eth0
   bridge_fd 9
   bridge_hello 2
   bridge_maxage 12
   bridge_stp off

Or is there another way to configure the host more like a switch and allow the individual containers to configure their own IPs (that would be safer so we're not always changing the interfaces file whenever we move containers around).

Best Answer

Use one bridge on the host.

The LXC config file dictates what the guest/containers IP addressing will be.

# ifconfig 
br0       Link encap:Ethernet  HWaddr 22:C7:47:62:9C:02  
          inet addr:10.2.0.6  Bcast:10.2.127.255  Mask:255.255.128.0
          inet6 addr: fe80::e611:5bff:febe:b62a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1756184372 errors:0 dropped:0 overruns:0 frame:0
          TX packets:470262588 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:112684582109 (104.9 GiB)  TX bytes:657886062154 (612.7 GiB)

And a sample LXC config file...

# cat /etc/lxc/Analytics.conf 
lxc.utsname = Analytics
lxc.tty = 4
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = 1500
lxc.network.ipv4 = 10.2.0.80/17
lxc.network.hwaddr = 00:30:6E:08:EC:80
lxc.rootfs = /srv/lxc/Analytics
lxc.mount = /etc/lxc/Analytics.fstab
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
# /dev/pts/*
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm