Thursday, March 1, 2012

Virtualization Tutorials

Installing an OpenVZ Server


“OpenVZ is container-based virtualization for Linux. OpenVZ creates multiple secure, isolated containers (otherwise known as VEs or VPSs) on a single physical server enabling better server utilization and ensuring that applications do not conflict. Each container performs and executes exactly like a stand-alone server; a container can be rebooted independently and have root access, users, IP addresses, memory, processes, files, applications, system libraries and configuration files”


Download the config file for OpenVZ repo or copy and create a file called openvz.repo in the /etc/yum.repos.d directory.
http://download.openvz.org/openvz.repo
[openvz-utils]
name=OpenVZ utilities
#baseurl=http://download.openvz.org/current/
mirrorlist=http://download.openvz.org/mirrors-current
enabled=1
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
# In addition to openvz-utils repo, you have to enable one the the
# kernel repositories below. In the stock config, openvz-kernel-rhel5
# is enabled; you might want to change this.
[openvz-kernel-2.6.27]
name=OpenVZ 2.6.27 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.27/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.27
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.26]
name=OpenVZ 2.6.26 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.26/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.26
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.24]
name=OpenVZ 2.6.24 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.24/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.24
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.22]
name=OpenVZ 2.6.22 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.22/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.22
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.20]
name=OpenVZ 2.6.20 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.20/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.20
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-rhel5]
name=OpenVZ RHEL5-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel5-2.6.18/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel5-2.6.18
enabled=1
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.18]
name=OpenVZ 2.6.18 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.18/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.18
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.16]
name=OpenVZ 2.6.16 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.16/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.16
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-rhel4]
name=OpenVZ RHEL4-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel4-2.6.9/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel4-2.6.9
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-2.6.8]
name=OpenVZ 2.6.8 kernel
#baseurl=http://download.openvz.org/kernel/branches/2.6.8/current
mirrorlist=http://download.openvz.org/kernel/mirrors-2.6.8
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
Install OpenVZ
# yum install ovzkernel vzctl
Create a Partition Called /vz
Create a partition called /vz and enter the settings in the /etc/fstab.
Move Templates into the  /vz/template/cache/ directory.
The templates you will be using need to be moved into the template directory before you are able to build a VPS.  These pre-created templates are available from the OpenVZ site.
cp centos-5-i386-default.tar.gz /vz/template/cache/
Disable SELinux
In order to set up the hardware node it is important to either not enable SELinux at installation or disable it after installation.  You can disable SELinux after installation by editing the /etc/sysconfig/selinux file.  Here is the file contents listed below, not that SELinux has been disabled by change the work “enforcing” to “disabled”.  You must restart the machine.
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing – SELinux security policy is enforced.
#       permissive – SELinux prints warnings instead of enforcing.
#       disabled – SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted – Only targeted network daemons are protected.
#       strict – Full SELinux protection.
SELINUXTYPE=targeted
Edit /etc/sysctl.conf
In order for networking to work for the virtual servers that you create, you will need to enable packet forwarding as the hardware node must forward all packets to the virtual servers.   Edit the /etc/sysctl.conf file on the host.
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
or
Another option to turn on packet forwarding is to change the active config in the /proc directory.  This will setting wold have to be repeated when you boot the hardware node.
# echo 1 > /proc/sys/net/ipv4/ip_forward
Once this is complete reboot your system and it will boot to the OpenVZ Kernel.  You can test if it installed correctly by using this command:
vzlist -a

Create a Virtual Server with OpenVZ

Create a Virtual Server 
Once you have OpenVZ installed you will be able to begin the process of installing individual containers.
Using the vzctl command you will be able to create a virtual server in less than 5 minutes.  One of the steps that must be completed before you begin is to move the template you will use into the /vz/template/cache.
The first command uses a template and sets the configuration as a vps.basic.  The 26 is a identifier for the vps.  It makes sense to use the last octet of the IP Address for this identifier.  Thus this is used for 192.168.4.26.  In the example the centos template is used.
# vzctl create 26 – -ostemplate centos-5 –config vps.basic
Creating VE private area (centos-5-i386-default)
Performing postcreate actions
VE private area was created
This setting provides for the VE to start on boot.  This is probably what you want.  However, the default is not to start a VE on boot so if you do not use this setting it will remain off.
vzctl set 26 – -onboot yes –save
vzctl set 26 – -hostname admin26–save
vzctl set 26 – -ipadd 192.168.4.26 –save
vzctl set 26 – -nameserver 12.32.34.32 –save
# vzctl start 26
Starting VE …
VE is mounted
Adding IP address(es): 192.168.4.26
Setting CPU units: 1000
Set hostname: admin26
File resolv.conf was modified
VE start in progress…
vzctl set 26 – -userpasswd root:bcrY67LD3 –save
Changing password for user root.
passwd: all authentication tokens updated successfully.
Saved parameters for VE 26
Now use vzlist to show which virtual servers are running.
vzlist -a
VEID      NPROC STATUS  IP_ADDR         HOSTNAME
26         16 running 192.168.4.26    admin26

Cloning a Virtual Server with OpenVZ

There may be many reasons for cloning a VPS.  It could be to backup a server or it could be to have a template that makes it easy to create a new server.
Cloning a VPS to Create a New Server
A common goal would be to have a server set up exactly like you want and then clone it in less than five minutes have a new one running that is the same.  The best way to do this is to create a VPS and configure it exactly like you need.  That means you must install all of the necessary programs and modify them so they are working like you want.  Set up the security and networking as well.  Once you have a running VPS, then make sure it is turned off to clone it.
In this example, a VPS 26 is cloned to create 27.  The data that you will create for a VPS is located in the /vz/private/ directory.  Be sure to use the switch -a when you use cp as it is important in getting the permissions right.  The -a options maintains all of the archival attributes which is very important.
cp -a /vz/private/26 /vz/private/27
Next you need to copy the configuration for the VPS so that all of the settings are available.  The configuration files are found in /etc/vz/conf.  Once you have copied the configuration file over you will need to edit two lines:
HOSTNAME=”admin26″
IP_ADDRESS=”192.168.4.26″
These two lines are important to set up the correct IP Address and the hostname to distinguish the virtual servers.
cp /etc/vz/conf/26.conf /etc/vz/conf/27.conf
#  Copyright (C) 2000-2007 SWsoft. All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
ONBOOT=”yes”
# UBC parameters (in form of barrier:limit)
# Primary parameters
AVNUMPROC=”40:40″
NUMPROC=”65:65″
NUMTCPSOCK=”80:80″
NUMOTHERSOCK=”80:80″
VMGUARPAGES=”6144:2147483647″
# Secondary parameters
KMEMSIZE=”2752512:2936012″
TCPSNDBUF=”319488:524288″
TCPRCVBUF=”319488:524288″
OTHERSOCKBUF=”132096:336896″
DGRAMRCVBUF=”132096:132096″
OOMGUARPAGES=”6144:2147483647″
# Auxiliary parameters
LOCKEDPAGES=”32:32″
SHMPAGES=”8192:8192″
PRIVVMPAGES=”49152:53575″
NUMFILE=”2048:2048″
NUMFLOCK=”100:110″
NUMPTY=”16:16″
NUMSIGINFO=”256:256″
DCACHESIZE=”1048576:1097728″
PHYSPAGES=”0:2147483647″
NUMIPTENT=”128:128″
# Disk quota parameters (in form of softlimit:hardlimit)
DISKSPACE=”1048576:1153434″
DISKINODES=”200000:220000″
QUOTATIME=”0″
# CPU fair sheduler parameter
CPUUNITS=”1000″
VE_ROOT=”/vz/root/$VEID”
VE_PRIVATE=”/vz/private/$VEID”
OSTEMPLATE=”centos-4-i386-default”
ORIGIN_SAMPLE=”vps.basic”
HOSTNAME=”admin26″
IP_ADDRESS=”192.168.4.26″
NAMESERVER=”12.32.34.32″
Once this is complete and you have saved it you may start the VPS.