1、LXC的簡介
1.1、 Linux Container容器是一種核心虛拟化技術,可以提供輕量級的虛拟化,以便隔離程序和資源。
1.2、LXC為Linux Container的簡寫。可以提供輕量級的虛拟化,以便隔離程序和資源,而且不需要提供指令解釋機制以及全虛拟化的其他複雜性。相當于C++中的NameSpace。容器有效地将由單個作業系統管理的資源劃分到孤立的組中,以更好地在孤立的組之間平衡有沖突的資源使用需求。與傳統虛拟化技術相比,它的優勢在于:
(1)與主控端使用同一個核心,性能損耗小;
(2)不需要指令級模拟;
(3)不需要即時(Just-in-time)編譯;
(4)容器可以在CPU核心的本地運作指令,不需要任何專門的解釋機制;
(5)避免了準虛拟化和系統調用替換中的複雜性;
(6)輕量級隔離,在隔離的同時還提供共享機制,以實作容器與主控端的資源共享。
總結:Linux Container是一種輕量級的虛拟化的手段。
1.3、LXC與docker的關系
(1)Docker并不是LXC的替代品,Docker的底層就是使用了LXC來實作的。LXC将Linux程序沙盒化,使得程序之間互相隔離,并且能夠控制各程序的資源配置設定。
(2)在LXC的基礎之上,Docker提供了一系列更強的功能。
2、LXC的安裝
2.1、安裝lxc 和 lxc 自帶的模闆包
yum -y install lxc lxc-templates
[[email protected] yum.repos.d]# yum -y install lxc lxc-templates
已加載插件:fastestmirror, refresh-packagekit, security
設定安裝程序
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
解決依賴關系
--> 執行事務檢查
---> Package lxc.x86_64 0:1.0.11-1.el6 will be 安裝
--> 處理依賴關系 lua-lxc(x86-64) = 1.0.11-1.el6,它被軟體包 lxc-1.0.11-1.el6.x86_64 需要
--> 處理依賴關系 lua-alt-getopt,它被軟體包 lxc-1.0.11-1.el6.x86_64 需要
--> 處理依賴關系 liblxc.so.1()(64bit),它被軟體包 lxc-1.0.11-1.el6.x86_64 需要
---> Package lxc-templates.x86_64 0:1.0.11-1.el6 will be 安裝
--> 執行事務檢查
---> Package lua-alt-getopt.noarch 0:0.7.0-1.el6 will be 安裝
---> Package lua-lxc.x86_64 0:1.0.11-1.el6 will be 安裝
--> 處理依賴關系 lua-filesystem,它被軟體包 lua-lxc-1.0.11-1.el6.x86_64 需要
---> Package lxc-libs.x86_64 0:1.0.11-1.el6 will be 安裝
--> 執行事務檢查
---> Package lua-filesystem.x86_64 0:1.4.2-1.el6 will be 安裝
--> 完成依賴關系計算
依賴關系解決
=================================================================================================================================================================================
軟體包 架構 版本 倉庫 大小
=================================================================================================================================================================================
正在安裝:
lxc x86_64 1.0.11-1.el6 epel 124 k
lxc-templates x86_64 1.0.11-1.el6 epel 82 k
為依賴而安裝:
lua-alt-getopt noarch 0.7.0-1.el6 epel 6.9 k
lua-filesystem x86_64 1.4.2-1.el6 epel 24 k
lua-lxc x86_64 1.0.11-1.el6 epel 16 k
lxc-libs x86_64 1.0.11-1.el6 epel 257 k
事務概要
=================================================================================================================================================================================
Install 6 Package(s)
總下載下傳量:510 k
Installed size: 1.3 M
下載下傳軟體包:
(1/6): lua-alt-getopt-0.7.0-1.el6.noarch.rpm | 6.9 kB 00:00
(2/6): lua-filesystem-1.4.2-1.el6.x86_64.rpm | 24 kB 00:00
(3/6): lua-lxc-1.0.11-1.el6.x86_64.rpm | 16 kB 00:00
(4/6): lxc-1.0.11-1.el6.x86_64.rpm | 124 kB 00:00
(5/6): lxc-libs-1.0.11-1.el6.x86_64.rpm | 257 kB 00:00
(6/6): lxc-templates-1.0.11-1.el6.x86_64.rpm | 82 kB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總計 469 kB/s | 510 kB 00:01
運作 rpm_check_debug
執行事務測試
事務測試成功
執行事務
正在安裝 : lxc-libs-1.0.11-1.el6.x86_64 1/6
/sbin/ldconfig: /lib/libuuid.so.1 不是符号連接配接
/sbin/ldconfig: /lib/libext2fs.so.2 不是符号連接配接
/sbin/ldconfig: /lib/libattr.so.1 不是符号連接配接
/sbin/ldconfig: /lib/libpamc.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libvolume_id.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libaudit.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libacl.so.1 不是符号連接配接
/sbin/ldconfig: /lib/libdbus-1.so.3 不是符号連接配接
/sbin/ldconfig: /lib/libgmodule-2.0.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libasound.so.2 不是符号連接配接
/sbin/ldconfig: /lib/libtermcap.so.2 不是符号連接配接
/sbin/ldconfig: /lib/libauparse.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libblkid.so.1 不是符号連接配接
/sbin/ldconfig: /lib/libgobject-2.0.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libpam_misc.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libe2p.so.2 不是符号連接配接
/sbin/ldconfig: /lib/libpam.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libss.so.2 不是符号連接配接
/sbin/ldconfig: /lib/libexpat.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libcom_err.so.2 不是符号連接配接
/sbin/ldconfig: /lib/libgthread-2.0.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libwrap.so.0 不是符号連接配接
/sbin/ldconfig: /lib/libcap.so.1 不是符号連接配接
/sbin/ldconfig: /lib/libglib-2.0.so.0 不是符号連接配接
正在安裝 : lua-filesystem-1.4.2-1.el6.x86_64 2/6
正在安裝 : lua-lxc-1.0.11-1.el6.x86_64 3/6
正在安裝 : lua-alt-getopt-0.7.0-1.el6.noarch 4/6
正在安裝 : lxc-1.0.11-1.el6.x86_64 5/6
正在安裝 : lxc-templates-1.0.11-1.el6.x86_64 6/6
Verifying : lxc-1.0.11-1.el6.x86_64 1/6
Verifying : lua-lxc-1.0.11-1.el6.x86_64 2/6
Verifying : lxc-libs-1.0.11-1.el6.x86_64 3/6
Verifying : lxc-templates-1.0.11-1.el6.x86_64 4/6
Verifying : lua-alt-getopt-0.7.0-1.el6.noarch 5/6
Verifying : lua-filesystem-1.4.2-1.el6.x86_64 6/6
已安裝:
lxc.x86_64 0:1.0.11-1.el6 lxc-templates.x86_64 0:1.0.11-1.el6
作為依賴被安裝:
lua-alt-getopt.noarch 0:0.7.0-1.el6 lua-filesystem.x86_64 0:1.4.2-1.el6 lua-lxc.x86_64 0:1.0.11-1.el6 lxc-libs.x86_64 0:1.0.11-1.el6
完畢!
2.2、檢測環境
lxc-checkconfig :此指令用于檢測目前系統配置是否滿足建立 container的要求,如果輸出所有項均為enbale狀态,則表明核心支援lxc容器,否則需重新編譯核心打開相應功能項。具體如何編譯核心此文暫不涉及,有需要的可以網上找其他文獻。
lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.10.5-3.el6.x86_64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: missing
Network namespace: enabled
Multiple /dev/pts instances: enabled
--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: 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
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled
--- Checkpoint/Restore ---
checkpoint restore: missing
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
2.3建立一個名為myContainer的容器,模闆名稱為/usr/share/lxc/template/lxc-centos
# lxc-create -n myContainer -t /usr/share/lxc/template/lxc-centos
# lxc-create -n myContainer -t /usr/share/lxc/template/lxc-centos
lxc_container: utils.c: get_template_path: 1106 No such file or directory - bad template: /usr/share/lxc/template/lxc-centos
lxc_container: lxccontainer.c: lxcapi_create: 1231 bad template: /usr/share/lxc/template/lxc-centos
lxc_container: lxc_create.c: main: 274 Error creating container myContainer
上述錯誤的主要原因是:沒有lx-centos的模闆
# lxc-create -n myContainer
A template must be specified.
Use "none" if you really want a container without a rootfs.
[[email protected] yum.repos.d]# lxc-create -n myContainer -t none
使用 -t none 表示不為此容器建立使用的模闆。
2.4建立一個myContainer的容器後,我們可以使用lxc-info -n myContainer指令檢視此容器的資訊
lxc-info -n myContainer
Name: myContainer
State: STOPPED
新建立的容器位于/var/lib/lxc/ 目錄下。
此處需要注意,由于建立容器時沒有指定模闆,是以建立的容器沒有rootfs相關資訊。由于模闆是一個腳本檔案,執行了一系列操作,包括建立容器的時候挂載檔案系統,配置網絡,安裝必要軟體,建立使用者/屬組,設定密碼等。是以一旦我們沒有指定模闆,那麼建立的容器便無法正常使用。
2.5 此時使用 lxc-start -n myContainer指令嘗試啟動一個容器時,會遇到如下錯誤:
lxc-start -n myContainer
lxc-start: conf.c: instantiate_veth: 3115 failed to attach 'vethLOMVGD' to the bridge 'virbr0': No such device
lxc-start: conf.c: lxc_create_network: 3407 failed to create netdev
lxc-start: start.c: lxc_spawn: 875 failed to create the network
lxc-start: start.c: __lxc_start: 1149 failed to spawn 'myContainer'
lxc-start: lxc_start.c: main: 336 The container failed to start.
lxc-start: lxc_start.c: main: 340 Additional information can be obtained by setting the --logfile and --logpriority options.
上述錯誤的主要原因是沒有virbr0網橋。
解決上述錯誤的方法時建立此virbr0網橋,具體方法如下:
(A)下包
# yum install bridge-utils -y
(B)檢視網橋資訊
# brctl show
(C)建立virbr0 網橋
# brctl addbr virbr0
(D)檢視網橋建立成功
# ip a |grep virbr0
(E)激活網橋
# ip link set dev virbr0 up
# ip a 查詢是否激活
(F)配置網橋位址
# ip addr add 10.1.0.1/24 dev virbr0
2.6 lxc的簡單指令
(1)lxc-checkconfig:
檢查系統環境是否滿足容器使用要求;
(2)lxc-create:建立lxc容器;
lxc-create -n name [-f config_file] [ -t template]
建立一個系統對象來存儲配置和使用者資訊,該對象儲存/var/lib/lxc(注意該目錄為版本相關的,不同版本的lxc可能不同)目錄下,name為其标示符。如果配置檔案未指定,則系統會支援預設的資源隔離:PID,sysv IPC和挂載點。
預設建立的config檔案的内容:
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = virbr0
(3)lxc-start:啟動容器;
lxc-start -n NAME
(4)lxc-stop:停止容器;(停止指令很慢)
lxc-stop -n NAME
關閉一個容器。與lxc-start類似,要stop的容器必須是已經lxc-create并且lxc-start的
(5)lxc-info:檢視容器相關的資訊;
lxc-info -n NAME
(6)lxc-destory:删除處于停機狀态的容器;
徹底删除一個容器,與之相關的一切資料都不可用
(7)lxc-snapshot:建立和恢複快照;
(8)當機該容器所有的程序。程序會被鎖定,直到lxc-unfreeze進行解鎖。
lxc-freeze -n name
(9)用于解除被lxc-freeze當機的容器程序。
lxc-unfreeze -n name