服务器在线 - 服务器软件 - 网站地图 服务器在线,专注于服务器技术!

当前位置:主页 > 云和虚拟化 > OpenVZ > 正文

CentOS系统如何安装配置OpenVZ VPS

时间:2015-01-11    来源:服务器在线    投稿:泡泡    点击:

 在这份指南中我将向您说明怎样在CentOS 5.2 server中安装和部署OpenVZ,利用OpenVZ您可以在一台服务器上部署多台VPS(即Virtual Private Servers 虚拟服务器),类似的产品还有 Xen 和 Linux Vserver project,OpenVZ是Virtuozzo(大多数虚拟服务器供应商使用的商业解决方案)的开源分支,OpenVZ kernel patch是遵循GPL许可证的, 而用户级工具遵循QPL许可证。

这是一份简明实用的指南,并不会包含太多的基础背景知识,您可以在网络上轻松寻找到很多这样的基础文档。

注意:作者不对依照本文档所做操作带来的相关一切后果负责。部署OpenVZ的方法很多,作者仅仅是抛砖引玉,说明自己的方法。

Step1 安装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

这个YUM库包含了一些不同版本的OpenVZ内核(你可以在这里找到一些详细的资料http://wiki.openvz.org/Kernel_flavors)

命令:yum search ovzkernel 可以帮助您显示可用的内核:

[root@server1 yum.repos.d]# yum search ovzkernel
Loading ”fastestmirror” plugin
Loading mirror speeds from cached hostfile
* base: ftp.halifax.rwth-aachen.de
* openvz-kernel-rhel5: openvz.proserve.nl
* updates: ftp.halifax.rwth-aachen.de
* addons: ftp.halifax.rwth-aachen.de
* openvz-utils: openvz.proserve.nl
* extras: ftp.halifax.rwth-aachen.de
openvz-kernel-rhel5        100% |=========================|   951 B     00:00
primary.xml.gz             100% |=========================|   22 kB     00:00
openvz-ker: ################################################## 14/14
openvz-utils               100% |=========================|   951 B     00:00
primary.xml.gz             100% |=========================| 6.7 kB     00:00
openvz-uti: ################################################## 26/26
ovzkernel-ent.i686 : The Linux kernel compiled for huge mem capable machines.
ovzkernel-PAE.i686 : The Linux kernel compiled for PAE capable machines.
ovzkernel-ent-devel.i686 : Development package for building kernel modules to match the ent kernel.
ovzkernel-PAE-devel.i686 : Development package for building kernel modules to match the PAE kernel.
ovzkernel.i686 : Virtuozzo Linux kernel (the core of the Linux operating system)
ovzkernel-xen.i686 : The Linux kernel compiled for Xen VM operations
ovzkernel-xen-devel.i686 : Development package for building kernel modules to match the kernel.
ovzkernel-devel.i686 : Development package for building kernel modules to match the kernel.
[root@server1 yum.repos.d]#

从中选择适合您的内核并用如下命令安装:

yum install ovzkernel

这也应该将GRUB引导程序自动更新,但是无论如何,我们应该打开 /boot/grub/menu.lst来看看

现在,第一个内核中就包含了OpenVZ,我想你最好把这个内核的标题中写入一些与OpenVZ有关的东西来提醒自己这是OpenVZ的内核,并要保证此内核(即包含OpenVZ的内核)设定为默认启动的内核。

vi /boot/grub/menu.lst

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after *** 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
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS OpenVZ (2.6.18-53.1.19.el5.028stab053.14)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-53.1.19.el5.028stab053.14 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-53.1.19.el5.028stab053.14.img
title CentOS (2.6.18-92.1.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-92.1.1.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-92.1.1.el5.img
title CentOS (2.6.18-92.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-92.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-92.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 并设置NEIGHBOUR_DEVS 的值为all

vi /etc/vz/vz.conf

[...]
NEIGHBOUR_DEVS=all
[...]

如果你想使用OpenVZ的话,还需要关掉SELinux ,通过如下操作可以实现

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

最后,重新启动您的系统

如果您的系统正常重新启动,恭喜您!

用uname - r 查看您的内核

uname -r

[root@server1 ~]# uname -r
2.6.18-53.1.19.el5.028stab053.14
[root@server1 ~]#

2 使用 OpenVZ

在我们创建虚拟服务器之前,需要在 /vz/template/cache 放置一个现有发行版的模板,并依据该模板来创建虚拟服务器,您可以在这里找到一些已经创建好的模板链接,http://wiki.openvz.org/Download/template/precreated.

在我们的例子中,我想使用CentOS 5 ,于是我下载相应的模板

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

现在我为您展示使用OpenVZ的基本命令:

以是刚才我们下载的CentOS 5的模板创建一个新的虚拟服务器

vzctl create 101 –ostemplate centos-5-i386-default –config vps.basic

在上面的命令中,101是唯一的ID,每个服务器需要有自己的唯一 ID,您可以用虚拟服务器的IP地址最后一位来用作唯一ID,例如,您的虚拟服务器IP为 192.168.0.101,您就可以用101做您的唯一ID了。

如果您想让刚才创建的虚拟服务器开机(当然是真实的主机)时启动,输入:

vzctl set 101 –onboot yes –save

为虚拟服务器设置hostname和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 213.133.98.98 –nameserver 213.133.99.99 –nameserver 213.133.100.100 –nameserver 145.253.2.75 –save

除了使用vzctl命令,您还可以直接编辑虚拟服务器的配置文件,假如是我们刚建立的ID为101的虚拟服务器,那么它的配置文件则位于:/etc/vz/conf/101.conf

 

启动虚拟服务器:

vzctl start 101

为虚拟服务器设置root密码

vzctl exec 101 passwd

你可以用ssh访问您的虚拟服务器,也可以使用如下命令:

vzctl enter 101

如果要离开虚拟服务器控制台,键入:

exit

停止一台虚拟服务器:

vzctl stop 101

重新启动虚拟服务器:

vzctl restart 101

从硬盘上删除一台虚拟服务器(删除之前务必做停止的操作)

vzctl destroy 101

得到当前主机上虚拟服务器的列表:

vzlist -a

root@server1 cache]# vzlist -a
VEID       NPROC STATUS   IP_ADDR          HOSTNAME
101          18 running 192.168.0.101    test.example.com
[root@server1 cache]#

显示虚拟服务器占用资源

[root@server1 cache]# vzlist -a
VEID       NPROC STATUS   IP_ADDR          HOSTNAME
101          18 running 192.168.0.101    test.example.com
[root@server1 cache]#

To find out about the resources allocated to a vm, run

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,如果不是,则表示虚拟服务器需要得到更多的资源,打开配置文件 /etc/vz/conf 并增加适当的资源,然后重新启动虚拟服务器。

寻求更详细的 vzctl 命令帮助,请使用:

man vzctl 

如果您的问题仍未解决,还可以加入服务器在线技术交流QQ群:8017413寻求帮助。


相关内容
最新热点内容
推荐内容