使用 LXD 容器執行 Ubuntu Core

Stéphane Graber發表於2017-11-23

使用 LXD 容器執行 Ubuntu Core

Ubuntu Core 是什麼?

Ubuntu Core 是完全基於 snap 包構建,並且完全事務化的 Ubuntu 版本。

該系統大部分是隻讀的,所有已安裝的應用全部來自 snap 包,完全使用事務化更新。這意味著不管在系統更新還是安裝軟體的時候遇到問題,整個系統都可以回退到之前的狀態並且記錄這個錯誤。

最新版是在 2016 年 11 月釋出的 Ubuntu Core 16。

注意,Ubuntu Core 限制只能夠安裝 snap 包(而非 “傳統” 軟體包),並且有相當數量的 snap 包在當前環境下不能正常執行,或者需要人工干預(建立使用者和使用者組等)才能正常執行。隨著新版的 snapd 和 “core” snap 包釋出,Ubuntu Core 每週都會得到改進。

環境需求

就 LXD 而言,Ubuntu Core 僅僅相當於另一個 Linux 發行版。也就是說,snapd 需要掛載無特權的 FUSE 和 AppArmor 名稱空間以及軟體棧,像下面這樣:

  • 一個新版的使用 Ubuntu 官方核心的系統
  • 一個新版的 LXD

建立一個 Ubuntu Core 容器

當前 Ubuntu Core 映象釋出在社群的映象伺服器。你可以像這樣啟動一個新的容器:

stgraber@dakara:~$ lxc launch images:ubuntu-core/16 ubuntu-core
Creating ubuntu-core
Starting ubuntu-core

這個容器啟動需要一點點時間,它會先執行第一階段的載入程式,載入程式會確定使用哪一個映象(映象是隻讀的),並且在系統上設定一個可讀層,你不要在這一階段中斷容器執行,這個時候什麼都沒有,所以執行 lxc exec 將會出錯。

幾秒鐘之後,執行 lxc list 將會展示容器的 IP 地址,這表明已經啟動了 Ubuntu Core:

stgraber@dakara:~$ lxc list
+-------------+---------+----------------------+----------------------------------------------+------------+-----------+
|     NAME    |  STATE  |          IPV4        |                      IPV6                    |    TYPE    | SNAPSHOTS |
+-------------+---------+----------------------+----------------------------------------------+------------+-----------+
| ubuntu-core | RUNNING | 10.90.151.104 (eth0) | 2001:470:b368:b2b5:216:3eff:fee1:296f (eth0) | PERSISTENT | 0         |
+-------------+---------+----------------------+----------------------------------------------+------------+-----------+

之後你就可以像使用其他的互動一樣和這個容器進行互動:

stgraber@dakara:~$ lxc exec ubuntu-core bash
root@ubuntu-core:~# snap list
Name       Version     Rev  Developer  Notes
core       16.04.1     394  canonical  -
pc         16.04-0.8   9    canonical  -
pc-kernel  4.4.0-45-4  37   canonical  -
root@ubuntu-core:~#

更新容器

如果你一直關注著 Ubuntu Core 的開發,你應該知道上面的版本已經很老了。這是因為被用作 Ubuntu LXD 映象的程式碼每隔幾個月才會更新。Ubuntu Core 系統在重啟時會檢查更新並進行自動更新(更新失敗會回退)。

如果你想現在強制更新,你可以這樣做:

stgraber@dakara:~$ lxc exec ubuntu-core bash
root@ubuntu-core:~# snap refresh
pc-kernel (stable) 4.4.0-53-1 from 'canonical' upgraded
core (stable) 16.04.1 from 'canonical' upgraded
root@ubuntu-core:~# snap version
snap 2.17
snapd 2.17
series 16
root@ubuntu-core:~#

然後重啟一下 Ubuntu Core 系統,然後看看 snapd 的版本。

root@ubuntu-core:~# reboot
root@ubuntu-core:~# 

stgraber@dakara:~$ lxc exec ubuntu-core bash
root@ubuntu-core:~# snap version
snap 2.21
snapd 2.21
series 16
root@ubuntu-core:~#

你也可以像下面這樣檢視所有 snapd 的歷史記錄:

stgraber@dakara:~$ lxc exec ubuntu-core snap changes
ID  Status  Spawn                 Ready                 Summary
1   Done    2017-01-31T05:14:38Z  2017-01-31T05:14:44Z  Initialize system state
2   Done    2017-01-31T05:14:40Z  2017-01-31T05:14:45Z  Initialize device
3   Done    2017-01-31T05:21:30Z  2017-01-31T05:22:45Z  Refresh all snaps in the system

安裝 Snap 軟體包

以一個最簡單的例子開始,經典的 Hello World:

stgraber@dakara:~$ lxc exec ubuntu-core bash
root@ubuntu-core:~# snap install hello-world
hello-world 6.3 from 'canonical' installed
root@ubuntu-core:~# hello-world
Hello World!

接下來讓我們看一些更有用的:

stgraber@dakara:~$ lxc exec ubuntu-core bash
root@ubuntu-core:~# snap install nextcloud
nextcloud 11.0.1snap2 from 'nextcloud' installed

之後透過 HTTP 訪問你的容器就可以看到剛才部署的 Nextcloud 例項。

如果你想直接透過 git 測試最新版 LXD,你可以這樣做:

stgraber@dakara:~$ lxc config set ubuntu-core security.nesting true
stgraber@dakara:~$ lxc exec ubuntu-core bash
root@ubuntu-core:~# snap install lxd --edge
lxd (edge) git-c6006fb from 'canonical' installed
root@ubuntu-core:~# lxd init
Name of the storage backend to use (dir or zfs) [default=dir]: 

We detected that you are running inside an unprivileged container.
This means that unless you manually configured your host otherwise,
you will not have enough uid and gid to allocate to your containers.

LXD can re-use your container's own allocation to avoid the problem.
Doing so makes your nested containers slightly less safe as they could
in theory attack their parent container and gain more privileges than
they otherwise would.

Would you like to have your containers share their parent's allocation (yes/no) [default=yes]? 
Would you like LXD to be available over the network (yes/no) [default=no]? 
Would you like stale cached images to be updated automatically (yes/no) [default=yes]? 
Would you like to create a new network bridge (yes/no) [default=yes]? 
What should the new bridge be called [default=lxdbr0]? 
What IPv4 address should be used (CIDR subnet notation, “auto” or “none”) [default=auto]? 
What IPv6 address should be used (CIDR subnet notation, “auto” or “none”) [default=auto]? 
LXD has been successfully configured.

已經設定過的容器不能回退版本,但是可以在 Ubuntu Core 16 中執行另一個 Ubuntu Core 16 容器:

root@ubuntu-core:~# lxc launch images:ubuntu-core/16 nested-core
Creating nested-core
Starting nested-core 
root@ubuntu-core:~# lxc list
+-------------+---------+---------------------+-----------------------------------------------+------------+-----------+
|    NAME     |  STATE  |         IPV4        |                       IPV6                    |    TYPE    | SNAPSHOTS |
+-------------+---------+---------------------+-----------------------------------------------+------------+-----------+
| nested-core | RUNNING | 10.71.135.21 (eth0) | fd42:2861:5aad:3842:216:3eff:feaf:e6bd (eth0) | PERSISTENT | 0         |
+-------------+---------+---------------------+-----------------------------------------------+------------+-----------+

寫在最後

如果你只是想試用一下 Ubuntu Core,這是一個不錯的方法。對於 snap 包開發者來說,這也是一個不錯的工具來測試你的 snap 包能否在不同的環境下正常執行。

如果你希望你的系統總是最新的,並且整體可複製,Ubuntu Core 是一個很不錯的方案,不過這也會帶來一些相應的限制,所以可能不太適合你。

最後是一個警告,對於測試來說,這些映象是足夠的,但是當前並沒有被正式的支援。在不久的將來,官方的 Ubuntu server 可以完整的支援 Ubuntu Core LXD 映象。

附錄


來自: https://insights.ubuntu.com/2017/02/27/ubuntu-core-in-lxd-containers/

作者:Stéphane Graber 譯者:aiwhj 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

相關文章