CentOS系統(tǒng)上OpenVZ虛擬機(jī)的安裝和基本使用

OpenVZ是開源軟件,是基于Linux平臺(tái)的操作系統(tǒng)級(jí)服務(wù)器虛擬化解決方案。OpenVZ采用SWsoft的Virutozzo虛擬化服務(wù)器軟件產(chǎn)品的內(nèi)核,Virutozzo是SWsoft公司提供的商業(yè)虛擬化解決方案。
OpenVZ可以在單個(gè)物理服務(wù)器上創(chuàng)建多個(gè)隔離的虛擬專用服務(wù)器(VPS)并以最大效率共享硬件和管理資源。每個(gè)VPS的運(yùn)行和獨(dú)立服務(wù)器完全一致,因其自身的用戶、應(yīng)用程序都可以獨(dú)立重啟,它擁有根訪問權(quán)限、用戶、IP地址、內(nèi)存、處理器、文件、應(yīng)用服務(wù)、系統(tǒng)庫文件和配置文件等。擁有輕量級(jí)處理損耗和高效設(shè)計(jì)的OpenVZ是運(yùn)行應(yīng)用服務(wù)和實(shí)時(shí)數(shù)據(jù)生產(chǎn)型服務(wù)器虛擬化的最佳選擇。
OpenVZ軟件同時(shí)為用戶提供工具,協(xié)助自動(dòng)化管理虛擬服務(wù)器?;谀0宓膽?yīng)用程序部署可以在幾分鐘內(nèi)用簡(jiǎn)單的方法創(chuàng)建新的虛擬服務(wù)器并投入使用,而且與其他的虛擬化技術(shù)相比,OpenVZ可以在每CPU上運(yùn)行幾倍于其他虛擬化服務(wù)器技術(shù)的虛擬化服務(wù)器。
OpenVZ是基于Linux內(nèi)核和作業(yè)系統(tǒng)的操作系統(tǒng)級(jí)虛擬化技術(shù)。OpenVZ允許物理服務(wù)器運(yùn)行多個(gè)操作系統(tǒng),被稱虛擬專用服務(wù)器(VPS,Virtual Private Server)或虛擬環(huán)境(VE, Virtual Environment)。
與硬件虛擬化的VMware和半虛擬化的Xen相比,OpenVZ的host OS和guest OS都必需是Linux(雖然在不同的虛擬環(huán)境里可以用不同的Linux發(fā)行版)。但是,OpenVZ聲稱這樣做有性能上的優(yōu)勢(shì)。根據(jù)OpenVZ網(wǎng)站的說法,使用OpenVZ與使用獨(dú)立的服務(wù)器相比,性能只會(huì)有1-3%的損失。
安裝OpenVZ
要完成OpenVZ的安裝工作,我們需要先把OpenVZ庫添加到y(tǒng)um當(dāng)中:
cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ
現(xiàn)在打開openvz.repo文件:
vi openvz.repo
接下來禁用[openvz-kernel-rhel5]庫(enabled=0)并啟用[openvz-kernel-rhel6]庫作為替代(enabled=1):
[...]
[openvz-kernel-rhel6]
name=OpenVZ RHEL6-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel6-2.6.32/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel6-2.6.32
enabled=1
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[openvz-kernel-rhel5]
name=OpenVZ RHEL5-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel5-2.6.18/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel5-2.6.18
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[...]
這套庫中包含多種OpenVZ內(nèi)核(欲了解不同內(nèi)核的具體情況,請(qǐng)?jiān)L問http://wiki.openvz.org/Kernel_flavors進(jìn)行查看)。以下命令:
yum search vzkernel
將在屏幕上列出所有可用內(nèi)核:
[root@server1 yum.repos.d]# yum search vzkernel
[...]
vzkernel.i686 : The Linux kernel
vzkernel.x86_64 : The Linux kernel
vzkernel-devel.i686 : Development package for building kernel modules to match the kernel
vzkernel-devel.x86_64 : Development package for building kernel modules to match the kernel
vzkernel-firmware.noarch : Firmware files used by the Linux kernel
vzkernel-headers.i686 : Header files for the Linux kernel for use by glibc
vzkernel-headers.x86_64 : Header files for the Linux kernel for use by glibc
[...]
[root@server1 yum.repos.d]#
選擇其中之一,并按如下命令進(jìn)行安裝:
yum install vzkernel
這一操作應(yīng)該也會(huì)自動(dòng)更新GRUB引導(dǎo)程序。無論如何,我們現(xiàn)在要打開/boot/grub/menu.lst;現(xiàn)在首個(gè)內(nèi)核中應(yīng)該已經(jīng)包含有新的OpenVZ內(nèi)核了。務(wù)必確保default值為0,這樣首個(gè)內(nèi)核(也就是OpenVZ內(nèi)核)才會(huì)被自動(dòng)引導(dǎo)并替換掉默認(rèn)的CentOS內(nèi)核。
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/mapper/vg_server1-lv_root
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title OpenVZ (2.6.32-042stab057.1)
root (hd0,0)
kernel /vmlinuz-2.6.32-042stab057.1 ro root=/dev/mapper/vg_server1-lv_root rd_LVM_LV=vg_server1/lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=vg_server1/lv_swap rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-042stab057.1.img
title CentOS (2.6.32-279.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-279.el6.x86_64 ro root=/dev/mapper/vg_server1-lv_root rd_LVM_LV=vg_server1/lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=vg_server1/lv_swap rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-279.el6.x86_64.img
現(xiàn)在我們需要安裝幾款OpenVZ用戶工具:
yum install vzctl vzquota
打開/etc/sysctl.conf并確保文件中包含以下設(shè)定:
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
[...]
如果大家需要對(duì)/etc/sysctl.conf進(jìn)行修改,請(qǐng)?jiān)谏鲜雒詈筇砑右韵聝?nèi)容:
sysctl -p
如果大家的虛擬機(jī)IP地址來自不同子網(wǎng)而非直接采用主機(jī)系統(tǒng)的IP地址,那么接下來的步驟非常關(guān)鍵。如果忽略這一步,網(wǎng)絡(luò)將無法在虛擬機(jī)上正常工作。
打開/etc/vz/vz.conf 并將NEIGHBOUR_DEVS 設(shè)置為 all:
vi /etc/vz/vz.conf
[...]
NEIGHBOUR_DEVS=all
[...]
如果大家想讓OpenVZ正常工作,那么SELinux必須被禁用。打開/etc/sysconfig/selinux 并將SELINUX 的值設(shè)為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 - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
最后,重新啟動(dòng)系統(tǒng):
reboot
如果大家的系統(tǒng)順利完成了重啟,那么恭喜--安裝工作成功結(jié)束!
運(yùn)行:
uname -r
這時(shí)我們的新OpenVZ內(nèi)核應(yīng)該顯示以下內(nèi)容:
[root@server1 ~]# uname -r
2.6.32-042stab057.1
[root@server1 ~]#
在利用OpenVZ創(chuàng)建虛擬機(jī)之前,我們需要在/vz/template/cache目錄下為所要用到的發(fā)行版添加一套模板,這套模板將負(fù)責(zé)虛擬機(jī)的生成工作。大家可以在以下地址中找到預(yù)創(chuàng)建模板清單:http://wiki.openvz.org/Download/template/precreated)。
我打算在自己的虛擬機(jī)中使用CentOS 6,所以我需要下載CentOS 6模板:
cd /vz/template/cache
wget http://download.openvz.org/template/precreated/centos-6-x86_64.tar.gz
現(xiàn)在我要向大家展示使用OpenVZ所涉及的基本命令。
要通過CentOS 6模板建立一套VPS,需要運(yùn)行:
vzctl create 101 --ostemplate centos-6-x86_64 --config basic
這里的101必須是單獨(dú)ID--每套虛擬機(jī)都需要擁有專屬的獨(dú)立ID。大家可以利用虛擬機(jī)IP地址的最后三位數(shù)字來為其命名。舉例來說,如果虛擬機(jī)IP地址為192.168.0.101,那么其ID為101即可。
如果大家希望在系統(tǒng)啟動(dòng)時(shí)自動(dòng)運(yùn)行虛擬機(jī),則執(zhí)行以下命令:
vzctl set 101 --onboot yes --save
要為虛擬機(jī)設(shè)定主機(jī)名稱與IP地址,則運(yùn)行以下命令:
vzctl set 101 --hostname test.example.com --save
vzctl set 101 --ipadd 192.168.0.101 --save
接下來我們將套接口數(shù)量設(shè)置為120,并為虛擬機(jī)指派數(shù)個(gè)域名服務(wù)器:
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
(請(qǐng)注意,這里我們也可以使用vzctl set命令或者直接對(duì)/etc/vz/conf目錄下的虛擬機(jī)配置文件進(jìn)行編輯。如果虛擬機(jī)ID為 101, 那么配置文件的路徑應(yīng)為/etc/vz/conf/101.conf。)
運(yùn)行以下命令來啟動(dòng)虛擬機(jī):
vzctl start 101
執(zhí)行以下命令為虛擬機(jī)設(shè)定root密碼:
vzctl exec 101 passwd
大家現(xiàn)在已經(jīng)可以通過SSH(例如PuTTY)或者手動(dòng)輸入以下命令的方式與虛擬機(jī)連通:
vzctl enter 101
是退出虛擬機(jī)控制臺(tái),可直接輸入:
exit
停止虛擬機(jī)運(yùn)行,運(yùn)行:
vzctl stop 101
重啟虛擬機(jī)則運(yùn)行:
vzctl restart 101
如果需要從磁盤中刪除虛擬機(jī)(前提是我們已經(jīng)停止了該虛擬機(jī)的運(yùn)行),執(zhí)行以下命令:
vzctl destroy 101
要列出當(dāng)前虛擬機(jī)清單及其運(yùn)行狀態(tài),需運(yùn)行:
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]#
要查看被指派給對(duì)應(yīng)虛擬機(jī)的資源,需運(yùn)行:
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這一列的信息非常重要,正常來說所有數(shù)值都應(yīng)該為0;如果出現(xiàn)非0數(shù)字,則代表當(dāng)前虛擬機(jī)所擁有的資源不足、我們需要為其分配更多資源。打開/etc/vz/conf目錄下的虛擬機(jī)配置文件,提高對(duì)應(yīng)資源數(shù)量,最后重新啟動(dòng)虛擬機(jī)。
如果大家還需要進(jìn)行其它操作,可以運(yùn)行下列命令以查看vzctl的所有操作指令:
man vzctl
相關(guān)文章
CentOS系統(tǒng)安裝PPTP(Xen+OpenVZ)完整教程
這篇文章主要介紹了CentOS系統(tǒng)安裝PPTP(Xen+OpenVZ)完整教程,本文是在在VPS上安裝PPTP VPN實(shí)際經(jīng)驗(yàn)總結(jié)而來,需要的朋友可以參考下2014-12-30CentOS 5.4+OpenVZ+Vtonf打造VPS服務(wù)器的方法
現(xiàn)在vps受很多朋友的青睞,對(duì)于這項(xiàng)技術(shù)大家也挺熱的,不知道具體的實(shí)現(xiàn)原理,很多都是使用商業(yè)軟件實(shí)現(xiàn)的,這里用Vtonf來實(shí)現(xiàn)的,可以參考下。2010-09-23