zoukankan      html  css  js  c++  java
  • 如何在CentOS 6.4上安装并使用OpenVZ?

    http://os.51cto.com/art/201304/387720.htm

    安装OpenVZ

    要完成OpenVZ的安装工作,我们需要先把OpenVZ库添加到yum当中:

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

    现在打开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内核(欲了解不同内核的具体情况,请访问http://wiki.openvz.org/Kernel_flavors进行查看)。以下命令:

    yum search vzkernel

    将在屏幕上列出所有可用内核:

    [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

    这一操作应该也会自动更新GRUB引导程序。无论如何,我们现在要打开/boot/grub/menu.lst;现在首个内核中应该已经包含有新的OpenVZ内核了。务必确保default值为0,这样首个内核(也就是OpenVZ内核)才会被自动引导并替换掉默认的CentOS内核。

    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

    现在我们需要安装几款OpenVZ用户工具:

    yum install vzctl vzquota

    打开/etc/sysctl.conf并确保文件中包含以下设定:

    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进行修改,请在上述命令后添加以下内容:

    sysctl -p

    如果大家的虚拟机IP地址来自不同子网而非直接采用主机系统的IP地址,那么接下来的步骤非常关键。如果忽略这一步,网络将无法在虚拟机上正常工作。

    打开/etc/vz/vz.conf 并将NEIGHBOUR_DEVS 设置为 all:

    vi /etc/vz/vz.conf
    [...]
    NEIGHBOUR_DEVS=all
    [...]

    如果大家想让OpenVZ正常工作,那么SELinux必须被禁用。打开/etc/sysconfig/selinux 并将SELINUX 的值设为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

    最后,重新启动系统:

    reboot

    如果大家的系统顺利完成了重启,那么恭喜--安装工作成功结束!

    运行:

    uname -r

    这时我们的新OpenVZ内核应该显示以下内容:

    [root@server1 ~]# uname -r
    2.6.32-042stab057.1
    [root@server1 ~]#

    在利用OpenVZ创建虚拟机之前,我们需要在/vz/template/cache目录下为所要用到的发行版添加一套模板,这套模板将负责虚拟机的生成工作。大家可以在以下地址中找到预创建模板清单: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

    现在我要向大家展示使用OpenVZ所涉及的基本命令。

    要通过CentOS 6模板建立一套VPS,需要运行:

    vzctl create 101 --ostemplate centos-6-x86_64 --config basic

    这里的101必须是单独ID--每套虚拟机都需要拥有专属的独立ID。大家可以利用虚拟机IP地址的最后三位数字来为其命名。举例来说,如果虚拟机IP地址为192.168.0.101,那么其ID为101即可。

    如果大家希望在系统启动时自动运行虚拟机,则执行以下命令:

    vzctl set 101 --onboot yes --save

    要为虚拟机设定主机名称与IP地址,则运行以下命令:

    vzctl set 101 --hostname test.example.com --save
    vzctl set 101 --ipadd 192.168.0.101 --save

    接下来我们将套接口数量设置为120,并为虚拟机指派数个域名服务器:

    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, 那么配置文件的路径应为/etc/vz/conf/101.conf。)

    运行以下命令来启动虚拟机:

    vzctl start 101

    执行以下命令为虚拟机设定root密码:

    vzctl exec 101 passwd

    大家现在已经可以通过SSH(例如PuTTY)或者手动输入以下命令的方式与虚拟机连通:

    vzctl enter 101

    是退出虚拟机控制台,可直接输入:

    exit

    停止虚拟机运行,运行:

    vzctl stop 101

    重启虚拟机则运行:

    vzctl restart 101

    如果需要从磁盘中删除虚拟机(前提是我们已经停止了该虚拟机的运行),执行以下命令:

    vzctl destroy 101

    要列出当前虚拟机清单及其运行状态,需运行:

    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]#

    要查看被指派给对应虚拟机的资源,需运行:

    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这一列的信息非常重要,正常来说所有数值都应该为0;如果出现非0数字,则代表当前虚拟机所拥有的资源不足、我们需要为其分配更多资源。打开/etc/vz/conf目录下的虚拟机配置文件,提高对应资源数量,最后重新启动虚拟机。

    如果大家还需要进行其它操作,可以运行下列命令以查看vzctl的所有操作指令:

    man vzctl
  • 相关阅读:
    【原创】tyvj1038 忠诚 & 计蒜客 管家的忠诚 & 线段树(单点更新,区间查询)
    [转载]线段树模板
    并查集相当6的一篇文章~~~
    觉得一篇讲SPFA还不错的文章
    【原创】POJ 3259 Wormholes(Bellman-Ford) && 简介Bellman-Ford算法
    【原创】谈谈数据结构课后作业......尴尬不已...《图》 后面的迷宫问题
    MyBatis源码分析(七):动态代理(Mybatis核心机制)
    1026: [SCOI2009]windy数
    Spring Boot 官方文档学习(一)入门及使用
    Spring Hibernate JPA 联表查询 复杂查询
  • 原文地址:https://www.cnblogs.com/qdfu/p/5480786.html
Copyright © 2011-2022 走看看