Installation and basic use of OpenVZ virtual machine on CentOS system

Time:2020-11-23

OpenVZ is an open source software, which is based on Linux platform operating system level server virtualization solution. OpenVZ adopts the core of virutozzo Virtualization Server software product of swsoft. Virutozzo is a commercial virtualization solution provided by swsoft company.
OpenVZ can create multiple isolated virtual private servers (VPS) on a single physical server and share hardware and management resources with maximum efficiency. Each VPS runs exactly the same as an independent server, because its own users and applications can be restarted independently. It has root access rights, users, IP addresses, memory, processors, files, application services, system library files and configuration files. OpenVZ with lightweight processing loss and efficient design is the best choice for running application services and real-time data production server virtualization.
OpenVZ software also provides users with tools to assist in automatic management of virtual servers. Template based application deployment can create a new virtual server and put it into use in a few minutes, and OpenVZ can run several times the Virtualization Server of other Virtualization Server technologies per CPU compared with other virtualization technologies.
OpenVZ is an operating system level virtualization technology based on Linux kernel and operating system. OpenVZ allows physical servers to run multiple operating systems, known as VPS or VE.
Compared with hardware virtualized VMware and semi virtualized Xen, both host OS and guest OS of OpenVZ must be Linux (although different Linux distributions can be used in different virtual environments). However, OpenVZ claims to have performance advantages. According to the OpenVZ website, there is only 1-3% performance loss with OpenVZ compared to using a stand-alone server.
Install OpenVZ

To complete the installation of OpenVZ, we need to add the OpenVZ library to yum

Copy code

The code is as follows:

cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm –import http://download.openvz.org/RPM-GPG-Key-OpenVZ

Now open it openvz.repo Document:

Copy code

The code is as follows:

vi openvz.repo

Next, disable the [openvz-kernel-rhel5] Library (enabled = 0) and enable the [openvz-kernel-rhel6] library as an alternative (enabled = 1):

Copy code

The code is as follows:

[…]
[openvz-kernel-rhel6]
name=OpenVZ RHEL6-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel6-2.6.32/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel6-2.6.32
enabled=1
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=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[…]

This library contains a variety of OpenVZ kernels (please visit http://wiki.openvz.org/Kernel_ To view). The following commands:

Copy code

The code is as follows:

yum search vzkernel

All available kernels are listed on the screen:

Copy code

The code is as follows:

[[email protected] yum.repos.d]# yum search vzkernel
[…]
vzkernel.i686 : The Linux kernel
vzkernel.x86_64 : The Linux kernel
vzkernel-devel.i686 : Development package for building kernel modules to match the kernel
vzkernel-devel.x86_64 : Development package for building kernel modules to match the kernel
vzkernel-firmware.noarch : Firmware files used by the Linux kernel
vzkernel-headers.i686 : Header files for the Linux kernel for use by glibc
vzkernel-headers.x86_64 : Header files for the Linux kernel for use by glibc
[…]
[[email protected] yum.repos.d]#

Select one of them and install as follows:

Copy code

The code is as follows:

yum install vzkernel

This should also automatically update the grub bootloader. Anyway, we’re going to open / boot / grub now/ menu.lst The new OpenVZ kernel should now be included in the first kernel. Make sure that the default value is 0, so that the first kernel (that is, the OpenVZ kernel) will automatically boot and replace the default CentOS kernel.

Copy code

The code is as follows:

vi /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/mapper/vg_server1-lv_root
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title OpenVZ (2.6.32-042stab057.1)
root (hd0,0)
kernel /vmlinuz-2.6.32-042stab057.1 ro root=/dev/mapper/vg_server1-lv_root rd_LVM_LV=vg_server1/lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=vg_server1/lv_swap rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-042stab057.1.img
title CentOS (2.6.32-279.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-279.el6.x86_64 ro root=/dev/mapper/vg_server1-lv_root rd_LVM_LV=vg_server1/lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=vg_server1/lv_swap rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-279.el6.x86_64.img

Now we need to install several OpenVZ user tools:

Copy code

The code is as follows:

yum install vzctl vzquota

Open / etc/ sysctl.conf And make sure the file contains the following settings:

Copy code

The code is as follows:

vi /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
[…]

If you need to/ sysctl.conf To modify, please add the following after the above command:

Copy code

The code is as follows:

sysctl -p

If your virtual machine IP address comes from different subnets instead of directly using the IP address of the host system, then the next step is very important. If you ignore this step, the network will not work properly on the virtual machine.

Open / etc / VZ/ vz.conf And put neighbour_ Devs is set to all:

Copy code

The code is as follows:

vi /etc/vz/vz.conf
[…]
NEIGHBOUR_DEVS=all
[…]

If you want OpenVZ to work, SELinux must be disabled. Open / etc / sysconfig / SELinux and set the value of SELinux to disabled:

Copy code

The code is as follows:

vi /etc/sysconfig/selinux
# 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 – No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted – Targeted processes are protected,
# mls – Multi Level Security protection.
SELINUXTYPE=targeted

Finally, restart the system:

Copy code

The code is as follows:

reboot

If your system is successfully rebooted, congratulations – the installation work is successfully completed!

function:

Copy code

The code is as follows:

uname -r

At this point, our new OpenVZ kernel should display the following:

Copy code

The code is as follows:

[[email protected] ~]# uname -r

2.6.32-042stab057.1

Copy code

The code is as follows:

Before using OpenVZ to create a virtual machine, we need to add a set of templates for the distribution to be used in the / VZ / template / cache directory, which will be responsible for the generation of virtual machines. You can find a list of pre created templates at the following address: http://wiki.openvz.org/Download/template/precreated )。

I plan to use CentOS 6 in my virtual machine, so I need to download CentOS 6 template:

Copy code

The code is as follows:

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

Now I’m going to show you the basic commands involved in using OpenVZ.

To establish a set of VPS through CentOS 6 template, you need to run:

Copy code

The code is as follows:

vzctl create 101 –ostemplate centos-6-x86_64 –config basic

The 101 here must be a separate ID — each set of virtual machines needs to have its own independent ID. You can use the last three digits of the virtual machine IP address to name it. For example, if the IP address of a virtual machine is 192.168.0.101, then its ID is 101.

If you want to run the virtual machine automatically when the system starts, execute the following command:

Copy code

The code is as follows:

vzctl set 101 –onboot yes –save

To set the host name and IP address for the virtual machine, run the following command:

Copy code

The code is as follows:

vzctl set 101 –hostname test.example.com –save
vzctl set 101 –ipadd 192.168.0.101 –save

Next, we set the number of sockets to 120 and assign several domain name servers to the virtual machine

Copy code

The code is as follows:

vzctl set 101 –numothersock 120 –save
vzctl set 101 –nameserver 8.8.8.8 –nameserver 8.8.4.4 –nameserver 145.253.2.75 –save

(note that we can also use the vzctl set command or edit the virtual machine configuration file in the / etc / VZ / conf directory directly. If the virtual machine ID is 101, the path to the configuration file should be / etc / VZ / conf / 101.conf. )

Run the following command to start the virtual machine:

Copy code

The code is as follows:

vzctl start 101

Execute the following command to set the root password for the virtual machine:

Copy code

The code is as follows:

vzctl exec 101 passwd

You can now connect to the virtual machine by SSH (for example, putty) or by manually entering the following commands:

Copy code

The code is as follows:

vzctl enter 101

To exit the virtual machine console, you can directly enter:

Copy code

The code is as follows:

exit

Stop the virtual machine and run:

Copy code

The code is as follows:

vzctl stop 101

Restart the virtual machine

Copy code

The code is as follows:

vzctl restart 101

If you need to delete the virtual machine from the disk (provided that we have stopped running the virtual machine), execute the following command:

Copy code

The code is as follows:

vzctl destroy 101

To list the current list of virtual machines and their running status, run:

Copy code

The code is as follows:

vzlist -a
[[email protected] cache]# vzlist -a
CTID NPROC STATUS IP_ADDR HOSTNAME
101 14 running 192.168.0.101 test.example.com
[[email protected] cache]#

To view the resources assigned to the corresponding virtual machine, run:

Copy code

The code is as follows:

vzctl exec 101 cat /proc/user_beancounters
[[email protected] cache]# vzctl exec 101 cat /proc/user_beancounters
Version: 2.5
uid resource held maxheld barrier limit failcnt
101: 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
[[email protected] cache]#

The information in the failcnt column is very important. Normally, all values should be 0. If a non-zero number appears, it means that the current virtual machine has insufficient resources and we need to allocate more resources to it. Open the virtual machine configuration file in the / etc / VZ / conf directory to increase the number of corresponding resources, and finally restart the virtual machine.

If you need to perform other operations, you can run the following command to view all the operation instructions of vzctl:

Copy code

The code is as follows:

man vzctl