我提到的一個情況是為你的使用者提供一個 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 <container name> security.nesting true</code>
或者對所有的容器使用一個指定的配置檔案:
<code>lxc profile set <profile name> 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中國”