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,這裡提供了一個簡單的批量創建腳本。