手机
当前位置:查字典教程网 >操作系统 >RedHat/Centos >CentOS系统上OpenVZ虚拟机的安装和基本使用
CentOS系统上OpenVZ虚拟机的安装和基本使用
摘要:OpenVZ是开源软件,是基于Linux平台的操作系统级服务器虚拟化解决方案。OpenVZ采用SWsoft的Virutozzo虚拟化服务器软...

OpenVZ是开源软件,是基于Linux平台的操作系统级服务器虚拟化解决方案。OpenVZ采用SWsoft的Virutozzo虚拟化服务器软件产品的内核,Virutozzo是SWsoft公司提供的商业虚拟化解决方案。

OpenVZ可以在单个物理服务器上创建多个隔离的虚拟专用服务器(VPS)并以最大效率共享硬件和管理资源。每个VPS的运行和独立服务器完全一致,因其自身的用户、应用程序都可以独立重启,它拥有根访问权限、用户、IP地址、内存、处理器、文件、应用服务、系统库文件和配置文件等。拥有轻量级处理损耗和高效设计的OpenVZ是运行应用服务和实时数据生产型服务器虚拟化的最佳选择。

OpenVZ软件同时为用户提供工具,协助自动化管理虚拟服务器。基于模板的应用程序部署可以在几分钟内用简单的方法创建新的虚拟服务器并投入使用,而且与其他的虚拟化技术相比,OpenVZ可以在每CPU上运行几倍于其他虚拟化服务器技术的虚拟化服务器。

OpenVZ是基于Linux内核和作业系统的操作系统级虚拟化技术。OpenVZ允许物理服务器运行多个操作系统,被称虚拟专用服务器(VPS,Virtual Private Server)或虚拟环境(VE, Virtual Environment)。

与硬件虚拟化的VMware和半虚拟化的Xen相比,OpenVZ的host OS和guest OS都必需是Linux(虽然在不同的虚拟环境里可以用不同的Linux发行版)。但是,OpenVZ声称这样做有性能上的优势。根据OpenVZ网站的说法,使用OpenVZ与使用独立的服务器相比,性能只会有1-3%的损失。

安装OpenVZ

要完成OpenVZ的安装工作,我们需要先把OpenVZ库添加到yum当中:

cd /etc/yum.repos.d

wget http://download.openvz.org/openvz.repo

rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ

现在打开openvz.repo文件:

vi openvz.repo

接下来禁用[openvz-kernel-rhel5]库(enabled=0)并启用[openvz-kernel-rhel6]库作为替代(enabled=1):

[...]

[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

[...]

这套库中包含多种OpenVZ内核(欲了解不同内核的具体情况,请访问http://wiki.openvz.org/Kernel_flavors进行查看)。以下命令:

yum search vzkernel

将在屏幕上列出所有可用内核:

[root@server1 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

[...]

[root@server1 yum.repos.d]#

选择其中之一,并按如下命令进行安装:

yum install vzkernel

这一操作应该也会自动更新GRUB引导程序。无论如何,我们现在要打开/boot/grub/menu.lst;现在首个内核中应该已经包含有新的OpenVZ内核了。务必确保default值为0,这样首个内核(也就是OpenVZ内核)才会被自动引导并替换掉默认的CentOS内核。

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

现在我们需要安装几款OpenVZ用户工具:

yum install vzctl vzquota

打开/etc/sysctl.conf并确保文件中包含以下设定:

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

[...]

如果大家需要对/etc/sysctl.conf进行修改,请在上述命令后添加以下内容:

sysctl -p

如果大家的虚拟机IP地址来自不同子网而非直接采用主机系统的IP地址,那么接下来的步骤非常关键。如果忽略这一步,网络将无法在虚拟机上正常工作。

打开/etc/vz/vz.conf 并将NEIGHBOUR_DEVS 设置为 all:

vi /etc/vz/vz.conf

[...]

NEIGHBOUR_DEVS=all

[...]

如果大家想让OpenVZ正常工作,那么SELinux必须被禁用。打开/etc/sysconfig/selinux 并将SELINUX 的值设为disabled:

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

最后,重新启动系统:

reboot

如果大家的系统顺利完成了重启,那么恭喜--安装工作成功结束!

运行:

uname -r

这时我们的新OpenVZ内核应该显示以下内容:

[root@server1 ~]# uname -r

2.6.32-042stab057.1

[root@server1 ~]#

在利用OpenVZ创建虚拟机之前,我们需要在/vz/template/cache目录下为所要用到的发行版添加一套模板,这套模板将负责虚拟机的生成工作。大家可以在以下地址中找到预创建模板清单:http://wiki.openvz.org/Download/template/precreated)。

我打算在自己的虚拟机中使用CentOS 6,所以我需要下载CentOS 6模板:

cd /vz/template/cache

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

现在我要向大家展示使用OpenVZ所涉及的基本命令。

要通过CentOS 6模板建立一套VPS,需要运行:

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

这里的101必须是单独ID--每套虚拟机都需要拥有专属的独立ID。大家可以利用虚拟机IP地址的最后三位数字来为其命名。举例来说,如果虚拟机IP地址为192.168.0.101,那么其ID为101即可。

如果大家希望在系统启动时自动运行虚拟机,则执行以下命令:

vzctl set 101 --onboot yes --save

要为虚拟机设定主机名称与IP地址,则运行以下命令:

vzctl set 101 --hostname test.example.com --save

vzctl set 101 --ipadd 192.168.0.101 --save

接下来我们将套接口数量设置为120,并为虚拟机指派数个域名服务器:

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

(请注意,这里我们也可以使用vzctl set命令或者直接对/etc/vz/conf目录下的虚拟机配置文件进行编辑。如果虚拟机ID为 101, 那么配置文件的路径应为/etc/vz/conf/101.conf。)

运行以下命令来启动虚拟机:

vzctl start 101

执行以下命令为虚拟机设定root密码:

vzctl exec 101 passwd

大家现在已经可以通过SSH(例如PuTTY)或者手动输入以下命令的方式与虚拟机连通:

vzctl enter 101

是退出虚拟机控制台,可直接输入:

exit

停止虚拟机运行,运行:

vzctl stop 101

重启虚拟机则运行:

vzctl restart 101

如果需要从磁盘中删除虚拟机(前提是我们已经停止了该虚拟机的运行),执行以下命令:

vzctl destroy 101

要列出当前虚拟机清单及其运行状态,需运行:

vzlist -a

[root@server1 cache]# vzlist -a

CTID NPROC STATUS IP_ADDR HOSTNAME

101 14 running 192.168.0.101 test.example.com

[root@server1 cache]#

要查看被指派给对应虚拟机的资源,需运行:

vzctl exec 101 cat /proc/user_beancounters

[root@server1 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

[root@server1 cache]#

failcnt这一列的信息非常重要,正常来说所有数值都应该为0;如果出现非0数字,则代表当前虚拟机所拥有的资源不足、我们需要为其分配更多资源。打开/etc/vz/conf目录下的虚拟机配置文件,提高对应资源数量,最后重新启动虚拟机。

如果大家还需要进行其它操作,可以运行下列命令以查看vzctl的所有操作指令:

man vzctl

【CentOS系统上OpenVZ虚拟机的安装和基本使用】相关文章:

CentOS系统安装PPTP(Xen+OpenVZ)完整教程

linux系统下rpm包的安装、删除、效验、查询

linux 操作系统 uniq命令的说明和使用

CentOS系统下安装scikit-learn的方法

LumaQQ的安装和使用

CentOS系统中安装高亮vim编辑工具的方法

CentOS操作系统安装CMake图文步骤

CentOS中Xen虚拟机的安装与基本操作教程

在CentOS系统中编译安装Hiawatha服务器的教程

在CentOS中安装和配置Xen虚拟机的教程

精品推荐
分类导航