教你在 Ubuntu 上使用 LXC 容器

拋開 Docker 的好處不談,讓我們聊一下 LXC 容器的好處:我可以使用 libvirt 提供的介面來管理 LXC,這些介面和 Docker 沒有任何關係。如果你有使用基於 libvirt 庫的管理工具(例如 virt-manager 和 virsh),你就可以使用它們來管理 LXC 容器。
在這篇教程中,我只介紹標準 LXC 容器管理工具的命令行操作,來教你如何在 Ubuntu 下創建和管理 LXC 容器。
Ubuntu 下安裝 LXC
使用下面的命令安裝 LXC 在用戶態的工具:
$ sudo apt-get install lxc
然後檢查當前內核是否支持 LXC。如果所有結果都是「enable」,說明內核支持:
$ lxc-checkconfig
安裝完 LXC 工具後,就能看到 LXC 自動創建了一塊橋接網卡(lxcbr0,可以在 /etc/lxc/default.conf 中設置)。
當你創建了 LXC 容器後,它的網口會自動鏈接到這個橋接網卡上,然後這個容器就能和外部世界通信了。
創建 LXC 容器
為了在指定環境下(比如 Debian Wheezy 64位)創建 LXC 容器,你需要一個相應的 LXC 模板。幸運的是 LXC 提供的工具集成了一整套現成的 LXC 模板,你可以在 /usr/share/lxc/templates 目錄下找到它們。
$ ls /usr/share/lxc/templates
一個 LXC 模板實質上就是一個腳本,用於創建指定環境下的容器。當你創建 LXC 容器時,你需要用到它們。
比如你要新建 Ubuntu 容器,使用下面的命令即可:
$ sudo lxc-create -n <container-name> -t ubuntu
默認情況下,這個命令會創建一個最小的 Ubuntu 環境,版本號與你的宿主機一致,我這邊是「活潑的蠑螈」(版本號是13.10),64位。
當然你也可以創建任何你喜歡的版本,只要在命令裡面加一個版本參數即可。舉個例子,創建 Ubuntu 14.10 的容器:
$ sudo lxc-create -n <container-name> -t ubuntu -- --release utopic
這個命令就會下載安裝指定環境下的軟體包,創建新容器。整個過程需要幾分鐘時間,與容器的類型有關,所以,你可能需要耐心等待。
下載安裝完所有軟體包後,LXC 容器鏡像就創建完成了,你可以看到默認的登錄界面。容器被放到 /var/lib/lxc/<容器名> 這個目錄下,容器的根文件系統放在 /var/lib/lxc/<容器名>/rootfs 目錄下。
創建過程中下載的軟體包保存在 /var/cache/lxc 目錄下面,當你想另外建一個一樣的容器時,可以省去很多下載時間。
用下面的命令看看主機上所有的 LXC 容器:
$ sudo lxc-ls --fancy
NAME STATE IPV4 IPV6 AUTOSTART
---------------------------------test-lxc STOPPED - - NO
使用下面的命令啟動容器。參數「-d」將容器作為後台進程打開。如果沒有指定這個參數,你可以在控制台界面上直接把容器的運行程序關閉(LCTT譯註:Ctrl+C組合鍵)。
$ sudo lxc-start -n <container-name> -d
打開容器後,看看狀態:
$ sudo lxc-ls --fancy
NAME STATE IPV4 IPV6 AUTOSTART
--------------------------------------lxc RUNNING 10.0.3.55 - NO
容器狀態是「運行中」,容器 IP 是10.0.3.55。
你也可以看到容器的網路介面(比如我這裡是 vethJ06SFL)自動與 LXC 內部網橋(lxcbr0)連上了:
$ brctl show lxcbr0
管理 LXC 容器
我們已經學習了怎麼創建和啟動 LXC 容器,現在來看看怎麼玩一個正在運行著的容器。
第一步:打開容器控制台:
$ sudo lxc-console -n <container-name>
使用「Crtl+a q」組合鍵退出控制台。
停止、刪除容器:
$ sudo lxc-stop -n <container-name>
$ sudo lxc-destroy -n <container-name>
複製容器,用下面的命令:
$ sudo lxc-stop -n <container-name>
$ sudo lxc-clone -o <container-name> -n <new-container-name>
常見問題
這個小節主要介紹你們在使用 LXC 過程中碰到過的問題。
- 創建 LXC 容器時遇到下面的錯誤:
$ sudo lxc-create -n test-lxc -t ubuntu
lxc-create: symbol lookup error: /usr/lib/x86_64-linux-gnu/liblxc.so.1: undefined symbol: cgmanager_get_pid_cgroup_abs_sync
錯誤的原因是你運行了最新的 LXC,但是它所依賴的 libcgmanager 版本較老,兩者不兼容。升級下 libcmanager 即可解決問題:
$ sudo apt-get install libcgmanager0
via: http://xmodulo.com/lxc-containers-ubuntu.html
本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive
350fairfax nordvpn special coupon code 2025
Magnificent goods from you, man. I’ve understand your stuff previous to and you are
just extremely fantastic. I really like what you’ve acquired here, certainly like
what you are stating and the way in which you say it.
You make it entertaining and you still care for to keep it wise.
I cant wait to read far more from you. This is actually a wonderful web site.
What a stuff of un-ambiguity and preserveness of valuable familiarity
on the topic of unexpected emotions.
Also visit my website: nordvpn coupons inspiresensation
Hi to every body, it’s my first pay a quick visit of
this weblog; this weblog carries remarkable and truly excellent information for
readers.
Also visit my webpage nordvpn coupons inspiresensation
You’re so cool! I don’t believe I’ve read a single thing like this before.
So wonderful to discover someone with some genuine
thoughts on this subject matter. Really..
many thanks for starting this up. This web site is one
thing that is required on the internet, someone with
a bit of originality!
Also visit my web blog :: nordvpn coupons inspiresensation
Hi there, i read your blog from time to time and
i own a similar one and i was just curious if you get a
lot of spam responses? If so how do you prevent it, any plugin or
anything you can recommend? I get so much lately it’s driving me crazy so any help is very
much appreciated.
Here is my web page: nordvpn coupons inspiresensation (http://da.gd)