Skip to main content

Setting up Linux Containers

Make sure system has 3.10+ {*Specifically, 3.16-0.generic*} for our software purpose.
Install LXC On Ubuntu
1. sudo apt-get update

LXC is available on Ubuntu default repositories. So, Run the following command to install it on Ubuntu, and its derivatives.
2. sudo apt-get install lxc lxctl lxc-templates
The above command will install LXC, and everything needed by LXC.

Now, enter the following command to check everything OK.
3. sudo lxc-checkconfig

Sample Output:
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.19.0-15-generic
--- 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
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
Creating Linux Containers (Virtual Units)
LXC is usage very simple, and easy. By default, LXC has many ready made templates. To list all available templates, enter the following command:
sudo ls /usr/share/lxc/templates/

Sample output:
lxc-alpine    lxc-archlinux  lxc-centos  lxc-debian    lxc-fedora  lxc-openmandriva  lxc-oracle  lxc-sshd    lxc-ubuntu-cloud
lxc-altlinux  lxc-busybox    lxc-cirros  lxc-download  lxc-gentoo  lxc-opensuse      lxc-plamo     lxc-ubuntu

To create a new Container, the command should be as below.
sudo lxc-create -n <container-name> -t <template>
Example:  sudo lxc-create -n lxc_01 -t ubuntu

The above command will create a ubuntu container with name “ubuntu01″.
Sample output:
Checking cache download in /var/cache/lxc/vivid/rootfs-amd64 ... 
Installing packages in template: ssh,vim,language-pack-en
Downloading ubuntu vivid minimal ...
I: Retrieving Release 
I: Retrieving Release.gpg 
I: Checking Release signature
I: Valid Release signature (key id 790BC7277767219C42C86F933B4FE6ACC0B21F32)
I: Retrieving Packages 
.
.
.
Generation complete.
Creating SSH2 RSA key; this may take some time ...
2048 ef:14:b4:98:fd:7a:3d:0c:cc:bd:69:17:87:03:a9:7c /etc/ssh/ssh_host_rsa_key.pub (RSA)
Creating SSH2 DSA key; this may take some time ...
1024 b1:ab:2a:4b:c4:77:2e:3f:da:ab:df:24:74:9f:69:a9 /etc/ssh/ssh_host_dsa_key.pub (DSA)
Creating SSH2 ECDSA key; this may take some time ...
256 1a:6a:93:88:05:c5:1c:10:ad:d5:f7:5a:aa:fb:04:57 /etc/ssh/ssh_host_ecdsa_key.pub (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 80:a8:d4:8e:3e:d2:06:a3:96:6d:51:4a:6a:a2:23:47 /etc/ssh/ssh_host_ed25519_key.pub (ED25519)
invoke-rc.d: policy-rc.d denied execution of start.
Current default time zone: 'Asia/Kolkata'
Local time is now:      Fri Jun 12 15:03:30 IST 2015.
Universal Time is now:  Fri Jun 12 09:33:30 UTC 2015.
##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##

Starting Containers
After creating the containers, start it using the following command.
sudo lxc-start -n lxc_01 -d
-d is used to skip all logs getting printed on standard output console.

Now, log in to the container with command:
sudo lxc-console -n lxc_01
Enter the username and password of the container.  The default username and password will be generated while creating new containers. In my case, the default username is ubuntu, and password is ubuntu.

Viewing the list of created containers
To view the list of newly generated containers, enter the following command from your original computer.
sudo lxc-ls   or sudo lxc-ls --fancy

h2. Viewing the complete details of a Container
sudo lxc-info -n lxc_01

Stopping Containers
sudo lxc-stop -n lxc_01

Cloning Containers
It’s possible to clone a new container from an existing container. For example, to clone a new container called “ubuntu02″ from an existing container “ubuntu01″, use the following commands:
sudo lxc-stop -n lxc_01
sudo lxc-clone lxc_01 lxc_02

Sample output:
Created container lxc_02 as copy of lxc-01

Take snapshot of a container
It’s also possible to take snapshot of a container. To take snapshot of the container ubuntu01, enter the following commands:
sudo lxc-stop -n lxc_01
sudo lxc-snapshot -n lxc_01

Sample Output:
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.

In Ubuntu 15.04, the snapshots will be stored in /var/lib/lxc/ directory of your original host computer.
sudo ls /var/lib/lxc/

In Ubuntu 14.04 and older versions, the snapshots will be stored in /var/lib/lxcsnaps/ directory.
sudo ls /var/lib/lxcsnaps/

Restoring Snapshots
To restore a container from the snapshot, use the following command.
sudo lxc-snapshot -n  lxc_01 -r snap0

Deleting Containers
To destroy a container completely from you original host, enter the following command:
sudo lxc-destroy -n lxc_01

Managing Containers using LXC web console
To install LXC web panel, run the following command:
wget http://lxc-webpanel.github.io/tools/install.sh -O - | sudo bash

Sample output:
--2015-06-12 15:37:41--  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/octet-stream]
Saving to: ‘STDOUT’
-                                           0%[                                                                                        ]       0  --.-KB/s              _     __   _______  __          __  _       _____                 _ 
| |    \ \ / / ____| \ \        / / | |     |  __ \               | |
| |     \ V / |       \ \  /\  / /__| |__   | |__) |_ _ _ __   ___| |
| |      > <| |        \ \/  \/ / _ \ '_ \  |  ___/ _` | '_ \ / _ \ |
| |____ / . \ |____     \  /\  /  __/ |_) | | |  | (_| | | | |  __/ |
|______/_/ \_\_____|     \/  \/ \___|_.__/  |_|   \__,_|_| |_|\___|_|
Automatic installer
-                                         100%[=======================================================================================>]   2.62K  --.-KB/s   in 0.003s 
.
.
.
.
Cleaning up...
Cloning LXC Web Panel...
Cloning into '/srv/lwp'...
remote: Counting objects: 167, done.
remote: Total 167 (delta 0), reused 0 (delta 0), pack-reused 167
Receiving objects: 100% (167/167), 148.85 KiB | 132.00 KiB/s, done.
Resolving deltas: 100% (63/63), done.
Checking connectivity... done.
Installation complete!
Adding /etc/init.d/lwp...
Done
Starting server...done.
Connect you on http://your-ip-address:5000/

Then, access the LXC web panel using URL: http://ip-address:5000. The default username/password is admin/admin.

Issues: UDP Checksum error while running dhcp on WAN links inside a container

Fix: Assuming we follow bridged architecture in bringing up containers,
Make sure to disable "tx-checksum-ip-generic " option on every bridge device attached to container.
Example: ethtool -K br-wan0 tx-checksum-ip-generic off

Comments

Popular posts from this blog

What Why How SDN..???????

What is SDN?   If you follow any number of news feeds or vendor accounts on Twitter, you've no doubt noticed the term "software-defined networking" or SDN popping up more and more lately. Depending on whom you believe, SDN is either the most important industry revolution since Ethernet or merely the latest marketing buzzword (the truth, of course, probably falls somewhere in between). Few people from either camp, however, take the time to explain what SDN actually means. This is chiefly because the term is so new and different parties have been stretching it to encompass varying definitions which serve their own agendas. The phrase "software-defined networking" only became popular over roughly the past eighteen months or so. So what the hell is it? Before we can appreciate the concept of SDN, we must first examine how current networks function. Each of the many processes of a router or switch can be assigned to one of three conceptual planes of operatio...

Sexy C#

Download samples   Table of Contents   1.   Introduction  2.   Background    3.   Sexy Features 3.1.   Extension Methods   3.2.   Anonymous Type   3.3.   Delegate   3.4.   Lambda Expression 3.5.   Async-Await Pair   3.6.   Generics   4.   Conclusion   1. Introduction     C#  is a very popular programming language. It is mostly popular in the .NET arena. The main reason behind that is the C# language contains so many useful features. It is actually a multi-paradigm programming language. Q.   Why do we call C# a muti-paradigm programming language? A.  Well, C# has the following characteristics:  Strongly typed   Object Oriented  Functional  Declarative Programming  Imperative Programming   Component based Programming Dynamic Programming ...

Making Python Programs Blazingly Fast

Let’s look at the performance of our Python programs and see how to make them up to 30% faster! Python  haters always say, that one of the reasons they don’t want to use it, is that it’s  slow . Well, whether specific program — regardless of the programming language used — is fast or slow is very much dependent on the developer who wrote it and their skill and ability to write  optimized  and  fast  programs. So, let’s prove some people wrong and let’s see how we can improve performance of our  Python  programs and make them really fast! Timing and Profiling Before we start optimizing anything, we first need to find out which parts of our code actually slow down the whole program. Sometimes the bottleneck of the program might be obvious, but in case you don’t know where it is, then here are options you have for finding out: Note: This is the program I will be using for demonstration purposes, it computes  e  to pow...