Home Linux Servers Linux Virtualisation with OpenVZ on CentOS

Linux Virtualisation with OpenVZ on CentOS
Written by Kwok Yao Chim   
Wednesday, 02 January 2008 00:00

A few years ago I spoke to a friend of mine who blabbered on about Virtuozzo, which allowed him to run one server hardware with many instances of server operating systems on the one hardware. The topic blew way over my head!

Recently these past few months I had some time to look into this "Virtualisation" technology and I was surprised that there were many open source versions of virtualisation.

The one which really stood out was OpenVZ, this is developed by SWSoft/Parallels but further enhanced and commercialized in the form of Virtuozzo. OpenVZ allows you to host different flavours of Linux distros on one hardware node, take a look at the diagram below to see where it sits...

 OpenVZ Architecture

More research on the web lead me to a really good website which I always frequent... www.howtoforge.org.
Here they have a fantastic step-by-step article on setting up virtualisation on a CentOS 4.4 base system.

 

Within this article I will try and see if I can setup Virtualisation on CentOS 5.1 base OS system.

We have five main parts to cover

  1. Requirements
  2. Install and configure host OS system on hardware node
  3. Install and configure OpenVZ kernel
  4. OS templates (such as Fedora, Unbuntu, Debian etc)
  5. Create Virtual Environment and control the VE
  6. Using Pre-Created OS Templates or build your own.
Lets get started...

1. Requirements

If want to be able to run a lot of multiple VEs, you would probably need something along these specs:
  • Pentium 4
  • 1GB RAM
  • 40GB HDD or more (you would probably want to give each VE 5GB or more)
  • Network Interface Card
  • CentOS (as base operating system)


2. Installing the Base OS

I've chosen CentOS as my base OS because it is based on Redhat EL which means stability, reliability and performance. It is also easier and faster to configure since I'm more familiar with Redhat and Fedora plus the bonus of YUM (I love it).

I used the network installation of CentOS 5.1 to get to the actual CentOS installation screen.  If your familiar with the Redhat / Fedora / CentOS installation procedures then all you will need to know are these settings:

Installation Type: Server
Disk Partitioning: Automatic
Create Partitions:
 
Partition FS Type Typical Size Description
/ ext3 2 - 4 Gb Root partition for base OS
swap swap 2 x Physical Memory RAM Paging partition for host OS
/vz ext3 Rest of available space Partition to host OpenVZ remplates and VEs

Network Config: Best to use static IP address and give a hostname of vznode0.yourdomain.com

Firewall Config: No Firewall, and DISABLE SELinux

Choose your language and choose your timezone

Enter your ROOT password

Next choose to install the bare OS, without any additional packages/applications

Your installation will begin.

After successful installation, update the the whole system.
$ yum update -y

And restart the system.
$ shutdown -r now

 

3. Installing and Configuring OpenVZ

You will need to download the OpenVZ repos in before you can start to install.
$ cd /etc/yum.repos.d
$ wget http://download.openvz.org/openvz.repo
$ yum install ovzkernel

On most CentOS installations the GRUB boot loader is used as default, and after installing OpenVZ kernel, it will have automaitcally added and entry to the /boob/grub/grub.conf file and it should look like this:

title Cent OS (2.6.8-022stab029.1)
       root (hd0,0)
       kernel /vmlinuz-2.6.8-022stab029.1 ro root=/dev/sda5 quiet rhgb vga=0x31B
       initrd /initrd-2.6.8-022stab029.1.img

Rename the title to OpenVZ so you know which one is which and remove last three parameters from the "kernel" line so it should now look like this:
title OpenVZ (2.6.8-022stab029.1)
       root (hd0,0)
       kernel /vmlinuz-2.6.8-022stab029.1 ro root=/dev/sda5
       initrd /initrd-2.6.8-022stab029.1.img

and set it to boot off OpenVZ by editing the defult to 0 like this: default=0

We have to change some kernel parameters to allow OpenVZ to work properly.
$ vi /etc/sysctl.conf

in the file edit these lines: 

...
# On Hardware Node we generally need
# packet forwarding enabled and proxy arp disabled
net.ipv4.ip_forward = 1
net.ipv4.conf.default.proxy_arp = 0
# Enables source route verification
net.ipv4.conf.all.rp_filter = 1
# Enables the magic-sysrq key
kernel.sysrq = 1
# TCP Explict Congestion Notification
#net.ipv4.tcp_ecn = 0
# we do not want all our interfaces to send redirects
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
...


Make sure SELinux is disabled if not then do so by editing /etc/sysconfig/selinux and add the line: SELINUX=disabled

Enable conntracks for VE0, add the following line to /etc/modprobe.conf file:
options ip_conntrack ip_conntrack_enable_ve0=1

Note: in kernels later than 2.6.8, connection tracking is enabled by default.

Now reboot the machine.

Now its time to install the utilities that allow you to create, control and destroy VPS.
$ yum install vzctl vzquota

Start the OpenVZ service.
$ service vz start

and also make it start on boot by going into ntsysv and marking it to startup.


4. OS Templates

You need to install some OS template utilites:
$ yum install vzpkg vzyum vzrpm43-python vzrpm44-python

After you have installed these utilies you can now start looking for some OS templates.
$ yum search vztmpl OR $ yum list vztmpl*

A list of available OS templates should be listed such as Fedora Core and CentOS.
Choose and install an ostemplate.
$ yum install vztmpl-centos-4.i386
OR
$ yum install vztmpl-fedora-7.i386

now run the vzpkgcahe to update / download the latest OS  template (this process may take some time.
$ vzpkgcache

The vzpkgcache commdn will update all the ostemplates as long as you have the necessary details (see part 6)

You can also download some pre-created OS templates from OpenVZ.org
http://openvz.org/download/template/cache/
and dump the OSname.tar.gz straight into /vz/template/cache/ , but be advised you will have to create your own OS template metadata in order to update it, more discussed later in the article.


5. Create and Control VEs

Now lets begin to experiment by creating a VE.
$ vzctl create 1020 --ostemplate centos-4-i386-default --ipadd 192.168.1.20 --hostname vps1020.domain.com

The above command creates a VE with ID 1020 using centos-4 OS template and given an IP address and hostname (for more info $vzctl --help).


You can add/change settings to the VE like this:
$ vzctl set 1020 --nameserver 199.128.3.21 --userpasswd root:password --save

Don't forget to add the --save parameter at the end

Here are some commands to start, stop and destroy VE:
$ vzctl start 1020  (this will start the VE)
$ vzctl stop 1020  (this will stop the VE)
$ vzctl destroy 1020  (this will destroy the VE and its saved settings and space)


6. Using Pre-Created Templates
 

You can download some pre-created OS templates from OpenVZ.org
http://openvz.org/download/template/cache/
and dump the OSname.tar.gz straight into /vz/template/cache/

Here's an example:
$ cd /vz/template/cache
$wget 
http://download.openvz.org/template/precreated/contrib/centos-5-i386-minimal.tar.gz

These pre-created OS templates can be used immediately by using --ostemplate parameter to use it.

However when you try to update/install a package with vzyum or try a vzpkgcache it will not work as there is no metadata associated with the OS template

In order to allow updates I created the directory /vz/template/centos/5/i386 within this directory it will need some more elements:
/vz/template/centos/5/i386/
      config/
      yum-cache/


or for Fedora
/vz/template/fedora/7/i386/
     config/
     yum-cache/


within the config/ directory there are usually *.list files such as default.list or minimal.list which is related to the type of ostemplate in the /vz/template/cache/ , for example:
centos-4-i386-default.tar.gz  --> /vz/template/centos/4/i386/config/default.list
centos-4-i386-minimal.tar.gz  --> /vz/template/centos/4/i386/config/minimal.list