使用 LXD 容器運行 Ubuntu Core

導讀Ubuntu Core 是完全基於 snap 包構建,並且完全事務化的 Ubuntu版本。該系統大部分是只讀的,所有已安裝的應用全部來自 snap包,完全使用事務化更新。這意味著不管在系統更新還是安裝軟體的時候遇到問題,整個系統都可以回退到之前的狀態並且記錄這個錯誤。最新版是在2016 年 11 月發布的 Ubuntu Core 16。

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

Advertisements

環境需求

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

  1. 一個新版的使用 Ubuntu 官方內核的系統

  2. 一個新版的 LXD

創建一個 Ubuntu Core 容器

當前 Ubuntu Core 鏡像發布在社區的鏡像伺服器。你可以像這樣啟動一個新的容器:

stgraber@dakara:~$ lxc launch images:ubuntu-core/16 ubuntu-coreCreating ubuntu-coreStarting ubuntu-core

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

Advertisements

幾秒鐘之後,執行 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 bashroot@ubuntu-core:~# snap listName Version Rev Developer Notescore 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 bashroot@ubuntu-core:~# snap refreshpc-kernel (stable) 4.4.0-53-1 from 'canonical' upgradedcore (stable) 16.04.1 from 'canonical' upgradedroot@ubuntu-core:~# snap versionsnap 2.17snapd 2.17series 16root@ubuntu-core:~#然後重啟一下 Ubuntu Core 系統,然後看看 snapd 的版本。root@ubuntu-core:~# rebootroot@ubuntu-core:~#stgraber@dakara:~$ lxc exec ubuntu-core bashroot@ubuntu-core:~# snap versionsnap 2.21snapd 2.21series 16root@ubuntu-core:~#

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

stgraber@dakara:~$ lxc exec ubuntu-core snap changesID Status Spawn Ready Summary1 Done 2017-01-31T05:14:38Z 2017-01-31T05:14:44Z Initialize system state2 Done 2017-01-31T05:14:40Z 2017-01-31T05:14:45Z Initialize device3 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 bashroot@ubuntu-core:~# snap install hello-worldhello-world 6.3 from 'canonical' installedroot@ubuntu-core:~# hello-worldHello World!

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

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

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

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

stgraber@dakara:~$ lxc config set ubuntu-core security.nesting truestgraber@dakara:~$ lxc exec ubuntu-core bashroot@ubuntu-core:~# snap install lxd --edgelxd (edge) git-c6006fb from 'canonical' installedroot@ubuntu-core:~# lxd initName 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 couldin theory attack their parent container and gain more privileges thanthey 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-coreCreating nested-coreStarting nested-coreroot@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 鏡像。

附錄

  • LXD 主站:https://linuxcontainers.org/lxd

  • Github:https://github.com/lxc/lxd

  • 郵件列表:https://lists.linuxcontainers.org

  • IRC:#lxcontainers on irc.freenode.net

  • 在線試用:https://linuxcontainers.org/lxd/try-it


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

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

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

原文來自:http://www.linuxprobe.com/lxd-ubuntucore.html

Advertisements

你可能會喜歡