CentOS 7 Linux Containers Quickstart

Created on: 2016-02-06 05:00:00 -0500

Categories: Linux Containers


Linux containers provide an easy way to quickly deploy a series of Linux servers. This can help us to test various network configurations, network application development using distributed systems and test security or provisioning policies on a test network before production deployment. In my early years of SysAdmining, we often had to find a series of scrap PC computers and build physical data centers to test out various configuration. With the advancements of virtualization this has made the process easier. However, there are still some issues namely cost of acquiring licenses to run certain VM packages or your desktop computer may not have enough resources to run numerous nodes using VirtualBox. Linux containers provide a lightweight approach to creating a virtualized test environment to test out various data center configuration.

This guide is intended to quickly get you up and running using LXC on your CentOS 7 desktop and will be used as the base for future articles relating to SDN and cloud technologies.

Setup

First, we have to install the epel-release package to get access to the lxc packages.

$ sudo yum install epel-release
$ sudo yum update

Now add the following lxc packages. Although we won’t be using some of these packages now they will be utilized in future posts.

$ sudo yum install lxc lxc-devel python-lxc lxc-libs lxc-templates libvirt ansible

Start the lxc and libvirtd services. Libvirtd will be used to help set up our bridge to allow our linux containers to access the network.

$ sudo systemctl start lxc.service
$ sudo systemctl start libvirtd

We will now create our first Linux container. It will be using the CentOS template and release number 7. The name for this container will be ‘c7’

$ sudo lxc-create -t centos -n c7 -- --release 7

After executing the command there will be some yum packaging operations taking place. Once it has finished you will be instructed that the root password is stored in ‘/var/lib/lxc/c7/tmp_root_pass’ and if you forget your password it can be reset by issuing:

chroot /var/lib/lxc/c7/rootfs passwd

Note: it is quicker to change the password now so just enter the chroot command

$ chroot /var/lib/lxc/c7/rootfs passwd

Test

$ sudo lxc-start -n c7 

Once the startup has finished log in using the new root password. At this time you can create a normal user account.

$ useradd vmuser -m
$ passwd vmuser

To get the IP address of the container enter:

$ ip a

From there you can access the container from SSH.

Note: There was some issue with the CentOS container being slow. To solve this I came across a solution from this webpage <a href=”https://lemarchand.io/run-centos-7-lxc-container-inside-debian-jessie/ . Basically, need to add the following line to /var/lib/lxc/c7/config

lxc.kmsg = 0

The problem has since stop and everything seems to be running OK now.

Normally, you could start the lxc with a -d flag to put the lxc into the background to avoid killing the container. For now you will need a second shell to kill this current VM. This can be done with the following command:

$ sudo lxc-stop -n c7

Or if you are logged in through the console

$ shutdown -h now

Start the VM again using the -d flag

$ sudo lxc-start -n c7 -d 

Then connect a console to it:

$ sudo lxc-console -n c7 -t 0

You can also test out SSH connection to the container. In the event you have forgotten the IP address of the container you can always run the lxc-ls command which list all the containers on your current computer.

$ sudo lxc-info -n c7
Name:		c7
State:		RUNNING
PID:		11852
IP:		192.168.122.140

This wraps up our quick introduction into Linux containers using LXC. In later posts I will demonstrate some applications of SDN to Linux containers and other cloud computing.