阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用 LXD 容器运行 Ubuntu Core

138次阅读
没有评论

共计 5131 个字符,预计需要花费 13 分钟才能阅读完成。

使用 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 镜像发布在社区的镜像服务器。你可以像这样启动一个新的容器:

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

这个容器启动需要一点点时间,它会先执行第一阶段的加载程序,加载程序会确定使用哪一个镜像(镜像是只读的),并且在系统上设置一个可读层,你不要在这一阶段中断容器执行,这个时候什么都没有,所以执行 lxc exec 将会出错。

几秒钟之后,执行 lxc list 将会展示容器的 IP 地址,这表明已经启动了 Ubuntu Core:

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

之后你就可以像使用其他的交互一样和这个容器进行交互:

  1. stgraber@dakara:~$ lxc exec ubuntu-core bash
  2. root@ubuntu-core:~# snap list
  3. NameVersionRevDeveloperNotes
  4. core 16.04.1394 canonical -
  5. pc 16.04-0.89 canonical -
  6. pc-kernel 4.4.0-45-437 canonical -
  7. root@ubuntu-core:~#

 

更新容器

如果你一直关注着 Ubuntu Core 的开发,你应该知道上面的版本已经很老了。这是因为被用作 Ubuntu LXD 镜像的代码每隔几个月才会更新。Ubuntu Core 系统在重启时会检查更新并进行自动更新(更新失败会回退)。

如果你想现在强制更新,你可以这样做:

  1. stgraber@dakara:~$ lxc exec ubuntu-core bash
  2. root@ubuntu-core:~# snap refresh
  3. pc-kernel (stable)4.4.0-53-1from'canonical' upgraded
  4. core (stable)16.04.1from'canonical' upgraded
  5. root@ubuntu-core:~# snap version
  6. snap 2.17
  7. snapd 2.17
  8. series 16
  9. root@ubuntu-core:~#

然后重启一下 Ubuntu Core 系统,然后看看 snapd 的版本。

  1. root@ubuntu-core:~#reboot
  2. root@ubuntu-core:~#
  3. stgraber@dakara:~$ lxc exec ubuntu-core bash
  4. root@ubuntu-core:~# snap version
  5. snap 2.21
  6. snapd 2.21
  7. series 16
  8. root@ubuntu-core:~#

你也可以像下面这样查看所有 snapd 的历史记录:

  1. stgraber@dakara:~$ lxc exec ubuntu-core snap changes
  2. ID StatusSpawnReadySummary
  3. 1Done2017-01-31T05:14:38Z2017-01-31T05:14:44ZInitialize system state
  4. 2Done2017-01-31T05:14:40Z2017-01-31T05:14:45ZInitialize device
  5. 3Done2017-01-31T05:21:30Z2017-01-31T05:22:45ZRefresh all snaps in the system

 

安装 Snap 软件包

以一个最简单的例子开始,经典的 Hello World:

  1. stgraber@dakara:~$ lxc exec ubuntu-core bash
  2. root@ubuntu-core:~# snap install hello-world
  3. hello-world 6.3from'canonical' installed
  4. root@ubuntu-core:~# hello-world
  5. HelloWorld!

接下来让我们看一些更有用的:

  1. stgraber@dakara:~$ lxc exec ubuntu-core bash
  2. root@ubuntu-core:~# snap install nextcloud
  3. nextcloud 11.0.1snap2from'nextcloud' installed

之后通过 HTTP 访问你的容器就可以看到刚才部署的 Nextcloud 实例。

如果你想直接通过 git 测试最新版 LXD,你可以这样做:

  1. stgraber@dakara:~$ lxc config set ubuntu-core security.nesting true
  2. stgraber@dakara:~$ lxc exec ubuntu-core bash
  3. root@ubuntu-core:~# snap install lxd --edge
  4. lxd (edge)git-c6006fb from'canonical' installed
  5. root@ubuntu-core:~# lxd init
  6. Name of the storage backend to use(diror zfs)[default=dir]:
  7. We detected that you are running inside an unprivileged container.
  8. This means that unless you manually configured your host otherwise,
  9. you will not have enough uid and gid to allocate to your containers.
  10. LXD can re-use your container's own allocation to avoid the problem.
  11. Doing so makes your nested containers slightly less safe as they could
  12. in theory attack their parent container and gain more privileges than
  13. they otherwise would.
  14. Would you like to have your containers share their parent's allocation (yes/no)[default=yes]?
  15. Would you like LXD to be available over the network (yes/no)[default=no]?
  16. Would you like stale cached images to be updated automatically (yes/no)[default=yes]?
  17. Would you like to create a new network bridge (yes/no)[default=yes]?
  18. What should the new bridge be called [default=lxdbr0]?
  19. WhatIPv4 address should be used (CIDR subnet notation,autoornone”)[default=auto]?
  20. WhatIPv6 address should be used (CIDR subnet notation,autoornone”)[default=auto]?
  21. LXD has been successfully configured.

已经设置过的容器不能回退版本,但是可以在 Ubuntu Core 16 中运行另一个 Ubuntu Core 16 容器:

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

 

写在最后

如果你只是想试用一下 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.linuxidc.com/Linux/2017-11/148833.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计5131字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中