Linux Containers is a light weight virtualization technology at the operating-system level which is capable of running more than one Linux system on a Linux host. It is an alternative to other traditional hypervisors like KVM and Xen. Compared to the full machine virtualization techniques, isolation offered in containers is less but at the same time overhead is reduced as a portion of the host kernel and operating system instance gets shared by the virtual machines. This does not mean that the containers can replace traditional hypervisors as each have their own pros and cons. In this article, we will briefly learn about the installation and usage of one of the popular Linux based container project LXC.
Installation of LXC
I'm using Ubuntu 14.10 for all the examples used here.
LXC can be installed by using the simple apt-get command in Debian based distros (yum in RedHat based ones). Make sure that you use 'sudo' command everywhere if you are not logged in as root.
sudo apt-get install lxc
Creation, listing, login and stopping
Next you need to create a container by using the lxc-create command
sudo lxc-create -t <template> -n <container-name>
There are quite a few ready-made templates available for creating containers. They are mostly for the popular Linux distributions. Let us try to create a Ubuntu based container.
sudo lxc-create -t ubuntu -n Ubuntu1
May be you want to help yourself with a cup of coffee as it takes a while for retrieving the required packages and creating the container. :)
Now we have an Ubuntu container with the name Unbuntu1.
Let us now list all the containers that are present on our host.
sudo lxc-ls
poornima@poornima-Lenovo:~$ sudo lxc-ls
[sudo] password for poornima:
Ubuntu1
We can view the complete details using lxc-info
sudolxc-info -n <container-name>
poornima@poornima-Lenovo:~$ sudo lxc-info -n Ubuntu1
Name: Ubuntu1
State: STOPPED
From the output you can see the list of all the containers present on the host and categorised depending on the different states that they are in (running, stopped or frozen).
Containers can be started using the lxc-start command.
lxc-start -n <container-name>
OR
lxc-start -d -n <container-name> to start the container in the background.
Verify if the container has actually started or not:
poornima@poornima-Lenovo:~$ sudo lxc-info -n Ubuntu1
Name: Ubuntu1
State: RUNNING
PID: 2969
IP: 10.0.3.150
CPU use: 1.27 seconds
BlkIO use: 20.66 MiB
Memory use: 26.27 MiB
KMem use: 0 bytes
Link: vethVFLSOP
TX bytes: 1.80 KiB
RX bytes: 4.94 KiB
Total bytes: 6.74 KiB
In order to login or attach back to the container console, we have lxc-console.
lxc-console -n <container-name>
poornima@poornima-Lenovo:~$ sudo lxc-console -n Ubuntu1
Connected to tty 1
Type <Ctrl+a q> to exit the console, <ctrl+a ctrl+a=""> to enter Ctrl+a itselfUbuntu 14.10 Ubuntu1 tty1
Ubuntu1 login: ubuntu
Password:
Last login: Thu Aug 27 12:05:59 IST 2015 on lxc/tty1
Welcome to Ubuntu 14.10 (GNU/Linux 3.16.0-23-generic i686)* Documentation: https://help.ubuntu.com/
ubuntu@Ubuntu1:~$
We can come back to the host's console using the key sequence 'Ctrl+a' followed q. Note that the container is still running in the background and we have just detached from it.
If you have to stop the container you need to use lxc-stop.
lxc-stop -n <container-name>
poornima@poornima-Lenovo:~$ sudo lxc-stop -n Ubuntu1
poornima@poornima-Lenovo:~$ sudo lxc-info -n Ubuntu1
Name: Ubuntu1
State: STOPPED
Freezing, unfreezing, cloning and poweroff
Containers can be frozen using the lxc-freeze command.
lxc-freeze -n <container-name>
poornima@poornima-Lenovo:~$ sudo lxc-freeze -n Ubuntu1
poornima@poornima-Lenovo:~$ sudo lxc-info -n Ubuntu1
Name: Ubuntu1
State: FROZEN
PID: 2969
IP: 10.0.3.150
CPU use: 1.48 seconds
BlkIO use: 21.42 MiB
Memory use: 26.96 MiB
KMem use: 0 bytes
Link: vethVFLSOP
TX bytes: 2.63 KiB
RX bytes: 5.80 KiB
Total bytes: 8.43 KiB
You can unfreeze them with lxc-unfreeze.
lxc-unfreeze -n <container-name>
One can even clone containers using the lxc-clone command. But before issuing the clone command, see to it that you stop the running container first using the lxc-stop command as mentioned previously.
lxc-clone -o <existing container> -n <new container>
poornima@poornima-Lenovo:~$ sudo lxc-clone -o Ubuntu1 -n Ubuntu-clone
Created container Ubuntu-clone as copy of Ubuntu1
poornima@poornima-Lenovo:~$ sudo lxc-ls
Ubuntu-clone Ubuntu1
To poweroff containers, use lxc poweroff when inside the containers console.
ubuntu@Ubuntu1:~$ sudo poweroff
[sudo] password for ubuntu:Broadcast message from ubuntu@Ubuntu1
(/dev/lxc/tty1) at 12:17 ...The system is going down for power off NOW!
You can verify from the host that the container has stopped.
poornima@poornima-Lenovo:~$ sudo lxc-info -n Ubuntu1
Name: Ubuntu1
State: STOPPED
Snapshots - creation and restoration
lxc-snapshot command is useful for taking a snapshot of the required container.
lxc-snapshot -n <container-name>
poornima@poornima-Lenovo:~$ sudo lxc-snapshot -n Ubuntu1
lxc_container: lxccontainer.c: lxcapi_snapshot: 2953 Snapshot of directory-backed container requested.
lxc_container: lxccontainer.c: lxcapi_snapshot: 2954 Making a copy-clone. If you do want snapshots, then
lxc_container: lxccontainer.c: lxcapi_snapshot: 2955 please create an aufs or overlayfs clone first, snapshot that
lxc_container: lxccontainer.c: lxcapi_snapshot: 2956 and keep the original container pristine.
These snapshots will be stored under /var/lib/lxc in Ubuntu 14.10 . In some earlier versions, you can find them in /var/lib/lxcsnaps.
poornima@poornima-Lenovo:~$ sudo lxc-snapshot --name Ubuntu1 --list
snap0 (/var/lib/lxc/Ubuntu1/snaps) 2015:08:27 12:20:41
Configuration options
By default, all the containers created using lxc are stored under /var/lib/lxc where each container will have a directory. Inside this directory, each containers configuration will be stored in a file called config. The option lxc.rootfs specifies the location of containers root file system. lxc. network.type specifies the kind of networking used by that container. Eg, veth
If you are interested in more configuration options, check out man 5 lxc.conf
Deletion
Containers can be completely destroyed from the host using lxc-destroy command. If you have created any snapshots from the container that you are about to delete, you need to first delete them.
lxc-destroy -n <container-name>
poornima@poornima-Lenovo:~$ sudo lxc-destroy --name=Ubuntu-clone
poornima@poornima-Lenovo:~$ sudo lxc-info --name=Ubuntu-clone
Ubuntu-clone doesn't exist
Management using web console
If you are not a fan of the Linux command line or not comfortable using it, then you can manage your containers using the LXC web panels through your browsers.
Install the web panel using the following command as a root user.
wget http://lxc-webpanel.github.io/tools/install.sh -O - | bash
root@poornima-Lenovo:/home/poornima# wget http://lxc-webpanel.github.io/tools/install.sh -O - | bash
--2015-08-27 13:15:13-- http://lxc-webpanel.github.io/tools/install.sh
Resolving lxc-webpanel.github.io (lxc-webpanel.github.io)... 103.245.222.133
Connecting to lxc-webpanel.github.io (lxc-webpanel.github.io)|103.245.222.133|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2678 (2.6K) [application/x-sh]
Saving to: STDOUT0% [ ] 0 --.-K/s _ __ _______ __ __ _ _____ _
| | \ \ / / ____| \ \ / / | | | __ \ | |
| | \ V / | \ \ /\ / /__| |__ | |__) |_ _ _ __ ___| |
| | > <| | \ \/ \/ / _ \ '_ \ | ___/ _` | '_ \ / _ \ |
| |____ / . \ |____ \ /\ / __/ |_) | | | | (_| | | | | __/ |
|______/_/ \_\_____| \/ \/ \___|_.__/ |_| \__,_|_| |_|\___|_|
Automatic installerInstalling requirement...
100%[======================================>] 2,678 --.-K/s in 0.003s2015-08-27 13:15:14 (867 KB/s) - written to stdout [2678/2678]
Cloning LXC Web Panel...
Cloning into '/srv/lwp'...
remote: Counting objects: 188, done.
remote: Total 188 (delta 0), reused 0 (delta 0), pack-reused 188
Receiving objects: 100% (188/188), 172.76 KiB | 49.00 KiB/s, done.
Resolving deltas: 100% (79/79), done.
Checking connectivity... done.Installation complete!
Adding /etc/init.d/lwp...
Done
Starting server...done.
Connect you on http://your-ip-address:5000/
We can then access the user interface using the URL: http://:5000 using the default userid / password admin/admin
Phew! Now, you are ready to perform all your container related operations using the web panel!!
Conclusion
In this article, we have learnt how to install LXC, use some of the available commands and the web panel. Of late, some tools have been developed which in turn use LXC underneath. Docker engine and LXD are some of them. In the near future, it is quite possible for the cloud infrastructure to heavily use Linux containers for all the advantages they have to offer.
The post How to Manage Linux Containers using LXC appeared first on LinOxide.