Friday, September 30, 2011

Your own network space within LXC / EC2

Not really on the subject of Perl but very interesting still.

The below works with Ubuntu Lucid, but I'd expect the networking will working with any Linux box. You can ignore the apt stuff if you don't use Ubuntu.

This LXC configuration may help if you have a server at say EC2 and have multiple containers within that server and you want your own local network address space.

Then you may want to do port forwarding to the container too so I've put some stuff about it.

External address 74.125.237.16
Internal address 192.168.1.1 #-kind of acts like your router

Linux server configuration /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 74.125.237.16
netmask xxx.xxx.xxx.xxx #you'll have your our values
gateway xxx.xxx.xxx.xxx

auto br0
iface br0 inet static
address 192.168.1.1
netmask 255.255.255.0
bridge_stp off
bridge_maxwait 0
pre-up /usr/sbin/brctl addbr br0
post-up /usr/sbin/brctl setfd br0 0
post-up /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
post-up echo 1 > /proc/sys/net/ipv4/ip_forward


There is some magic here that was adapted from Daniel Lezcano information

Container configuration /etc/network/interfaces
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or
# /usr/share/doc/ifupdown/examples for more information.

auto lo
iface lo inet loopback

#auto eth0
#iface eth0 inet dhcp

auto eth0
iface eth0 inet static
address 192.168.1.209
netmask 255.255.255.0
gateway 192.168.1.1
broadcast 102.168.0.255
network 192.168.1.0

Don't forget to setup your containers resolv.conf, it should point to the nameservers your Linux server uses.

Also just a little gripe and some information that may come in handy if your using Lucid. LXC doesn't work by default because the Lucid LTS build doesn't have namespaces and I'm not entirely sure if LXC are going to work around it or if Lucid will have it rebuilt back into the kernel. Anyway to work around it you need to use an older kernel. This is my work around, I'm sure there are others out there, but rebuilding your kernel may not be your idea of fun.

Do this at your own risk too if you don't install your old kernel properly you may need to do some livecd magic to ensure a kernel is on the system.



sudo apt-get install python-software-properties
sudo add-apt-repository ppa:kernel-ppa/ppa
sudo apt-get install linux-image-2.6.32-21-generic
#you may need to remove more kernels unless you can be bothered setting up grub2 to always boot the old kernel
sudo apt-get remove linux-image-2.6.32-33-generic
sudo update-grub



And just for good measure I think Serge has maybe been doing something to fix stuff with Lucid so you could throw in his update if you like



sudo add-apt-repository ppa:serge-hallyn/lxc-lucid-updates
sudo apt-get update
sudo apt-get install lxc


You can then use iptable to port forward stuff from the external address to the internal address. Just like a router port forward and you can do port re-address too.

Example iptables setup on Linux Server to Linux Container (192.168.1.2) - port forward port 4566 to port 4566


sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 5566 -j DNAT --to-destination 192.168.1.2:5566


Many thanks to all the other people on the Internet that have provided information on this subject. Disclaimer I haven't tried this on EC2, but I expect it still works.