天天看點

Docker系列之——lxc虛拟化快速入門

一、Cgroups

Cgroups是為Control groups的縮寫,是Linux核心的一個功能,用來限制報告和隔離一個程序組的資源(CPU、記憶體、磁盤輸入輸出等)。最初由由Google的工程師(主要是Paul Menage和Rohit Seth)在2006年以“process containers(程序容器)”的名字開始的, 在2007年的晚些時候被重命名為“控制組”并被合并到了2.6.24版的核心中。

簡單來講,Cgroup是用于将一個或多個程序關聯成“程序組”并統一進行的監控和管理的容器。根據監控或管理的需要,運作中的系統上很可能存在多個Cgroup,而這些Cgroup被組織成了層級結構(倒置的樹狀結構)。事實上,Cgroups是一種資源控制機制,其資源配置的最小機關是所謂的Subsystem——一種資源控制器,通過将資源劃分為多種不同的類型(CPU、記憶體、磁盤輸入輸出等)并将這些類型的資源關聯至層級結構中指定的Cgroup實作資源配置設定。

(待補全)

二、LXC

LXC即LinuX Containers,它是一種利用新版Linux核心的特性(Cgroups等)實作的無需hypervisor的輕型虛拟化技術。LXC能夠虛拟出一個完整的系統環境(rootfs),也可以僅為單個或多個應用程式提供虛拟化運作環境。從使用方式來看,它更像是增強版的chroot環境。

三、CentOS 6.5上使用lxc-1.0.5

3.1 解決依賴關系

# yum install libcgroup 

# service cgconfig start

3.2 提供虛拟網橋接口

在/etc/sysconfig/network-scripts目錄中建立名為ifcfg-br0的配置檔案,其内容如下:

DEVICE=br0

TYPE=Bridge

BOOTPROTO=static

IPADDR=172.16.100.7

NETMASK=255.255.0.0

GATEWAY=172.16.0.1

ONBOOT=yes

DELAY=0

NM_CONTROLLED=no

接下将橋接的實體網卡(假設為eth0)關聯至前面定義的橋接裝置,編輯/etc/sysconfig/network-script/ifcfg-eth0為類似如下内容:

DEVICE="eth0"

BOOTPROTO="static"

NM_CONTROLLED="no"

ONBOOT="yes"

TYPE="Ethernet"

BRIDGE=br0

上述步驟無誤後重新開機network服務即可。另外,還有其它簡單的方式來實作橋接裝置的建立,例如使用brctl或virsh等,這裡不再詳述。

3.3 安裝lxc

epel源中提供的lxc版本為0.9.0,其未帶centos系統模闆。是以,這裡選擇使用目前最新的lxc版本1.0.5。編譯安裝過程較為簡單,按其源碼目錄中的INSTALL文檔中的提示進行即可。我們事先經過測試後已經将lxc-1.0.5制作成了适用于centos 6 x86_64平台的rpm包(通過附件下載下傳),是以,這裡将直接使用rpm指令安裝。

# yum install lxc-1.0.5-1.el6.x86_64.rpm  lxc-libs-1.0.5-1.el6.x86_64.rpm

3.4 檢查lxc運作環境

# lxc-checkconfig 

Kernel configuration not found at /proc/config.gz; searching...

Kernel configuration found at /boot/config-2.6.32-431.el6.x86_64

--- Namespaces ---

Namespaces: enabled

Utsname namespace: enabled

Ipc namespace: enabled

Pid namespace: enabled

User namespace: enabled

Network namespace: enabled

Multiple /dev/pts instances: enabled

--- Control groups ---

Cgroup: enabled

Cgroup namespace: enabled

Cgroup device: enabled

Cgroup sched: enabled

Cgroup cpu account: enabled

Cgroup memory controller: enabled

Cgroup cpuset: enabled

--- Misc ---

Veth pair device: enabled

Macvlan: enabled

Vlan: enabled

File capabilities: enabled

Note : Before booting a new kernel, you can check its configuration

usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig

3.5 建立centos虛拟機

lxc為建立虛拟機提供了模闆檔案,它們位于/usr/share/lxc/templates目錄中。其中的lxc-centos即為建立lxc centos系統的模闆。

另外,lxc為虛拟機提供的預設配置檔案為/etc/lxc/default.conf,其中使用的橋接接口名稱為virbr0,此與前面的建立的接口名稱不一緻,是以需要作出修改。當然,也可以将此檔案複制之後進行修改,并以為作為接下來的要建立的centos虛拟機的專用配置檔案。修改後的default.conf如下所示。

lxc.network.type = veth

lxc.network.link = br0

lxc.network.flags = up

建立虛拟機centos:

# lxc-create -n centos -t /usr/share/lxc/templates/lxc-centos 

Host CPE ID from /etc/system-release-cpe: cpe:/o:centos:linux:6:GA

Checking cache download in /var/cache/lxc/centos/x86_64/6/rootfs ... 

…………

Complete!

Download complete.

Copy /var/cache/lxc/centos/x86_64/6/rootfs to /var/lib/lxc/centos/rootfs ... 

Copying rootfs to /var/lib/lxc/centos/rootfs ...

Storing root password in '/var/lib/lxc/centos/tmp_root_pass'

Expiring password for user root.

passwd: Success

Container rootfs and config have been created.

Edit the config file to check/enable networking setup.

The temporary root password is stored in:

        '/var/lib/lxc/centos/tmp_root_pass'

The root password is set up as expired and will require it to be changed

at first login, which you should do as soon as possible.  If you lose the

root password or wish to change it without starting the container, you

can change it from the host by running the following command (which will

also reset the expired flag):

        chroot /var/lib/lxc/centos/rootfs passwd

上述輸出内容表示系統安裝已經成功,可由lxc-start指令啟動了。另外,目标系統的root使用者的預設密碼在/var/lib/lxc/centos/tmp_root_pass檔案中。

3.6 啟動目标系統centos:

啟動lxc虛拟機需要使用lxc-start指令。此指令的常用選項有

-n NAME:要啟動的虛拟機的名稱

-d: 在背景運作此虛拟機

-o /path/to/somefile: 日志檔案的儲存位置

-l: 日志級别

簡單的啟動指令如下所示:

# lxc-start -n centos

啟動後可直接連接配接至目标系統的控制台,并在顯示登入提示符後登入系統即可。需要注意的是,root使用者的預設密碼已經被設定為過期,是以第一次登入時需要修改密碼後方可使用。

也可以在lxc-start的指令後面附加-d選項,讓系統運作于背景。如果要停止虛拟機,使用lxc-stop指令即可。

(詳細使用教程待完成)