This article is a basic step-by-step HOWTO to create a server capable of hosting LXC-based containers.
This article has been superseded by Building an LXC Server - Ubuntu 14.04 Edition
- Prerequisites and Dependencies
- Install LXC
- Configure LXC
- Using LXC
- Port Forwarding
Prerequisites and Dependencies
This server will be using Ubuntu 12.04. Since 12.04 is getting pretty old, some work will be done to bring it up to date and configure it to use more modern components.
First, make sure all of the base packages are up to date:
Ubuntu has backported the 3.8 kernel from Ubuntu 13.04 to 12.04. To install it, do:
LXC doesn't natively use Open vSwitch, but I like having it installed as a more modern replacement to the standard Linux kernel bridging. Ubuntu 12.04 ships with Open vSwitch 1.4 which is really old. To install 1.9, do:
The newer LXC builds support ZFS as a backing store. This means that deduplication, compression, and snapshotting can all be taken advantage of. To install ZFS on Ubuntu 12.04, do:
I recommend adding an apt repository that will have the daily LXC builds:
Once the repository is added, installing LXC is as simple as:
By default, LXC will look for a zpool titled
Make sure deduplication and compression are turned on:
LXC can use ZFS's native snapshot features. To make sure you can see snapshots when running
zfs list, do the following:
LXC Network Settings
Next, review the settings in
/etc/default/lxc-net, such as the default NAT'd subnet. If you make any changes, restart the
Creating a Base Container
To take advantage of ZFS's deduplication, create a base container:
When the command has finished, you'll see that LXC has created a new ZFS partition:
Cloning the Base Container
Now when you want to launch an Ubuntu container, just clone the base container:
When the command completes, you'll see that LXC has created a ZFS snapshot in addition to another ZFS partition. Also, you can see that almost no extra disk space was used due to deduplication:
By default, LXC uses the
veth networking mode for containers. This is the most robust networking mode. Other modes exist and I highly recommend this article for a detailed look at them.
veth mode can be thought of as a form of NAT and the LXC server is now acting as a NAT'd gateway for all of the containers running on the server. If you want the containers to be accessible from the public internet, you will need to do some port forwarding.
I have put together a small script called lxc-nat that will configure port forwarding based on entries made in
For example, if you have Apache running in a container called
www, create the following entry:
Or if you want to access
www via SSH:
This article showed the steps used to configure a server to host LXC-based containers. It used the latest Ubuntu 12.04 kernel and up to date versions of Open vSwitch, ZFS, and LXC itself.