Setup Linux Containers using LXC on Ubuntu

LXC Containers is a Lightweight Virtualization Technology. Containers that we will be launching will use the same Kernel as the Host, so its fast, lightweight and shows great performance!

We will be setting up LXC, and launch a couple of containers to demonstrate how easy it is to get started with LXC.

Installing Packages:

$ sudo apt-get install lxc lxctl lxc-templates
``` <p>

**Check your Config:**

```language-bash
$ sudo lxc-checkconfig
``` <p>

`Sample Output:`

```language-yaml
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled

--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled

--- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled
``` <p>

**List Available Templates:**

```language-bash
$ sudo ls -ll /usr/share/lxc/templates/ | awk '{print $9}'
``` <p>

`Sample Output:`

```language-yaml
lxc-alpine
lxc-altlinux
lxc-archlinux
lxc-busybox
lxc-centos
lxc-cirros
lxc-debian
lxc-download
lxc-fedora
lxc-gentoo
lxc-openmandriva
lxc-opensuse
lxc-oracle
lxc-plamo
lxc-sshd
lxc-ubuntu
lxc-ubuntu-cloud
``` <p>

**Create a Ubuntu Container:**
```language-bash
$ sudo lxc-create --name my-ubuntu-container --template ubuntu
``` <p>

**Getting Container Info:**
```language-bash
$ sudo lxc-info --name my-ubuntu-container
Name:           my-ubunutu-container
State:          STOPPED
``` <p>

**Listing Containers:**
```language-bash
$ sudo lxc-ls --fancy
NAME                 STATE    IPV4        IPV6  AUTOSTART
my-ubuntu-container  RUNNING  10.0.3.121  -     NO
``` <p>

**Starting the Container:**
```language-bash
$ sudo lxc-start --name my-ubuntu-container --daemon
``` <p>

**Access your Container:**
```language-bash
$ sudo lxc-console --name my-ubuntu-container
user: ubuntu
passwd: ubuntu
``` <p>

To exit the container execute `(ctrl+a) + (q)`. You can also access your container by SSH, to retrieve your containers IP Address you can execute `lxc-info`.

**Create a CentOS Container:**

For CentOS containers, we need to install yum as a pre-requirement.

```language-bash
$ sudo apt-get install yum
$ sudo lxc-create --name my-centos-container --template centos
$ sudo lxc-start --name my-centos-container --daemon
``` <p>

`Output:`

```language-bash
The temporary root password is stored in:

        '/var/lib/lxc/centos01/tmp_root_pass'

The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible.  If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):

        chroot /var/lib/lxc/centos01/rootfs passwd

``` <p>

**Cloning Containers:**

We can clone a container, in order to do so, we fist need to stop the container by executing:

```language-bash
$ sudo lxc-stop --name my-ubuntu-container
``` <p>

Now that our container is stopped, we can continue to clone our container:

```language-bash
$ sudo lxc-clone my-ubuntu-container cloned-ubuntu-container
Created container cloned-ubuntu-container as copy of my-ubuntu-container
``` <p>

**Snapshot Containers:**

In order to make snapshots, we first need to stop our container:

```language-bash
$ sudo lxc-stop --name my-ubuntu-container
``` <p>

Once that is done, we can make our snapshot of our container. In this scenario, we want to do a snapshot before we upgrade glibc:

```language-bash
$ echo "before upgrading glibc" >> snaphot-comments
$ sudo lxc-snapshot --name my-ubuntu-container --comment snapshot-comments
``` <p>

Now we can verify if the snapshot succeeded:

```language-bash
$ sudo lxc-snapshot --name my-ubuntu-container -L -C

Output:

$ sudo lxc-snapshot -n ubuntu01 -L -C
before upgrading glibc
snap0 (/var/lib/lxcsnaps/my-ubuntu-container) 2016:05:02 13:15:30

Our destination directory for our snapshot is dependent on the version of Ubuntu that you will be using.

For Ubuntu 15 and up /var/lib/lxcsnaps/ and prior to that will be located under /var/lib/lxc/

Restoring Snapshots:

If its a scenario where we would like to revert at a certain point in time:

$ sudo lxc-snapshot --name my-ubuntu-container --restore snap0
``` <p>

If we would like to restore to a new container:

```language-bash
$ sudo lxc-snapshot --name my-ubuntu-container --restore snap0 ubuntu-snapshot1
``` <p>

**Terminating Containers:**

```language-bash
sudo lxc-destroy --name my-ubuntu-container
``` <p>

**Access via a WebGUI:**

The default username and password is `admin:admin`

```language-bash
wget http://lxc-webpanel.github.io/tools/install.sh -O - | sudo bash
``` <p>

More information on this can be found via their website: [linuxcontainers.org](https://linuxcontainers.org/)