在这里主要讲述怎么安装和准备在Centos 5.5 服务器,OpenVZ可以建立多个虚拟私人服务器在同一个硬件,类似与Linux Vserver Xen项目。OpenVZ是开源的分支,在商业Virtuozzo虚拟化技术解决方案提供商,提供所使用的许多虚拟服务器,内核补丁的OpenVZ GPL授权许可,和用户级工具是在QPL执照。

       是意味着作为实用指南;它不覆盖所有的理论环境。

       这份文档是没有任何类型的保证,!. 我想说的是这并不是唯一编译内核的方法体系。有许多方法可以达到这个目标,但这是我选择的方法。我不敢保证这个方法对你也试用。

1 安装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 内核 (你可以找到更多详情,他们在这里:http://wiki.openvz.org/Kernel_flavorshttp://wiki.openvz.org/Kernel_flavors)。该命令:

yum search ovzkernel

显示系统可用的内核:

[root@server1 yum.repos.d]# yum search ovzkernel
...
ovzkernel.i686 : Virtuozzo Linux kernel (the core of the Linux operating system)
ovzkernel.x86_64 : Virtuozzo Linux kernel (the core of the Linux operating system)
ovzkernel-PAE.i686 : The Linux kernel compiled for PAE capable machines.
ovzkernel-PAE-debug.i686 : The Linux PAE kernel compiled with debug config
ovzkernel-PAE-devel.i686 : Development package for building kernel modules to match the PAE kernel.
ovzkernel-debug.i686 : The Linux kernel compiled with debug config
ovzkernel-debug.x86_64 : The Linux kernel compiled with debug config
ovzkernel-devel.i686 : Development package for building kernel modules to match the kernel.
ovzkernel-devel.x86_64 : Development package for building kernel modules to match the kernel.
ovzkernel-ent.i686 : The Linux kernel compiled for huge mem capable machines.
ovzkernel-ent-debug.i686 : The Linux ent kernel compiled with debug config
ovzkernel-ent-devel.i686 : Development package for building kernel modules to match the ent kernel.
ovzkernel-xen.i686 : The Linux kernel compiled for Xen VM operations
ovzkernel-xen.x86_64 : The Linux kernel compiled for Xen VM operations
ovzkernel-xen-devel.i686 : Development package for building kernel modules to match the kernel.
ovzkernel-xen-devel.x86_64 : Development package for building kernel modules to match the kernel.
[root@server1 yum.repos.d]#

挑选其中一个安装

yum install ovzkernel

这里应该自动的更新启动引导,无论如何我们应该打开/boot/grub/menu.lst,第一个内核文件应该控制新的OpenVZ,内核文件写的是"CentOS". 我想这是个好方法,可以让你知道这是“OpenVZ”内核,我们也要确认这是系统默认的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/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS OpenVZ (2.6.18-238.5.1.el5.028stab085.5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-238.5.1.el5.028stab085.5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-238.5.1.el5.028stab085.5.img
title CentOS (2.6.18-238.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-238.el5.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地址必须不同与主机地址,属于同一网络段,如果不是这样,网络就不会在你虚拟机工作。

打开/etc/vz/vz.conf 和set NEIGHBOUR_DEVS 设置为所有:

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 - 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

最后重启系统:

reboot

如果你的系统启动正常,那后面也会顺利

uname -r

新的内核OpenVZ会出现:

[root@server1 ~]# uname -r
2.6.18-238.5.1.el5.028stab085.5
[root@server1 ~]#

2 使用OpenVZ

我们能创建虚拟机OpenVZ,需要一个模板的分布,我们要使用虚拟机目录/vz/template/cache ,虚拟机将从这里创建这个模板,你能找到的模板在http://wiki.openvz.org/Download/template/precreated.这里。

我想使用我的CentOS 5 在虚拟机上,所以我下载Centos 5 模板:

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

我现在要用的是OpenVZ.的基本命令:

建立一个虚拟专业服务器在CentOS 5上运行:

vzctl create 101 --ostemplate centos-5-x86 --config basic

这101 必须是独特ID,每台虚拟机必须有其自己独特的ID,你用的最后部份是虚拟机的IP地址。例如,如果虚拟机的IP地址 你使用101as the ID.随着ID。

如果你想在开机启动运行:

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设置,你也同样可以直接编辑vm的配置文件储存在/etc/vz/conf ,如果vm 配置是在101,那我们可以配置/etc/vz/conf/101.conf。

开始运行重启vm,

vzctl start 101

设置root 用户与虚拟机中,

vzctl exec 101 passwd

你现在可以远程连接到vm,或者你进入如下:

vzctl enter 101

离开vm的控制台,

exit

停止运行vm:

vzctl stop 101

去重启vm:
 

vzctl restart 101

从硬盘驱动器删除vm,(在应用停止之前,可以做到),

vzctl destroy 101

你要列出vm 详情:

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]#

找出vm 的资源分配情形:

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是非常重要的,其中只还有零点,如果不是vm 需要更多资源供

分配到vm ,开放的虚拟内存配置文件/etc/vz/conf ,而提高资源在重启虚拟机,

想了解更多vzctl命令。请输入如下:

man vzctl

3链接: