<a target="_blank"></a>
如果你以前使用过 lxc,你可能还记得那些 lxc “模板”,基本上都是导出一个容器文件系统以及一点配置的 shell 脚本。
大多数模板是通过在本机上执行一个完整的发行版自举来生成该文件系统。这可能需要相当长的时间,并且无法在所有的发行版上可用,另外可能需要大量的网络带宽。
回到 lxc 1.0,我写了一个“下载”模板,它允许用户下载预先打包的容器镜像,用模板脚本在中央服务器上生成,接着高度压缩、签名并通过 https 分发。我们很多用户从旧版的容器生成方式切换到了使用这种新的、更快更可靠的创建容器的方式。
使用 lxd,我们通过全面的基于镜像的工作流程向前迈进了一步。所有容器都是从镜像创建的,我们在 lxd 中具有高级镜像缓存和预加载支持,以使镜像存储保持最新。
在更深入了解镜像格式之前,让我们快速了解下 lxd 可以让你做些什么。
所有的容器都是由镜像创建的。镜像可以来自一台远程服务器并使用它的完整 hash、短 hash 或者别名拉取下来,但是最终每个 lxd 容器都是创建自一个本地镜像。
这有个例子:
<code>lxc launch ubuntu:14.04 c1</code>
<code>lxc launch ubuntu:75182b1241be475a64e68a518ce853e800e9b50397d2f152816c24f038c94d6e c2</code>
<code>lxc launch ubuntu:75182b1241be c3</code>
所有这些引用相同的远程镜像(在写这篇文章时),在第一次运行这些命令其中之一时,远程镜像将作为缓存镜像导入本地 lxd 镜像存储,接着从其创建容器。
下一次运行其中一个命令时,lxd 将只检查镜像是否仍然是最新的(当不是由指纹引用时),如果是,它将创建容器而不下载任何东西。
现在镜像被缓存在本地镜像存储中,你也可以从那里启动它,甚至不检查它是否是最新的:
<code>lxc launch 75182b1241be c4</code>
最后,如果你有个名为“myimage”的本地镜像,你可以:
<code>lxc launch my-image c5</code>
如果你想复制远程的某个镜像到你本地镜像存储,但不立即从它创建一个容器,你可以使用<code>lxc image copy</code>命令。它可以让你调整一些镜像标志,比如:
<code>lxc image copy ubuntu:14.04 local:</code>
这只是简单地复制一个远程镜像到本地存储。
如果您想要通过比记住其指纹更容易的方式来记住你引用的镜像副本,则可以在复制时添加别名:
<code>lxc image copy ubuntu:12.04 local: --alias old-ubuntu</code>
<code>lxc launch old-ubuntu c6</code>
如果你想要使用源服务器上设置的别名,你可以要求 lxd 复制下来:
<code>lxc image copy ubuntu:15.10 local: --copy-aliases</code>
<code>lxc launch 15.10 c7</code>
上面的副本都是一次性拷贝,也就是复制远程镜像的当前版本到本地镜像存储中。如果你想要 lxd 保持镜像最新,就像它在缓存中存储的那样,你需要使用 <code>–auto-update</code> 标志:
<code>lxc image copy images:gentoo/current/amd64 local: --alias gentoo --auto-update</code>
如果某人给你提供了一个单独的 tarball,你可以用下面的命令导入:
<code>lxc image import <tarball></code>
如果你想在导入时设置一个别名,你可以这么做:
<code>lxc image import <tarball> --alias random-image</code>
现在如果你被给了两个 tarball,要识别哪个是含有 lxd 元数据的。通常可以通过 tarball 的名称来识别,如果不行就选择最小的那个,元数据 tarball 包是很小的。 然后将它们一起导入:
<code>lxc image import <metadata tarball> <rootfs tarball></code>
<code>lxc image import</code> 也可以与指定的 url 一起使用。如果你的一台 https web 服务器的某个路径中有<code>lxd-image-url</code> 和 <code>lxd-image-hash</code> 的标头设置,那么 lxd 就会把这个镜像拉到镜像存储中。
可以参照例子这么做:
<code>lxc image import https://dl.stgraber.org/lxd --alias busybox-amd64</code>
当拉取镜像时,lxd 还会设置一些标头,远程服务器可以检查它们以返回适当的镜像。 它们是 <code>lxd-server-architectures</code> 和 <code>lxd-server-version</code>。
这相当于一个简陋的镜像服务器。 它可以通过任何静态 web 服务器提供一中用户友好的导入镜像的方式。
现在我们本地已经有一些镜像了,让我们瞧瞧可以做些什么。我们已经介绍了最主要的部分,可以从它们来创建容器,但是你还可以在本地镜像存储上做更多。
要列出所有的镜像,运行 <code>lxc image list</code>:
<code>stgraber@dakara:~$ lxc image list</code>
<code>+---------------+--------------+--------+------------------------------------------------------+--------+----------+------------------------------+</code>
<code>| alias | fingerprint | public | description | arch | size | upload date |</code>
<code>| alpine-32 | 6d9c131efab3 | yes | alpine edge (i386) (20160329_23:52) | i686 | 2.50mb | mar 30, 2016 at 4:36am (utc) |</code>
<code>| busybox-amd64 | 74186c79ca2f | no | busybox x86_64 | x86_64 | 0.79mb | mar 30, 2016 at 4:33am (utc) |</code>
<code>| gentoo | 1a134c5951e0 | no | gentoo current (amd64) (20160329_14:12) | x86_64 | 232.50mb | mar 30, 2016 at 4:34am (utc) |</code>
<code>| my-image | c9b6e738fae7 | no | scientific linux 6 x86_64 (default) (20160215_02:36) | x86_64 | 625.34mb | mar 2, 2016 at 4:56am (utc) |</code>
<code>| old-ubuntu | 4d558b08f22f | no | ubuntu 12.04 lts amd64 (release) (20160315) | x86_64 | 155.09mb | mar 30, 2016 at 4:30am (utc) |</code>
<code>| w (11 more) | d3703a994910 | no | ubuntu 15.10 amd64 (release) (20160315) | x86_64 | 153.35mb | mar 30, 2016 at 4:31am (utc) |</code>
<code>| | 75182b1241be | no | ubuntu 14.04 lts amd64 (release) (20160314) | x86_64 | 118.17mb | mar 30, 2016 at 4:27am (utc) |</code>
你可以通过别名或者指纹来过滤:
<code>stgraber@dakara:~$ lxc image list amd64</code>
<code>+---------------+--------------+--------+-----------------------------------------+--------+----------+------------------------------+</code>
<code>| alias | fingerprint | public | description | arch | size | upload date |</code>
<code>| busybox-amd64 | 74186c79ca2f | no | busybox x86_64 | x86_64 | 0.79mb | mar 30, 2016 at 4:33am (utc) |</code>
<code>| w (11 more) | d3703a994910 | no | ubuntu 15.10 amd64 (release) (20160315) | x86_64 | 153.35mb | mar 30, 2016 at 4:31am (utc) |</code>
或者指定一个镜像属性中的键值对来过滤:
<code>stgraber@dakara:~$ lxc image list os=ubuntu</code>
<code>+-------------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+</code>
<code>| alias | fingerprint | public | description | arch | size | upload date |</code>
<code>| old-ubuntu | 4d558b08f22f | no | ubuntu 12.04 lts amd64 (release) (20160315) | x86_64 | 155.09mb | mar 30, 2016 at 4:30am (utc) |</code>
<code>| w (11 more) | d3703a994910 | no | ubuntu 15.10 amd64 (release) (20160315) | x86_64 | 153.35mb | mar 30, 2016 at 4:31am (utc) |</code>
<code>| | 75182b1241be | no | ubuntu 14.04 lts amd64 (release) (20160314) | x86_64 | 118.17mb | mar 30, 2016 at 4:27am (utc) |</code>
要了解镜像的所有信息,你可以使用<code>lxc image info</code>:
<code>stgraber@castiana:~$ lxc image info ubuntu</code>
<code>fingerprint: e8a33ec326ae7dd02331bd72f5d22181ba25401480b8e733c247da5950a7d084</code>
<code>size: 139.43mb</code>
<code>architecture: i686</code>
<code>public: no</code>
<code>timestamps:</code>
<code>created: 2016/03/15 00:00 utc</code>
<code>uploaded: 2016/03/16 05:50 utc</code>
<code>expires: 2017/04/26 00:00 utc</code>
<code>properties:</code>
<code>version: 12.04</code>
<code>aliases: 12.04,p,precise</code>
<code>architecture: i386</code>
<code>description: ubuntu 12.04 lts i386 (release) (20160315)</code>
<code>label: release</code>
<code>os: ubuntu</code>
<code>release: precise</code>
<code>serial: 20160315</code>
<code>aliases:</code>
<code>- ubuntu</code>
<code>auto update: enabled</code>
<code>source:</code>
<code>server: https://cloud-images.ubuntu.com/releases</code>
<code>protocol: simplestreams</code>
<code>alias: precise/i386</code>
编辑镜像的属性和标志的简单方法是使用:
<code>lxc image edit <alias or fingerprint></code>
这会打开默认文本编辑器,内容像这样:
<code>autoupdate: true</code>
<code>aliases: 14.04,default,lts,t,trusty</code>
<code>architecture: amd64</code>
<code>description: ubuntu 14.04 lts amd64 (release) (20160314)</code>
<code>release: trusty</code>
<code>serial: "20160314"</code>
<code>version: "14.04"</code>
<code>public: false</code>
你可以修改任何属性,打开或者关闭自动更新,或者标记一个镜像是公共的(后面详述)。
删除镜像只需要运行:
<code>lxc image delete <alias or fingerprint></code>
注意你不必移除缓存对象,它们会在过期后被 lxd 自动移除(默认上,在最后一次使用的 10 天后)。
如果你想得到目前镜像的 tarball,你可以使用<code>lxc image export</code>,像这样:
<code>stgraber@dakara:~$ lxc image export old-ubuntu .</code>
<code>output is in .</code>
<code>stgraber@dakara:~$ ls -lh *.tar.xz</code>
<code>-rw------- 1 stgraber domain admins 656 mar 30 00:55 meta-ubuntu-12.04-server-cloudimg-amd64-lxd.tar.xz</code>
<code>-rw------- 1 stgraber domain admins 156m mar 30 00:55 ubuntu-12.04-server-cloudimg-amd64-lxd.tar.xz</code>
lxd 现在支持两种镜像布局,unified 或者 split。这两者都是有效的 lxd 格式,虽然后者在与其他容器或虚拟机一起运行时更容易重用其文件系统。
lxd 专注于系统容器,不支持任何应用程序容器的“标准”镜像格式,我们也不打算这么做。
我们的镜像很简单,它们是由容器文件系统,以及包含了镜像制作时间、到期时间、什么架构,以及可选的一堆文件模板的元数据文件组成。
unified 镜像格式是 lxd 在生成镜像时使用的格式。它们是一个单独的大型 tarball,包含 <code>rootfs</code> 目录下的容器文件系统,在 tarball 根目录下有 <code>metadata.yaml</code> 文件,任何模板都放到 <code>templates</code> 目录。
tarball 可以用任何方式压缩(或者不压缩)。镜像散列是压缩后的 tarball 的 sha256 。
这种格式最常用于滚动更新镜像并已经有了一个压缩文件系统 tarball 时。
它们由两个不同的 tarball 组成,第一个只包含 lxd 使用的元数据, <code>metadata.yaml</code> 文件在根目录,任何模板都在 <code>templates</code> 目录。
第二个 tarball 只包含直接位于其根目录下的容器文件系统。大多数发行版已经有这样的 tarball,因为它们常用于引导新机器。 此镜像格式允许不经修改就重用。
两个 tarball 都可以压缩(或者不压缩),它们可以使用不同的压缩算法。 镜像散列是元数据的 tarball 和 rootfs 的 tarball 结合的 sha256。
典型的 <code>metadata.yaml</code> 文件看起来像这样:
<code>architecture: "i686"</code>
<code>creation_date: 1458040200</code>
<code>description: "ubuntu 12.04 lts server (20160315)"</code>
<code>os: "ubuntu"</code>
<code>release: "precise"</code>
<code>templates:</code>
<code>/var/lib/cloud/seed/nocloud-net/meta-data:</code>
<code>when:</code>
<code>- start</code>
<code>template: cloud-init-meta.tpl</code>
<code>/var/lib/cloud/seed/nocloud-net/user-data:</code>
<code>template: cloud-init-user.tpl</code>
<code>default: |</code>
<code>#cloud-config</code>
<code>{}</code>
<code>/var/lib/cloud/seed/nocloud-net/vendor-data:</code>
<code>template: cloud-init-vendor.tpl</code>
<code>/etc/init/console.override:</code>
<code>- create</code>
<code>template: upstart-override.tpl</code>
<code>/etc/init/tty1.override:</code>
<code>/etc/init/tty2.override:</code>
<code>/etc/init/tty3.override:</code>
<code>/etc/init/tty4.override:</code>
两个唯一的必填字段是 <code>creation date</code>(unix 纪元时间)和 <code>architecture</code>。 其他都可以保持未设置,镜像就可以正常地导入。
额外的属性主要是帮助用户弄清楚镜像是什么。 例如 <code>description</code> 属性是在 <code>lxc image list</code> 中可见的。 用户可以使用其它属性的键/值对来搜索特定镜像。
相反,这些属性用户可以通过 <code>lxc image edit</code>来编辑,<code>creation date</code> 和 <code>architecture</code> 字段是不可变的。
模板机制允许在容器生命周期中的某一点生成或重新生成容器中的一些文件。
正如你在上面的例子中看到的,我们使用在 ubuntu 中使用它们来进行 <code>cloud-init</code> 并关闭一些 init 脚本。
lxd 专注于运行完整的 linux 系统,这意味着我们期望大多数用户只使用干净的发行版镜像,而不是只用自己的镜像。
但是有一些情况下,你有自己的镜像是有必要的。 例如生产服务器上的预配置镜像,或者构建那些我们没有构建的发行版或者架构的镜像。
目前使用 lxd 构造镜像最简单的方法是将容器变成镜像。
可以这么做:
<code>lxc launch ubuntu:14.04 my-container</code>
<code>lxc exec my-container bash</code>
<code><do whatever change you want></code>
<code>lxc publish my-container --alias my-new-image</code>
你甚至可以将一个容器过去的快照变成镜像:
<code>lxc publish my-container/some-snapshot --alias some-image</code>
构建你自己的镜像也很简单。
生成容器文件系统。这完全取决于你使用的发行版。对于 ubuntu 和 debian,它将用于启动。
配置容器中该发行版正常工作所需的任何东西(如果需要任何东西)。
制作该容器文件系统的 tarball,可选择压缩它。
根据上面描述的内容写一个新的 <code>metadata.yaml</code> 文件。
创建另一个包含 <code>metadata.yaml</code> 文件的 tarball。
用下面的命令导入这两个 tarball 作为 lxd 镜像:<code>lxc image import <metadata tarball> <rootfs tarball> --alias some-name</code>
在一切都正常工作前你可能需要经历几次这样的工作,调整这里或那里,可能会添加一些模板和属性。
所有 lxd 守护程序都充当镜像服务器。除非另有说明,否则加载到镜像存储中的所有镜像都会被标记为私有,因此只有受信任的客户端可以检索这些镜像,但是如果要创建公共镜像服务器,你需要做的是将一些镜像标记为公开,并确保你的 lxd 守护进程监听网络。
最简单的共享镜像的方式是运行一个公共的 lxd 守护进程。
你只要运行:
<code>lxc config set core.https_address "[::]:8443"</code>
远程用户就可以添加你的服务器作为公共服务器:
<code>lxc remote add <some name> <ip or dns> --public</code>
他们就可以像使用任何默认的镜像服务器一样使用它们。 由于远程服务器添加了 <code>-public</code> 选项,因此不需要身份验证,并且客户端仅限于使用已标记为 <code>public</code> 的镜像。
要将镜像设置成公共的,只需使用 <code>lxc image edit</code> 编辑它们,并将 <code>public</code> 标志设置为 <code>true</code>。
如上所述,<code>lxc image import</code> 支持从静态 https 服务器下载。 基本要求是:
服务器必须支持具有有效证书的 https、tls 1.2 和 ec 算法。
当访问 <code>lxc image import</code> 提供的 url 时,服务器必须返回一个包含 <code>lxd-image-hash</code> 和<code>lxd-image-url</code> 的 http 标头。
如果你想使它动态化,你可以让你的服务器查找 lxd 在请求镜像时发送的 <code>lxd-server-architectures</code>和 <code>lxd-server-version</code> 的 http 标头,这可以让你返回符合该服务器架构的正确镜像。
<code>ubuntu:</code> 和 <code>ubuntu-daily:</code> 远端服务器不使用 lxd 协议(<code>images:</code> 使用),而是使用称为简单流(simplestreams)的不同协议。
简单流基本上是一个镜像服务器的描述格式,使用 json 来描述产品以及相关产品的文件列表。
它被各种工具,如 openstack、juju、maas 等用来查找、下载或者做镜像系统,lxd 将它作为用于镜像检索的原生协议。
虽然这的确不是提供 lxd 镜像的最简单的方法,但是如果你的镜像也被其它一些工具使用,那这也许值得考虑一下。
我希望这篇关于如何使用 lxd 管理镜像以及构建和发布镜像文章让你有所了解。对于以前的 lxc 而言,可以在一组全球分布式系统上得到完全相同的镜像是一个很大的进步,并且引导了更多可复制性的发展方向。
原文发布时间为:2017-01-10
本文来自云栖社区合作伙伴“linux中国”