LXD 2.0 係列(八):LXD 中的 LXD
介紹
在上一篇文章中,我介紹了如何在 LXD 中運行 Docker,這是一個訪問由 Docker 提供的應用程序組合的很好方式,同時 Docker 還運行在 LXD 提供的安全環境中。
我提到的一個情況是為你的用戶提供一個 LXD 容器,然後讓他們使用他們的容器來運行 Docker。那麼,如果他們自己想要在其容器中使用 LXD 運行其他 Linux 發行版,或者甚至允許另一組人來訪問運行在他們的容器中的 Linux 係統呢?
原來 LXD 使得用戶運行嵌套容器變得非常簡單。
嵌套 LXD
最簡單的情況可以使用 Ubuntu 16.04 鏡像來展示。 Ubuntu 16.04 雲鏡像預裝了 LXD。守護進程本身沒有運行,因為它是由套接字激活的,所以它不使用任何資源,直到你真正使用它。
讓我們啟動一個啟用了嵌套的 Ubuntu 16.04 容器:
lxc launch ubuntu-daily:16.04 c1 -c security.nesting=true
你也可以在一個已有的容器上設置 security.nesting
:
lxc config set <container name> security.nesting true
或者對所有的容器使用一個指定的配置文件:
lxc profile set <profile name> security.nesting true
容器啟動後,你可以從容器內部得到一個 shell,配置 LXD 並生成一個容器:
stgraber@dakara:~$ lxc launch ubuntu-daily:16.04 c1 -c security.nesting=true
Creating c1
Starting c1
stgraber@dakara:~$ lxc exec c1 bash
root@c1:~# lxd init
Name of the storage backend to use (dir or zfs): dir
We detected that you are running inside an unprivileged container.
This means that unless you manually configured your host otherwise,
you will not have enough uid and gid to allocate to your containers.
LXD can re-use your container's own allocation to avoid the problem.
Doing so makes your nested containers slightly less safe as they could
in theory attack their parent container and gain more privileges than
they otherwise would.
Would you like to have your containers share their parent's allocation (yes/no)? yes
Would you like LXD to be available over the network (yes/no)? no
Do you want to configure the LXD bridge (yes/no)? yes
Warning: Stopping lxd.service, but it can still be activated by:
lxd.socket
LXD has been successfully configured.
root@c1:~# lxc launch ubuntu:14.04 trusty
Generating a client certificate. This may take a minute...
If this is your first time using LXD, you should also run: sudo lxd init
Creating trusty
Retrieving image: 100%
Starting trusty
root@c1:~# lxc list
+--------+---------+-----------------------+----------------------------------------------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------+---------+-----------------------+----------------------------------------------+------------+-----------+
| trusty | RUNNING | 10.153.141.124 (eth0) | fd7:f15d:d1d6:da14:216:3eff:fef1:4002 (eth0) | PERSISTENT | 0 |
+--------+---------+-----------------------+----------------------------------------------+------------+-----------+
root@c1:~#
就是這樣簡單。
在線演示服務器
因為這篇文章很短,我想我會花一點時間談論我們運行中的演示服務器。我們今天早些時候剛剛達到了 10000 個會話!
這個服務器基本上隻是一個運行在一個相當強大的虛擬機上的正常的 LXD,一個小型的守護進程實現了我們的網站所使用的 REST API。
當你接受服務條款時,將為你創建一個新的 LXD 容器,並啟用 security.nesting
,如上所述。接著你就像使用 lxc exec
時一樣連接到了那個容器,除了我們使用 websockets 和 javascript 來做這些。
你在此環境中創建的容器都是嵌套的 LXD 容器。如果你想,你可以進一步地嵌套。
我們全範圍地使用了 LXD 資源限製,以防止一個用戶的行為影響其他用戶,並仔細監控服務器的任何濫用跡象。
原文發布時間為:2017-03-02
本文來自雲棲社區合作夥伴“Linux中國”
最後更新:2017-05-25 17:02:02