This article will describe how to create 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.
OpenVZ is the open-source branch of Virtuozzo, a commercial virtualization solution widely used by hosting providers. The OpenVZ kernel patch is licensed under GPL license, and the user-level tools are under the QPL license.
As there are many ways of achieving this goal, I’d keep this howto as smart as possible. In the following days, I’d create similar posts on Xen / Vserver, please keep a close eye on my planet.
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:
cd /etc/yum.repos.d wget http://download.openvz.org/openvz.repo 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”
cat /boot/grub/menu.lst # 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. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00 # initrd /initrd-version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.18-128.2.1.el5.028stab064.7) root (hd0,0) kernel /vmlinuz-2.6.18-128.2.1.el5.028stab064.7 ro root=LABEL=/ initrd /initrd-2.6.18-128.2.1.el5.028stab064.7.img
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:
cat /etc/sysctl.conf 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 net.ipv4.icmp_echo_ignore_broadcasts=1 net.ipv4.conf.default.forwarding=1
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 2.6.18-128.2.1.el5.028stab064.7
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:
cd /vz/template/cache wget http://download.openvz.org/template/precreated/contrib/centos-5-i386-default.tar.gz
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 22.214.171.124 --nameserver 126.96.36.199 --nameserver 188.8.131.52 --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 Version: 2.5 uid resource held maxheld barrier limit failcnt 211: kmemsize 1508202 1661695 11055923 11377049 0 lockedpages 0 0 256 256 0 privvmpages 5430 7102 65536 69632 0 shmpages 381 381 21504 21504 0 dummy 0 0 0 0 0 numproc 19 21 240 240 0 physpages 2489 2775 0 2147483647 0 vmguarpages 0 0 33792 2147483647 0 oomguarpages 2489 2775 26112 2147483647 0 numtcpsock 5 5 360 360 0 numflock 3 4 188 206 0 numpty 0 1 16 16 0 numsiginfo 0 2 256 256 0 tcpsndbuf 44720 0 1720320 2703360 0 tcprcvbuf 81920 0 1720320 2703360 0 othersockbuf 13144 14356 1126080 2097152 0 dgramrcvbuf 0 8380 262144 262144 0 numothersock 11 13 120 120 0 dcachesize 0 0 3409920 3624960 0 numfile 503 531 9312 9312 0 dummy 0 0 0 0 0 dummy 0 0 0 0 0 dummy 0 0 0 0 0 numiptent 10 10 128 128 0
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”. If you faced any issues during the installation, please create a thread and show your errors in the sub forum virtualization at admon community.