诛仙四大神兽都死了:如何在CentOS 5.X中安装OpenVZ

来源:百度文库 编辑:九乡新闻网 时间:2024/05/04 17:10:20

如何在CentOS 5.X中安装OpenVZ实践过的

祥哥哥 发表于 2010-10-5 4:55 Tuesday

为了安装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 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 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

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

修改为1

在尾部增加

#Add

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

 

最后,重启您的系统

 

init 6

 

 

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

 

用uname - r 查看您的内核

 

uname -r

 

[root@server1 ~]# uname -r

2.6.18-53.1.19.el5.028stab053.14

 

2 使用 OpenVZ

 

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

 

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

 

cd /vz/template/cache

wget -c 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 // --config vps.basic 指定要哪个 vps的配置文件 配置文件在 cd /etc/sysconfig/vz-scripts/目录下面的 自己可以指定

如果出现如下报错

[root@centos5 cache]# vzctl create 101 --ostemplate centos-5-i386-default --config vps.basic

File /etc/vz/conf/ve-vps.basic.conf-sample is not found

Creation of container private area failed

[root@centos5 cache]# vzctl create 101 --ostemplate centos-5-i386-default --config vps.basic

File /etc/vz/conf/ve-vps.basic.conf-sample is not found

Creation of container private area failed

只需要把

cd /etc/vz/conf/

 

[root@centos5 conf]# cp ve-basic.conf-sample ve-vps.basic.conf-sample

[root@centos5 conf]#

[root@centos5 conf]#

[root@centos5 conf]# vzctl create 101 --ostemplate centos-5-i386-default --config vps.basic

Creating container private area (centos-5-i386-default)

Performing postcreate actions

Container private area was created

 

 

在上面的命令中,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 10.0.2.200 --save

 

 

下面我们设置端口号为120并为虚拟服务器设置名称服务器

 

 

 

 

vzctl set 101 --numothersock 120 --save

vzctl set 101 --nameserver 202.96.128.86 --nameserver 202.96.128.143 --nameserver 8.8.8.8 --nameserver 8.8.4.4 --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

 

 

 

 

vzctl create 102 --ostemplate centos-5-i386-default --config light.conf-sample --ipadd 10.0.2.201 --hostname test2.example.com

 

 

light.conf-sample 这个模板文件高度简化的 只有 30M内存 500 硬盘

 

 

 

/usr/sbin/vtonfd -f /etc/vtonf/vtonf.conf

 

 

安装Vtonf web vps 控制面板

 

yum -y install vzpkg expect

wget -c http://mesh.dl.sourceforge.net/sourceforge/vtonf/vtonfinstaller.1.0-beta1.tar.gz

(此软件包比较大,一定要在wget后加-c参数,这样在下载停顿的时候可以按Ctrl+C结束,再续传!)

tar zxvf vtonfinstaller.1.0-beta1.tar.gz

cd vtonfinstaller.1.0-beta1

./install

 

会问你是否开始安装程序,输入yes

 

Do you wish to start the installation procedure (y/n)? : y

Starting installation

Please wait…………

 

安装完成。

 

打开vtonf控制面板:http://服务器ip:8001/

 

输入安装过程中你设置的用户名和密码

Login Name : admin

Password :

 

属于你自己的VPS已经呈现在你的眼前。

 

5、安装Vtonf汉化包(可选)

 

wget http://download.linuxzh.org/xyj/Downloads/vtonf-cn-1.0.tar.bz2

tar xvfj vtonf-cn-1.0.tar.bz2

cd vtonf-cn

./vtonf-cn.sh /usr/local/vtonf

 

汉化完成,打开登录界面会出现简体中文语言选择。

 

启动 /usr/sbin/vtonfd -f /etc/vtonf/vtonf.conf

关闭 killall -9 vtonfd