如何在Ubuntu上建立及管理LXC容器?,ubuntu建立lxc容器
將LXC安裝到Ubuntu上
$ sudo apt-get install lxc
安裝完畢之後,運行lxc-checkconifg工具,檢查當前Linux核心支援LXC的情況。要是一切都已被啟用,核心對LXC的支援已準備就緒。
$ lxc-checkconfig
安裝LXC工具後,你會發現,LXC的預設橋接器介面(lxcbr0)已自動建立(已在/etc/lxc/default.conf中加以配置)。
$ ifconfig lxcbr0
建立LXC容器後,視窗的介面就會自動連接到該橋接器,那樣容器就能與外界進行聯絡了。
建立LXC容器
在/usr/share/lxc/templates目錄下找到可用的LXC模板。
$ ls /usr/share/lxc/templates
1.Create a configration file, named lxc_common.conf:
lxc.network.type = vethlxc.network.flags = uplxc.network.name = eth0lxc.network.link = lxcbr0
2.Change mirrors:
sudo vim /usr/share/lxc/templates/ubuntu
Change this to the faster mirror server you know:
MIRROR=http://archive.ubuntu.com/ubuntuSECURITY_MIRROR=http://security.ubuntu.com/ubuntu
3.LXC模板其實就是一段指令碼而已,用來為某個特定的Linux環境建立容器。你在建立LXC容器時,需要用到這其中一個模板。
比如說,為了建立Ubuntu容器,使用下面這個命令列:
$ sudo lxc-create -n precise -f lxc_common.conf -t ubuntu -- -r precise
4.Configure the network, sudo vim /etc/default/lxc-net, change the configs if you like, I have uncomment this line:
LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf
5.Use a fixed ip address for “precise” container, sudo vim /etc/lxc/dnsmasq.conf:
dhcp-host=precise,10.0.3.2
管理LXC容器
既然我們已知道了如何建立及啟動LXC容器,現在不妨看看我們可以如何處理運行中的容器。
首先,我們想要訪問容器的控制台。為此,鍵入這個命令:
$ sudo lxc-console -n precise
Restart lxc-net:
$ sudo service lxc-net restart
Start the lxc container:
$ sudo lxc-start -n precise
Stop the lxc container:
$ sudo lxc-stop -n precise
詳細內容請看:LXC安裝與配置