CentOS 6安装和配置OpenVZ


CentOS 6安装OpenVZ可以分为两种方式,第一种比较复杂,使用rpm安装,需要手工的去下载安装包;第二种比较简单,就是通过YUM去安装。

配置YUM源

下载openvz.repo文件到/etc/yum.repos.d/目录,通过下面的命令来完成,使用root用户:

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

安装OpenVZ Kernel运行下面的命令

# yum install [o]vzkernel[-flavor]

对于RHEL6或CentOS6以上的版本,使用vzkernel,对于其它的版本使用ovzkernel,需要注意的是[ –flavor]参数是可选的,可以是-smp或-enterprise,了解更多kernel flavors

配置CentOS内核参数及其SELinux属性

配置OS内核参数,进入/etc/sysctl.conf文件,修改下面两个参数即可

#为了让VE访问外部网络,需要对IP进行转发
net.ipv4.ip_forward = 1
#主要是控制对kernel系统信息的debug功能,具体如何debug,没有深究过
kernel.sysrq = 1

SELinux是2.6版本的Linux内核提供的强制访问控制系统,安装OpenVZ需要将其禁用,进入/etc/sysconfig/selinux文件,按照如下设置

SELINUX=disabled

Reboot OS

重启系统是必须的一步,重启之后OpenVZ的kernel才会生效

安装OpenVZ用户管理工具

有两个工具需要安装:

vzctl:是管理OpenVZ的一个工具,主要功能包含create、destroy、start、stop、set parameters 等

vzquato:管理VZ磁盘配额的一个工具,一般都是有vzctl来调用的

# yum install vzctl vzquota

启动OpenVZ

启动之前可以先检查一下OpenVZ服务时候已经启动了

[root@centos6 ~]# service vz status 
OpenVZ is stopped. 
[root@centos6 ~]# service vz start 
Starting OpenVZ:                                           [  OK  ] 
Bringing up interface venet0:                              [  OK  ]

安装VE模版

下载需要安装的模板到/vz/template/cache目录(宿主机是Debain,则需要下载到/var/lib/vz/template/cache/目录)

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

安装VE需要根据自己对VE的不同要求会有所不同,安装最基本的VE follow下面几步就可以

[root@centos6 ~]# vzctl create 101 --ostemplate centos-6-x86
Creating container private area (centos-6-x86)
Performing postcreate actions
CT configuration saved to /etc/vz/conf/101.conf
Container private area was created
[root@centos6 ~]# vzctl set 101 --hostname centos.ve1 --save
CT configuration saved to /etc/vz/conf/101.conf
[root@centos6 ~]# vzctl set 101 --ipadd 192.168.0.10 --save
CT configuration saved to /etc/vz/conf/101.conf
[root@centos6 ~]# vzctl set 101 --nameserver 192.168.0.1 --save
CT configuration saved to /etc/vz/conf/101.conf

启动VE

[root@centos6 ~]# vzctl start 101
Starting container ...
Container is mounted
Adding IP address(es): 192.168.0.10
Setting CPU units: 1000
Container start in progress...

进入VE

[root@centos6 ~]# vzctl enter 101
entered into CT 101
[root@centos /]#

退出VE

[root@centos /]# exit
logout
exited from CT 101
[root@centos6 ~]# 

停止VE

[root@centos6 ~]# vzctl stop 101
Stopping container ...
Container was stopped
Container is unmounted

销毁VE

[root@centos6 ~]# vzctl destroy 101
Destroying container private area: /vz/private/101
Container private area was destroyed

查看正在宿主机上正在运行的VE

[root@centos6 ~]# vzlist
      CTID      NPROC STATUS    IP_ADDR         HOSTNAME
       101         17 running   192.168.0.10    centos.ve1

更多OpenVZ使用相关的命令可以参见官方文档

一般我们在宿主机上创建VE都是批量的,因此为了方便在一台Server上创建多个VE,这里提供了一个简单的批量创建脚本。

相关内容

    暂无相关文章