Install Multiple VPS On Ubuntu With OpenVZ

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 can have root access, users, IP addresses, memory, processes, files, applications, system libraries, and configuration files. OpenVZ is free open source software, available under the GNU GPL.

Before installing OpenVZ please remember this:

  • OpenVZ was only supported in Ubuntu 8.04 LTS (Hardy Heron), and not in Ubuntu 10.04 (Lucid Lynx).
  • At the time of writing (November 2010), the OpenVZ patch for kernel Linux 2.6.32 works but is not yet released as stable, and for this reason is not recommended for production environments.
  • The Ext4 filesystem is not yet supported by vzquota. If you want host kernel stability, better use Ext3 for containers space in /vz or /var/lib/vz.

How to install and configure OpenVZ on Ubuntu server:

1. Open your terminal and log in as root (otherwise you should prepend all the commands below with su).

2. In case your default shell is /bin/dash, you should change it to /bin/bash:

dpkg-reconfigure dash
#install dash as /bin/sh? N

3. In case you have AppArmor installed, remove it!

/etc/init.d/apparmor stop
update-rc.d -f apparmor remove
apt-get remove apparmor apparmor-utils

4. Find your kernel series:

uname -r
#Mine returns 2.6.32-24-server

5. Install these packages to build a new kernel:

aptitude install kernel-package libncurses5-dev fakeroot wget bzip2 module-assistant debhelper

Then build the kernel:

aptitude build-dep linux-image-2.6.32-24-server

6. Download the 2.6.32 kernel sources plus the OpenVZ kernel patch and the official config for the OpenVZ kernel:

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.32.tar.bz2
m-a prepare
wget http://download.openvz.org/kernel/branches/2.6.32/2.6.32-belyayev.1/patches/patch-belyayev.1-combined.gz
wget http://download.openvz.org/kernel/branches/2.6.32/2.6.32-belyayev.1/configs/kernel-2.6.32-x86_64.config.ovz

7. Then run this command:

kernel-packageconfig
echo "CONCURRENCY_LEVEL := 5" >> /etc/kernel-pkg.conf
#Note: CONCURRENCY_LEVEL is same as your CPU core + 1. I have 4 core CPU so i make it 5

8. Patch the kernel sources and prepare the build:

tar -xpf linux-2.6.32.tar.bz2
mv linux-2.6.32 linux-2.6.32-openvz
rm -f linux
ln -s linux-2.6.32-openvz linux
cd linux
gunzip -dc ../patch-belyayev.1-combined.gz | patch -p1
cp -rf ../kernel-2.6.32-x86_64.config.ovz .config
make oldconfig

9. Build the OpenVZ kernel with the following command:

fakeroot make-kpkg --initrd --append-to-version=-ovz32 --revision=1.0 kernel_image kernel_headers

10. Look for the new compiled kernel:

cd ..
ls -l *.deb
# My result:
#-rw-r--r-- 1 root src  6300866 2010-11-25 19:18 linux-headers-2.6.32.14-ovz32_1.0_amd64.deb
#-rw-r--r-- 1 root src 26274680 2010-11-25 19:12 linux-image-2.6.32.14-ovz32_1.0_amd64.deb

11. Install the new kernel:

dpkg -i linux-image-2.6.32.14-ovz32_1.0_amd64.deb linux-headers-2.6.32.14-ovz32_1.0_amd64.deb

12. Then update the initrd and the GRUB bootloader configuration:

mkinitramfs -k 2.6.32.14-ovz32 -o /boot/initrd.img-2.6.32.14-ovz32
update-grub

13. Build the OpenVZ tools (vzctl, vzquota, vzpkg):

Prerequisite:

aptitude install git-core git-buildpackage automake autoconf libtool liblockfile-simple-perl cstream liblog-agent-perl ssl-cert postfix rpm yum alien

vzctl:

cd
git clone git://git.openvz.org/pub/vzctl vzctl
cd vzctl
./autogen.sh
./configure --enable-bashcomp --enable-logrotate
make
make install
make install-debian

vzquota:

cd
git clone git://git.openvz.org/pub/vzquota vzquota
cd vzquota
make
make install

vzpkg:

cd
git clone git://git.openvz.org/pub/vzpkg vzpkg
cd vzpkg
make install

14. Run OpenVZ on startup:

update-rc.d vz defaults
ln -s /vz /var/lib/vz

15. Check sysctl.conf:

vim /etc/sysctl.conf

Should be like this:

net.ipv4.conf.all.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.default.forwarding=1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.ip_forward=1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.eth0.proxy_arp=1

Reload sysctl.conf:

sysctl -p

16. Open /etc/vz/vz.conf and set NEIGHBOUR_DEVS to all:

vim /etc/vz/vz.conf

Set:

NEIGHBOUR_DEVS=all

17. Reboot the machine:

reboot

18. Check your kernel — it should have changed now:

uname -r
#2.6.32.14-ovz32

OpenVZ is now installed. Now we need to make a virtual machine in it:

1. Download the OpenVZ template and place it under the /vz/template/cache directory. You can download it from this link: http://ftp.openvz.org/template/precreated/contrib/

2. I’m downloading Ubuntu 10.04 for this example:

cd /vz/template/cache
wget http://download.openvz.org/template/precreated/ubuntu-10.04-x86_64.tar.gz

3. Set up a VPS from the Ubuntu 10.04 template:

vzctl create 99 --ostemplate ubuntu-10.04-x86_64 --config basic

Note: 99 must be a unique ID — each virtual machine must have its own unique ID.

4. Run the virtual machine on boot:

vzctl set 99 --onboot yes --save

5. Set a hostname and IP address for the VM:

vzctl set 99 --hostname ivankristianto.com --save
vzctl set 99 --ipadd 192.168.0.99 --save

6. Set the number of sockets to 120 and assign nameservers:

vzctl set 99 --numothersock 120 --save
vzctl set 99 --nameserver 208.67.222.222 --nameserver 208.67.220.220 --save

Note: Virtual machine configuration is located in /etc/vz/conf/{vm-id}.conf

7. Start the VM:

vzctl start 99

8. Now you have a virtual machine. Test it and have fun with it.

Additional vzctl commands:

To set a root password:

vzctl exec {vm-id} passwd

Connect to VM via SSH:

vzctl enter {vm-id}

Stop VM:

vzctl stop {vm-id}

Restart VM:

vzctl restart {vm-id}

Delete VM:

vzctl destroy {vm-id}

List of your VMs and status:

vzlist -a

Find more commands in the vzctl manual:

man vzctl

Useful links: