如何在CentOS 6.4上安裝并使用OpenVZ?
譯文【51CTO精選譯文】在本篇指導(dǎo)文章中,我將向大家介紹如何為OpenVZ創(chuàng)建一套CentOS 6.4服務(wù)器。在OpenVZ的幫助下,大家可以在同一套硬件上獲得多套虛擬私有服務(wù)器(簡稱VPS),這跟Xen以及Linux Vserver項目頗為相似。OpenVZ是Virtuozzo的開源分支,作為家族長輩,Virtuozzo以商業(yè)虛擬化方案的姿態(tài)出現(xiàn)在很多提供虛擬服務(wù)器業(yè)務(wù)的供應(yīng)商處。OpenVZ內(nèi)核補丁以GPL許可為基礎(chǔ),而用戶層工具則基于QPL許可。
由于采取實用性指南的定位,這篇文章不會包含任何理論背景知識。網(wǎng)絡(luò)上背景知識方面的資源已經(jīng)非常豐富,這里我們就不再贅述。這份文檔不向大家提供任何效果保證,僅僅作為參考性材料存在。以下流程是否實際生效需視具體情況而定?!?nbsp;
安裝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)核的具體情況,請訪問http://wiki.openvz.org/Kernel_flavors進行查看)。以下命令:
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]#
選擇其中之一,并按如下命令進行安裝:
yum install vzkernel
這一操作應(yīng)該也會自動更新GRUB引導(dǎo)程序。無論如何,我們現(xiàn)在要打開/boot/grub/menu.lst;現(xiàn)在首個內(nèi)核中應(yīng)該已經(jīng)包含有新的OpenVZ內(nèi)核了。務(wù)必確保default值為0,這樣首個內(nèi)核(也就是OpenVZ內(nèi)核)才會被自動引導(dǎo)并替換掉默認的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 [...]
如果大家需要對/etc/sysctl.conf進行修改,請在上述命令后添加以下內(nèi)容:
sysctl -p
如果大家的虛擬機IP地址來自不同子網(wǎng)而非直接采用主機系統(tǒng)的IP地址,那么接下來的步驟非常關(guān)鍵。如果忽略這一步,網(wǎng)絡(luò)將無法在虛擬機上正常工作。
打開/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
最后,重新啟動系統(tǒng):
reboot
如果大家的系統(tǒng)順利完成了重啟,那么恭喜--安裝工作成功結(jié)束!
運行:
uname -r
這時我們的新OpenVZ內(nèi)核應(yīng)該顯示以下內(nèi)容:
[root@server1 ~]# uname -r 2.6.32-042stab057.1 [root@server1 ~]#
在利用OpenVZ創(chuàng)建虛擬機之前,我們需要在/vz/template/cache目錄下為所要用到的發(fā)行版添加一套模板,這套模板將負責(zé)虛擬機的生成工作。大家可以在以下地址中找到預(yù)創(chuàng)建模板清單:http://wiki.openvz.org/Download/template/precreated)。
我打算在自己的虛擬機中使用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,需要運行:
vzctl create 101 --ostemplate centos-6-x86_64 --config basic
這里的101必須是單獨ID--每套虛擬機都需要擁有專屬的獨立ID。大家可以利用虛擬機IP地址的最后三位數(shù)字來為其命名。舉例來說,如果虛擬機IP地址為192.168.0.101,那么其ID為101即可。
如果大家希望在系統(tǒng)啟動時自動運行虛擬機,則執(zhí)行以下命令:
vzctl set 101 --onboot yes --save
要為虛擬機設(shè)定主機名稱與IP地址,則運行以下命令:
vzctl set 101 --hostname test.example.com --save vzctl set 101 --ipadd 192.168.0.101 --save
接下來我們將套接口數(shù)量設(shè)置為120,并為虛擬機指派數(shù)個域名服務(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
(請注意,這里我們也可以使用vzctl set命令或者直接對/etc/vz/conf目錄下的虛擬機配置文件進行編輯。如果虛擬機ID為 101, 那么配置文件的路徑應(yīng)為/etc/vz/conf/101.conf。)
運行以下命令來啟動虛擬機:
vzctl start 101
執(zhí)行以下命令為虛擬機設(shè)定root密碼:
vzctl exec 101 passwd
大家現(xiàn)在已經(jīng)可以通過SSH(例如PuTTY)或者手動輸入以下命令的方式與虛擬機連通:
vzctl enter 101
是退出虛擬機控制臺,可直接輸入:
exit
停止虛擬機運行,運行:
vzctl stop 101
重啟虛擬機則運行:
vzctl restart 101
如果需要從磁盤中刪除虛擬機(前提是我們已經(jīng)停止了該虛擬機的運行),執(zhí)行以下命令:
vzctl destroy 101
要列出當(dāng)前虛擬機清單及其運行狀態(tài),需運行:
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]#
要查看被指派給對應(yīng)虛擬機的資源,需運行:
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)前虛擬機所擁有的資源不足、我們需要為其分配更多資源。打開/etc/vz/conf目錄下的虛擬機配置文件,提高對應(yīng)資源數(shù)量,最后重新啟動虛擬機。
如果大家還需要進行其它操作,可以運行下列命令以查看vzctl的所有操作指令:
man vzctl