在 Ubuntu 上体验 LXD 容器

在 Ubuntu 上体验 LXD 容器

本文的主角是容器,一种类似虚拟机但更轻量级的构造。你可以轻易地在你的 Ubuntu 桌面系统中创建一堆容器!

虚拟机会虚拟出整个电脑让你来安装客户机操作系统。相比之下,容器复用了主机的 Linux 内核,只是简单地 包容 了我们选择的根文件系统(也就是运行时环境)。Linux 内核有很多功能可以将运行的 Linux 容器与我们的主机分割开(也就是我们的 Ubuntu 桌面)。

Linux 本身需要一些手工操作来直接管理他们。好在,有 LXD(读音为 Lex-deeh),这是一款为我们管理 Linux 容器的服务。

我们将会看到如何:

  1. 在我们的 Ubuntu 桌面上配置容器,
  2. 创建容器,
  3. 安装一台 web 服务器,
  4. 测试一下这台 web 服务器,以及
  5. 清理所有的东西。

 

设置 Ubuntu 容器

如果你安装的是 Ubuntu 16.04,那么你什么都不用做。只要安装下面所列出的一些额外的包就行了。若你安装的是 Ubuntu 14.04.x 或 Ubuntu 15.10,那么按照 LXD 2.0 系列(二):安装与配置 来进行一些操作,然后再回来。

确保已经更新了包列表:

  1. <span class="kwd">sudo</span><span class="pln"> apt update</span>
  2. <span class="kwd">sudo</span><span class="pln"> apt upgrade</span>

安装 lxd 包:

  1. <span class="kwd">sudo</span><span class="pln"> apt install lxd</span>

若你安装的是 Ubuntu 16.04,那么还可以让你的容器文件以 ZFS 文件系统的格式进行存储。Ubuntu 16.04 的 Linux kernel 包含了支持 ZFS 必要的内核模块。若要让 LXD 使用 ZFS 进行存储,我们只需要安装 ZFS 工具包。没有 ZFS,容器会在主机文件系统中以单独的文件形式进行存储。通过 ZFS,我们就有了写入时拷贝等功能,可以让任务完成更快一些。

安装 zfsutils-linux 包(若你安装的是 Ubuntu 16.04.x):

  1. <span class="kwd">sudo</span><span class="pln"> apt install zfsutils</span><span class="pun">-</span><span class="pln">linux</span>

安装好 LXD 后,包安装脚本应该会将你加入 lxd 组。该组成员可以使你无需通过 sudo 就能直接使用 LXD 管理容器。根据 Linux 的习惯,你需要先登出桌面会话然后再登录 才能应用 lxd 的组成员关系。(若你是高手,也可以通过在当前 shell 中执行 newgrp lxd 命令,就不用重登录了)。

在开始使用前,LXD 需要初始化存储和网络参数。

运行下面命令:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lxd </span><span class="kwd">init</span>
  2. <span class="typ">Name</span><span class="pln"> of the storage backend to </span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">dir</span><span class="kwd">or</span><span class="pln"> zfs</span><span class="pun">):</span><span class="pln"> zfs</span>
  3. <span class="typ">Create</span><span class="pln"> a </span><span class="kwd">new</span><span class="pln"> ZFS pool </span><span class="pun">(</span><span class="kwd">yes</span><span class="pun">/</span><span class="kwd">no</span><span class="pun">)?</span><span class="kwd">yes</span>
  4. <span class="typ">Name</span><span class="pln"> of the </span><span class="kwd">new</span><span class="pln"> ZFS pool</span><span class="pun">:</span><span class="pln"> lxd</span><span class="pun">-</span><span class="pln">pool</span>
  5. <span class="typ">Would</span><span class="pln"> you like to </span><span class="kwd">use</span><span class="pln"> an existing block device </span><span class="pun">(</span><span class="kwd">yes</span><span class="pun">/</span><span class="kwd">no</span><span class="pun">)?</span><span class="kwd">no</span>
  6. <span class="typ">Size</span><span class="kwd">in</span><span class="pln"> GB of the </span><span class="kwd">new</span><span class="pln"> loop device </span><span class="pun">(</span><span class="lit">1GB</span><span class="pln"> minimum</span><span class="pun">):</span><span class="lit">30</span>
  7. <span class="typ">Would</span><span class="pln"> you like LXD to be available over the network </span><span class="pun">(</span><span class="kwd">yes</span><span class="pun">/</span><span class="kwd">no</span><span class="pun">)?</span><span class="kwd">no</span>
  8. <span class="typ">Do</span><span class="pln"> you want to configure the LXD bridge </span><span class="pun">(</span><span class="kwd">yes</span><span class="pun">/</span><span class="kwd">no</span><span class="pun">)?</span><span class="kwd">yes</span>
  9. <span class="pun">></span><span class="typ">You</span><span class="pln"> will be asked about the network bridge configuration</span><span class="pun">.</span><span class="typ">Accept</span><span class="pln"> all defaults </span><span class="kwd">and</span><span class="kwd">continue</span><span class="pun">.</span>
  10. <span class="typ">Warning</span><span class="pun">:</span><span class="typ">Stopping</span><span class="pln"> lxd</span><span class="pun">.</span><span class="pln">service</span><span class="pun">,</span><span class="pln"> but it can still be activated by</span><span class="pun">:</span>
  11. <span class="pln">lxd</span><span class="pun">.</span><span class="pln">socket</span>
  12. <span class="pln">LXD has been successfully configured</span><span class="pun">.</span>
  13. <span class="pln">$ _</span>

我们在一个(单独)的文件而不是块设备(即分区)中构建了一个文件系统来作为 ZFS 池,因此我们无需进行额外的分区操作。在本例中我指定了 30GB 大小,这个空间取之于根(/) 文件系统中。这个文件就是 /var/lib/lxd/zfs.img

行了!最初的配置完成了。若有问题,或者想了解其他信息,请阅读 https://www.stgraber.org/2016/03/15/lxd-2-0-installing-and-configuring-lxd-212/

 

创建第一个容器

所有 LXD 的管理操作都可以通过 lxc 命令来进行。我们通过给 lxc 不同参数来管理容器。

  1. <span class="pln">lxc </span><span class="kwd">list</span>

可以列出所有已经安装的容器。很明显,这个列表现在是空的,但这表示我们的安装是没问题的。

  1. <span class="pln">lxc image </span><span class="kwd">list</span>

列出可以用来启动容器的(已经缓存的)镜像列表。很明显这个列表也是空的,但这也说明我们的安装是没问题的。

  1. <span class="pln">lxc image </span><span class="kwd">list</span><span class="pln"> ubuntu</span><span class="pun">:</span>

列出可以下载并启动容器的远程镜像。而且指定了显示 Ubuntu 镜像。

  1. <span class="pln">lxc image </span><span class="kwd">list</span><span class="pln"> images</span><span class="pun">:</span>

列出可以用来启动容器的(已经缓存的)各种发行版的镜像列表。这会列出各种发行版的镜像比如 Alpine、Debian、Gentoo、OpenSUSE 以及 Fedora。

让我们启动一个 Ubuntu 16.04 容器,并称之为 c1

  1. <span class="pln">$ lxc launch ubuntu</span><span class="pun">:</span><span class="pln">x c1</span>
  2. <span class="typ">Creating</span><span class="pln"> c1</span>
  3. <span class="typ">Starting</span><span class="pln"> c1</span>
  4. <span class="pln">$ </span>

我们使用 launch 动作,然后选择镜像 ubuntu:xx 表示 Xenial/16.04 镜像),最后我们使用名字 c1 作为容器的名称。

让我们来看看安装好的首个容器,

  1. <span class="pln">$ lxc </span><span class="kwd">list</span>
  2. <span class="pun">+---------|---------|----------------------|------|------------|-----------+</span>
  3. <span class="pun">|</span><span class="pln"> NAME </span><span class="pun">|</span><span class="pln"> STATE </span><span class="pun">|</span><span class="pln"> IPV4 </span><span class="pun">|</span><span class="pln"> IPV6 </span><span class="pun">|</span><span class="pln"> TYPE </span><span class="pun">|</span><span class="pln"> SNAPSHOTS </span><span class="pun">|</span>
  4. <span class="pun">+---------|---------|----------------------|------|------------|-----------+</span>
  5. <span class="pun">|</span><span class="pln"> c1 </span><span class="pun">|</span><span class="pln"> RUNNING </span><span class="pun">|</span><span class="lit">10.173</span><span class="pun">.</span><span class="lit">82.158</span><span class="pun">(</span><span class="pln">eth0</span><span class="pun">)</span><span class="pun">|</span><span class="pun">|</span><span class="pln"> PERSISTENT </span><span class="pun">|</span><span class="lit">0</span><span class="pun">|</span>
  6. <span class="pun">+---------|---------|----------------------|------|------------|-----------+</span>

我们的首个容器 c1 已经运行起来了,它还有自己的 IP 地址(可以本地访问)。我们可以开始用它了!

 

安装 web 服务器

我们可以在容器中运行命令。运行命令的动作为 exec

  1. <span class="pln">$ lxc </span><span class="kwd">exec</span><span class="pln"> c1 </span><span class="pun">--</span><span class="kwd">uptime</span>
  2. <span class="lit">11</span><span class="pun">:</span><span class="lit">47</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> up </span><span class="lit">2</span><span class="pln"> min</span><span class="pun">,</span><span class="lit">0</span><span class="kwd">users</span><span class="pun">,</span><span class="pln">load average</span><span class="pun">:</span><span class="lit">0.07</span><span class="pun">,</span><span class="lit">0.05</span><span class="pun">,</span><span class="lit">0.04</span>
  3. <span class="pln">$ _</span>

exec 后面,我们指定容器、最后输入要在容器中运行的命令。该容器的运行时间只有 2 分钟,这是个新出炉的容器:-)。

命令行中的 -- 跟我们 shell 的参数处理过程有关。若我们的命令没有任何参数,则完全可以省略 -

  1. <span class="pln">$ lxc </span><span class="kwd">exec</span><span class="pln"> c1 </span><span class="pun">--</span><span class="kwd">df</span><span class="pun">-</span><span class="pln">h</span>

这是一个必须要 - 的例子,由于我们的命令使用了参数 -h。若省略了 -,会报错。

然后我们运行容器中的 shell 来更新包列表。

  1. <span class="pln">$ lxc </span><span class="kwd">exec</span><span class="pln"> c1 </span><span class="kwd">bash</span>
  2. <span class="pln">root@c1</span><span class="pun">:~#</span><span class="pln"> apt update</span>
  3. <span class="typ">Ign</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//archive.ubuntu.com trusty InRelease</span>
  4. <span class="typ">Get</span><span class="pun">:</span><span class="lit">1</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//archive.ubuntu.com trusty-updates InRelease [65.9 kB]</span>
  5. <span class="typ">Get</span><span class="pun">:</span><span class="lit">2</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//security.ubuntu.com trusty-security InRelease [65.9 kB]</span>
  6. <span class="pun">...</span>
  7. <span class="typ">Hit</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//archive.ubuntu.com trusty/universe Translation-en </span>
  8. <span class="typ">Fetched</span><span class="lit">11.2</span><span class="pln"> MB </span><span class="kwd">in</span><span class="lit">9s</span><span class="pun">(</span><span class="lit">1228</span><span class="pln"> kB</span><span class="pun">/</span><span class="pln">s</span><span class="pun">)</span>
  9. <span class="typ">Reading</span><span class="kwd">package</span><span class="pln"> lists</span><span class="pun">...</span><span class="typ">Done</span>
  10. <span class="pln">root@c1</span><span class="pun">:~#</span><span class="pln"> apt upgrade</span>
  11. <span class="typ">Reading</span><span class="kwd">package</span><span class="pln"> lists</span><span class="pun">...</span><span class="typ">Done</span>
  12. <span class="typ">Building</span><span class="pln"> dependency tree </span>
  13. <span class="pun">...</span>
  14. <span class="typ">Processing</span><span class="pln"> triggers </span><span class="kwd">for</span><span class="kwd">man</span><span class="pun">-</span><span class="pln">db </span><span class="pun">(</span><span class="lit">2.6</span><span class="pun">.</span><span class="lit">7.1</span><span class="pun">-</span><span class="lit">1ubuntu1</span><span class="pun">)</span><span class="pun">...</span>
  15. <span class="typ">Setting</span><span class="pln"> up dpkg </span><span class="pun">(</span><span class="lit">1.17</span><span class="pun">.</span><span class="lit">5ubuntu5.7</span><span class="pun">)</span><span class="pun">...</span>
  16. <span class="pln">root@c1</span><span class="pun">:~#</span><span class="pln"> _</span>

我们使用 nginx 来做 web 服务器。nginx 在某些方面要比 Apache web 服务器更酷一些。

  1. <span class="pln">root@c1</span><span class="pun">:~#</span><span class="pln"> apt install nginx</span>
  2. <span class="typ">Reading</span><span class="kwd">package</span><span class="pln"> lists</span><span class="pun">...</span><span class="typ">Done</span>
  3. <span class="typ">Building</span><span class="pln"> dependency tree</span>
  4. <span class="pun">...</span>
  5. <span class="typ">Setting</span><span class="pln"> up nginx</span><span class="pun">-</span><span class="pln">core </span><span class="pun">(</span><span class="lit">1.4</span><span class="pun">.</span><span class="lit">6</span><span class="pun">-</span><span class="lit">1ubuntu3.5</span><span class="pun">)</span><span class="pun">...</span>
  6. <span class="typ">Setting</span><span class="pln"> up nginx </span><span class="pun">(</span><span class="lit">1.4</span><span class="pun">.</span><span class="lit">6</span><span class="pun">-</span><span class="lit">1ubuntu3.5</span><span class="pun">)</span><span class="pun">...</span>
  7. <span class="typ">Processing</span><span class="pln"> triggers </span><span class="kwd">for</span><span class="pln"> libc</span><span class="pun">-</span><span class="pln">bin </span><span class="pun">(</span><span class="lit">2.19</span><span class="pun">-</span><span class="lit">0ubuntu6.9</span><span class="pun">)</span><span class="pun">...</span>
  8. <span class="pln">root@c1</span><span class="pun">:~#</span><span class="pln"> _</span>

让我们用浏览器访问一下这个 web 服务器。记住 IP 地址为 10.173.82.158,因此你需要在浏览器中输入这个 IP。

在 Ubuntu 上体验 LXD 容器

lxd-nginx

让我们对页面文字做一些小改动。回到容器中,进入默认 HTML 页面的目录中。

  1. <span class="pln">root@c1</span><span class="pun">:~#</span><span class="kwd">cd</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span>
  2. <span class="pln">root@c1</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="com">#</span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l</span>
  3. <span class="pln">total </span><span class="lit">2</span>
  4. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span><span class="lit">1</span><span class="pln"> root root </span><span class="lit">612</span><span class="typ">Jun</span><span class="lit">25</span><span class="lit">12</span><span class="pun">:</span><span class="lit">15</span><span class="pln"> index</span><span class="pun">.</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">debian</span><span class="pun">.</span><span class="pln">html</span>
  5. <span class="pln">root@c1</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="com">#</span>

使用 nano 编辑文件,然后保存:

在 Ubuntu 上体验 LXD 容器

lxd-nginx-nano

之后,再刷一下页面看看,

在 Ubuntu 上体验 LXD 容器

lxd-nginx-modified

 

清理

让我们清理一下这个容器,也就是删掉它。当需要的时候我们可以很方便地创建一个新容器出来。

  1. <span class="pln">$ lxc </span><span class="kwd">list</span>
  2. <span class="pun">+---------+---------+----------------------+------+------------+-----------+</span>
  3. <span class="pun">|</span><span class="pln"> NAME </span><span class="pun">|</span><span class="pln"> STATE </span><span class="pun">|</span><span class="pln"> IPV4 </span><span class="pun">|</span><span class="pln"> IPV6 </span><span class="pun">|</span><span class="pln"> TYPE </span><span class="pun">|</span><span class="pln"> SNAPSHOTS </span><span class="pun">|</span>
  4. <span class="pun">+---------+---------+----------------------+------+------------+-----------+</span>
  5. <span class="pun">|</span><span class="pln"> c1 </span><span class="pun">|</span><span class="pln"> RUNNING </span><span class="pun">|</span><span class="lit">10.173</span><span class="pun">.</span><span class="lit">82.169</span><span class="pun">(</span><span class="pln">eth0</span><span class="pun">)</span><span class="pun">|</span><span class="pun">|</span><span class="pln"> PERSISTENT </span><span class="pun">|</span><span class="lit">0</span><span class="pun">|</span>
  6. <span class="pun">+---------+---------+----------------------+------+------------+-----------+</span>
  7. <span class="pln">$ lxc stop c1</span>
  8. <span class="pln">$ lxc </span><span class="kwd">delete</span><span class="pln"> c1</span>
  9. <span class="pln">$ lxc </span><span class="kwd">list</span>
  10. <span class="pun">+---------+---------+----------------------+------+------------+-----------+</span>
  11. <span class="pun">|</span><span class="pln"> NAME </span><span class="pun">|</span><span class="pln"> STATE </span><span class="pun">|</span><span class="pln"> IPV4 </span><span class="pun">|</span><span class="pln"> IPV6 </span><span class="pun">|</span><span class="pln"> TYPE </span><span class="pun">|</span><span class="pln"> SNAPSHOTS </span><span class="pun">|</span>
  12. <span class="pun">+---------+---------+----------------------+------+------------+-----------+</span>
  13. <span class="pun">+---------+---------+----------------------+------+------------+-----------+</span>

我们停止(关闭)这个容器,然后删掉它了。

本文至此就结束了。关于容器有很多玩法。而这只是配置 Ubuntu 并尝试使用容器的第一步而已。


via: https://blog.simos.info/trying-out-lxd-containers-on-our-ubuntu/

作者:Simos Xenitellis 译者:lujun9972 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐