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是SWsoft, Inc.公司开发的专有软件Virtuozzo的基础。OpenVZ的授权为GPLv2。
1、安装
wget http://download.openvz.org/openvz.repo
rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ
yum search ovzkernel
yum install ovzkernel ovzkernel-devel vzctl vzquota
2、配置ip转发和kernel debug功能,关闭selinux
vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
kernel.sysrq = 1
vi /etc/sysconfig/selinux
SELINUX=disabled
3、重启
reboot
4、查看内核版本
uname -r
2.6.32-042stab116.1
5、查看服务状态
service vz status
OpenVZ is running...
6、下载ve模板
cd /vz/template/cache
wget http://download.openvz.org/template/precreated/centos-6-x86.tar.gz
7、创建VZ
[root@lsn-linux ~]# vzctl create 101 --ostemplate centos-6-x86 --config basic
Creating container private area (centos-6-x86)
Performing postcreate actions
CT configuration saved to /etc/vz/conf/101.conf
Container private area was created
备注1:默认VZ文件系统为poolp,由于创建时报错如下,在/etc/vz/vz.conf中将VE_LAYOUT的ploop改为simfs后可以。
Can't load ploop library: libploop.so: cannot open shared object file: No such file or directory
Please install ploop packages!
Alternatively, if you can't or don't want to use ploop, please
add --layout simfs option, or set VE_LAYOUT=simfs in /etc/vz/vz.conf
Creation of container private area failed
备注2:可以指定模板指定配置,也可以在vz.conf配置默认的模板和配置,然后使用命令 vzctl create 101 创建即可。
8、VZ配置
启动
[root@lsn-linux ~]# vzctl set 101 --onboot yes --save
CT configuration saved to /etc/vz/conf/101.conf
配置主机名网络域名
[root@lsn-linux ~]# vzctl set 101 --hostname vm101.lsn.com --save
CT configuration saved to /etc/vz/conf/101.conf
[root@lsn-linux ~]# vzctl set 101 --ipadd 10.0.0.1 --save
CT configuration saved to /etc/vz/conf/101.conf
[root@lsn-linux ~]# vzctl set 101 --nameserver 192.168.1.1 --save
CT configuration saved to /etc/vz/conf/101.conf
设置VZ root密码
[root@lsn-linux ~]# vzctl set 101 --userpasswd root:123456
Changing password for user root.
passwd: all authentication tokens updated successfully.
备注:其实在/etc/vz/conf/101.conf配置也可以。
9、启动VZ
[root@lsn-linux ~]# vzctl start 101
Starting container...
Container is mounted
Adding IP address(es): 10.0.0.1
Setting CPU units: 1000
Container start in progress...
You have new mail in /var/spool/mail/root
10、扩展命令管理,使用vzctl exec 直接在VZ中执行命令
[root@lsn-linux ~]# vzctl exec 101 service sshd status
openssh-daemon (pid 531) is running...
[root@lsn-linux ~]# vzctl exec 101 hostname
vm101.lsn.com
在所有VZ上执行命令
for CT in $(vzlist -H -o ctid); do echo "== CT $CT =="; vzctl exec $CT command; done
[root@single-coremail vz]# for CT in $(vzlist -H -o ctid); do echo "== CT $CT =="; vzctl exec $CT uptime; done
== CT 101 ==
02:22:23 up 20 min, 0 users, load average: 0.00, 0.00, 0.00
11、查看