天天看点

LXD 2.0 系列(八):LXD 中的 LXD

我提到的一个情况是为你的用户提供一个 lxd 容器,然后让他们使用他们的容器来运行 docker。那么,如果他们自己想要在其容器中使用 lxd 运行其他 linux 发行版,或者甚至允许另一组人来访问运行在他们的容器中的 linux 系统呢?

原来 lxd 使得用户运行嵌套容器变得非常简单。

<a target="_blank"></a>

最简单的情况可以使用 ubuntu 16.04 镜像来展示。 ubuntu 16.04 云镜像预装了 lxd。守护进程本身没有运行,因为它是由套接字激活的,所以它不使用任何资源,直到你真正使用它。

让我们启动一个启用了嵌套的 ubuntu 16.04 容器:

<code>lxc launch ubuntu-daily:16.04 c1 -c security.nesting=true</code>

你也可以在一个已有的容器上设置 <code>security.nesting</code>:

<code>lxc config set &lt;container name&gt; security.nesting true</code>

或者对所有的容器使用一个指定的配置文件:

<code>lxc profile set &lt;profile name&gt; security.nesting true</code>

容器启动后,你可以从容器内部得到一个 shell,配置 lxd 并生成一个容器:

<code>stgraber@dakara:~$ lxc launch ubuntu-daily:16.04 c1 -c security.nesting=true</code>

<code>creating c1</code>

<code>starting c1</code>

<code></code>

<code>stgraber@dakara:~$ lxc exec c1 bash</code>

<code>root@c1:~# lxd init</code>

<code>name of the storage backend to use (dir or zfs): dir</code>

<code>we detected that you are running inside an unprivileged container.</code>

<code>this means that unless you manually configured your host otherwise,</code>

<code>you will not have enough uid and gid to allocate to your containers.</code>

<code>lxd can re-use your container's own allocation to avoid the problem.</code>

<code>doing so makes your nested containers slightly less safe as they could</code>

<code>in theory attack their parent container and gain more privileges than</code>

<code>they otherwise would.</code>

<code>would you like to have your containers share their parent's allocation (yes/no)? yes</code>

<code>would you like lxd to be available over the network (yes/no)? no</code>

<code>do you want to configure the lxd bridge (yes/no)? yes</code>

<code>warning: stopping lxd.service, but it can still be activated by:</code>

<code>lxd.socket</code>

<code>lxd has been successfully configured.</code>

<code>root@c1:~# lxc launch ubuntu:14.04 trusty</code>

<code>generating a client certificate. this may take a minute...</code>

<code>if this is your first time using lxd, you should also run: sudo lxd init</code>

<code>creating trusty</code>

<code>retrieving image: 100%</code>

<code>starting trusty</code>

<code>root@c1:~# lxc list</code>

<code>+--------+---------+-----------------------+----------------------------------------------+------------+-----------+</code>

<code>| name | state | ipv4 | ipv6 | type | snapshots |</code>

<code>| trusty | running | 10.153.141.124 (eth0) | fd7:f15d:d1d6:da14:216:3eff:fef1:4002 (eth0) | persistent | 0 |</code>

<code>root@c1:~#</code>

就是这样简单。

这个服务器基本上只是一个运行在一个相当强大的虚拟机上的正常的 lxd,一个小型的守护进程实现了我们的网站所使用的 rest api。

当你接受服务条款时,将为你创建一个新的 lxd 容器,并启用 <code>security.nesting</code>,如上所述。接着你就像使用 <code>lxc exec</code> 时一样连接到了那个容器,除了我们使用 websockets 和 javascript 来做这些。

你在此环境中创建的容器都是嵌套的 lxd 容器。如果你想,你可以进一步地嵌套。

原文发布时间为:2017-03-02

本文来自云栖社区合作伙伴“linux中国”