"LinuX Containers (LXC) provide lightweight virtualization that lets you isolate processes and resources without the need to provide instruction interpretation mechanisms and other complexities of full virtualization."
In this tutorial I provide step by step instructions to install LXC on an Ubuntu Natty server.
Basic *nix knowledge is assumed though.
Install required packages
apt-get install lxc debootstrap bridge-utils screen
Add a new bridge for LXC, including NAT rule
Each container will have its own local ip, which will be bound to a bridge network interface on the main server called "br-lxc".
The IP of the bridge is 192.168.254.1, the first container will get IP 192.168.254.2, the second will get 192.168.254.3, etc.
In /etc/network/interfaces add the following:
auto br-lxc
iface br-lxc inet static
address 192.168.254.1
netmask 255.255.255.0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
pre-down echo 0 > /proc/sys/net/ipv4/ip_forward
pre-down iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
bridge_ports none
bridge_stp off
Bring up the bridge:
ifup br-lxc
Create a mountpoint and mount cgroup
mkdir /cgroup echo "cgroup /cgroup cgroup" >> /etc/fstab mount /cgroup
Create the first container
First create an LXC configuration file for the container named <container name>.conf:
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br-lxc
lxc.network.ipv4 = 192.168.254.2/24
lxc.network.name = eth0
lxc.cgroup.cpu.shares = 512
lxc.cgroup.memory.limit_in_bytes = 1024M
lxc.cgroup.memory.memsw.limit_in_bytes = 3072M
This container is limited on cpu shares (512 vs the default 1024), a maximum amount of 1024M RAM and a maximum total amount of memory (RAM+swap) of 3072M
Next we create the container:
lxc-create -n <container name> -t natty -f <container name>.conf
The container will contain its own minimal version of Ubuntu Natty. The files of the container can be found in /var/lib/lxc/<container name>/rootfs/.
Configure networking
Edit the file /var/lib/lxc/<container name>/rootfs/etc/network/interfaces and make it look like this:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.254.2
netmask 255.255.255.0
gateway 192.168.254.1
You can then forward ports to your container with basic iptables:
iptables -t nat -I PREROUTING -p tcp -d <external IP> -j DNAT --dport <port> --to-destination 192.168.254.2
Sharing foders between host and container (optional)
If you want a folder to be shared between the host and a container, or between multiple containers, you can do that with a bind mount. For example, to share the folder /var/data, add the following to /etc/fstab:
/var/data/ /var/lib/lxc/<container name>/rootfs/var/data/ none bind 0 0
Make sure the directories exist (mkdir), and mount them:
mount /var/lib/lxc/<container name>/rootfs/var/data/
Start the container
I always start the container in a screen, so it's accessible without using SSH.
screen -dmS init-<container name> lxc-start -n <container name>
A screen session named init-<container name> will be created in the background.
You can log in with root/root. Either ssh to it, or if that doesn't work connect to the screen. I had the problem that the network didn't always come up. When this happens, log in through the screen and type ifdown eth0 && ifup eth0, and everything should work.
Credits:


Comments
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Tue, 07/12/2011 - 14:06
Hi Nice tutorial ,but I am not able to access internet on my containers..What to do?how can I solve this issue?
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by lennart on Tue, 07/12/2011 - 22:35
What does
cat /proc/sys/net/ipv4/ip_forwardsay?Can you ping the local ip's of your containers from the host?
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Wed, 07/13/2011 - 10:15
cat /proc/sys/net/ipv4/ip_forward shows 1..Lemme explain you my requirement I want to install LXC on Amazon EC2.also I want container should be in same network as host also container should get different IP and accessible via SSH aand internet sshould wwork,
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by lennart on Wed, 07/13/2011 - 10:31
Assigning an IP to your container can be done with iptables:
E.g. if 71.45.121.151 is your external IP you want to assign to the container with local IP 192.168.254.2:
Internet from the container should work... (try
ping 74.125.39.105) but perhaps your dns settings are wrong? Are the nameservers configured in /etc/resolv.conf ?If not, this might work:
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Wed, 07/13/2011 - 10:43
Yes my DNS is in /etc/resolv.conf also I am not able to ping the LXC from host vise verca ,ssuppose if my host IP is 192.168.116.112 iss it not possible to give the same range IP to containers say 192.168.116.113?
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by lennart on Wed, 07/13/2011 - 10:53
No, you container should be in the same range than the bridge on the host. The bridge interface (br-lxc in my example), is on the host and has IP
192.168.254.1. Correct? Is it up and running? The containers then should get IP192.168.254.2,192.168.254.3, ... is the interface up on the containers? (connect to the container andifconfig). If that works, you can forward 192.168.116.113 to your container with iptables tho (see previous post).PS: I was talking about resolv.conf inside the containers btw ;)
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Wed, 07/13/2011 - 11:03
Ohh Thanks alot ..What if I give the same range IP to bridge on host or is it possible container get IP from my DHCP server?Just for curiosity asking this?
PS:Yes I got that:-)
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by lennart on Wed, 07/13/2011 - 12:10
Then you'll have to make sure your routing table is set up correctly. My advice is not to try that, and use iptables to forward the external IP to the container. Or even better, only forward the ports you want to use on that container.
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Wed, 07/13/2011 - 12:27
Thanks for your help LXC containers working properly wwith ur tutorial,ca u please tell me about the Routing taable method ?
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Thu, 07/14/2011 - 10:13
Thanks for your help LXC containers working properly wwith ur tutorial,ca u please tell me about the Routing taable method ?
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by lennart on Thu, 07/14/2011 - 10:31
Not completely sure if that's even possible, but... you'll probably have to add something like this:
But I doubt that alone will do the trick ;)
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Thu, 07/14/2011 - 10:54
Yes man that did the trick ..Thanks Alot ..for all ur help.now the only issue is I am not able to do SSH to the container from network that need to check
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Wed, 08/10/2011 - 10:56
If I need to do SSH to the container from network how I will do that?
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Fri, 08/12/2011 - 06:51
Hi I have succesfully created LXC on ubuntu 11.04 andd installed application on it like mysql,php,apache,java etc..is there any way so that I can make template of my LXC adn at the time of installation of 2ndd LXC I can deploy from it,so that I dont need to do aall the configuration part again and again.
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Thu, 12/29/2011 - 23:32
I just can thank you enough for this tutorial ... great great article
thanks,
Re: LXC installation on Ubuntu Server 11.04 Permalink
Submitted by Anonymous (not verified) on Tue, 01/10/2012 - 18:25
Beautiful. I owe you a beer.
Add new comment