There are number of virtualization products exist for Linux. OpenVZ is one the most
popular among others which has been widely used by many Web Hosting Provider.
OpenVZ is a glorified Linux chroot or BSD jail system that allows you to completely isolate processes from each other, increase security by keeping bits separate, and tightly control resource utilization. OpenVZ refers to these “virtual machines” as containers, virtual private servers (VPS), or virtual environments (VE).
The Installation of OpenVZ is quite simple. It requires a special kernel to provide the virtualization support it needs, and this can be obtained easily via the OpenVZ project itself. The OpenVZ kernel patch is licensed under GPL license, and the user-level tools are under the QPL license.
The article below explain how to install OpenVZ container in CentOS 5.4. With OpenVZ you can create multiple Virtual Private Servers (VPS) on same hardware machine and running them simultaneously and efficiently.
1, Install OpenVZ
In order to install OpenVZ, we need to add the OpenVZ repository to yum, this would also help us keep the kernel up-to-date:
rpm –import http://download.openvz.org/RPM-GPG-Key-OpenVZ
The repository contains a few different OpenVZ kernels (you can find more details here: http://wiki.openvz.org/Kernel_flavors), or with this command “yum search ovzkernel“.
Pick one of them and install it as follows:
yum install ovzkernel
This should automatically update the GRUB bootloader as well. An example is listed below”
# grub.conf generated by anaconda
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
#all kernel and initrd paths are relative to /boot/, eg.
#kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
title CentOS (2.6.18-128.2.1.el5.028stab064.7)
kernel /vmlinuz-2.6.18-128.2.1.el5.028stab064.7 ro root=LABEL=/
2, Pre-adjustings before the installation
Now we install some OpenVZ user tools:
yum install vzctl vzquota
Open /etc/sysctl.conf and make sure that you have the following settings in it:
net.ipv4.ip_forward = 1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
The following step is important if the IP addresses of your virtual machines are from a different subnet than the host system’s IP address. If you don’t do this, networking will not work in the virtual machines!
Open /etc/vz/vz.conf and set NEIGHBOUR_DEVS to all, the modified entry is like this “NEIGHBOUR_DEVS=all“. SELinux needs to be disabled, you can do it by making changes on its config file /etc/sysconfig/selinux. Finally, reboot the system, and then your new OpenVZ kernel should show up:
[root@server1 ~]# uname -r
3, Create a Virutal Server
Before we can create virtual machines with OpenVZ, we need to have a template for the distribution that we want to use in the virtual machines in the /vz/template/cache by default. New virtual machines will be created from these templates. A list of precreated templates is available here.
I want to use CentOS 5 in my virtual machines, so I download a CentOS 5 template:
To set up a VPS from the default CentOS 5 template, run:
vzctl create 211 –ostemplate centos-5-i386-default –config vps.basic
The 211 must be uniqe and each virtual machine must have its own ID. You can use the last part of the VPS’s IP address for it. For example, if the virtual machine’s IP address is 192.168.0.211, you use 211 as the ID.
4, Common controls on OpenVZ
If you want to have the vm started at boot, run:
vzctl set 211 –onboot yes –save
To set a hostname and IP address for the vm, run:
vzctl set 211 –hostname test.example.com –save
vzctl set 211 –ipadd 192.168.0.211 –save
Next we set the number of sockets to 120 and assign a few nameservers to the vm:
vzctl set 211 –numothersock 120 –save
vzctl set 211 –nameserver 184.108.40.206 –nameserver 220.127.116.11 –nameserver 18.104.22.168 –save
Instead of using the vzctl set commands, you can directly edit vm’s configuration file which is stored in the directory /etc/vz/conf. If the ID of the vm is 211, then the configuration file is /etc/vz/conf/211.conf.
In order to start the vm, run “vzctl start 211″, to set a root password for the vm, run “vzctl exec 211 passwd”. Now You can either connect to the vm via SSH (e.g. with PuTTY), or login through console directly as follows:
vzctl enter 211
To leave the vm’s console, type “exit”, to stop a vm, run “vzctl stop 211“, to restart a vm, run “vzctl restart 211“.
To delete a vm from the hard drive (it must be stopped before you can do this), run “vzctl destroy 211“.
5, Check VM Status
To get a list of your vms and their statuses, run “vzlist -a” like this:
[root@server1 cache]# vzlist -a
VEID NPROC STATUS IP_ADDR HOSTNAME
211 18 running 192.168.0.211 test.example.com
To find out about the resources allocated to a vm, run like follows:
[root@server1 cache]# vzctl exec 211 cat /proc/user_beancounters
The failcnt column is very important, it should be only zeros; if not, means that the vm needs more resources than currently allocated. Open the vm’s configuration file in /etc/vz/conf and raise the appropriate resource, then restart it.
To find out more about the vzctl command, run “man vzctl”.
Article Written By Shahram Azin http://www.hostingbangkok.com/