LXD 2.0 系列(八):LXD中的LXD
這是 LXD 2.0 系列介紹文章的第八篇。
介紹
在上一篇文章中,我介紹了如何在 LXD 中運行 Docker,這是一個訪問由 Docker 提供的應(yīng)用程序組合的很好方式,同時 Docker 還運行在 LXD 提供的安全環(huán)境中。
我提到的一個情況是為你的用戶提供一個 LXD 容器,然后讓他們使用他們的容器來運行 Docker。那么,如果他們自己想要在其容器中使用 LXD 運行其他 Linux 發(fā)行版,或者甚至允許另一組人來訪問運行在他們的容器中的 Linux 系統(tǒng)呢?
原來 LXD 使得用戶運行嵌套容器變得非常簡單。
嵌套 LXD
最簡單的情況可以使用 Ubuntu 16.04 鏡像來展示。 Ubuntu 16.04 云鏡像預(yù)裝了 LXD。守護(hù)進(jìn)程本身沒有運行,因為它是由套接字激活的,所以它不使用任何資源,直到你真正使用它。
讓我們啟動一個啟用了嵌套的 Ubuntu 16.04 容器:
- lxc launch ubuntu-daily:16.04 c1 -c security.nesting=true
你也可以在一個已有的容器上設(shè)置 security.nesting:
- lxc config set <container name> security.nesting true
或者對所有的容器使用一個指定的配置文件:
- lxc profile set <profile name> security.nesting true
容器啟動后,你可以從容器內(nèi)部得到一個 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:~#
就是這樣簡單。
在線演示服務(wù)器
因為這篇文章很短,我想我會花一點時間談?wù)撐覀冞\行中的演示服務(wù)器。我們今天早些時候剛剛達(dá)到了 10000 個會話!
這個服務(wù)器基本上只是一個運行在一個相當(dāng)強(qiáng)大的虛擬機(jī)上的正常的 LXD,一個小型的守護(hù)進(jìn)程實現(xiàn)了我們的網(wǎng)站所使用的 REST API。
當(dāng)你接受服務(wù)條款時,將為你創(chuàng)建一個新的 LXD 容器,并啟用 security.nesting,如上所述。接著你就像使用 lxc exec 時一樣連接到了那個容器,除了我們使用 websockets 和 javascript 來做這些。
你在此環(huán)境中創(chuàng)建的容器都是嵌套的 LXD 容器。如果你想,你可以進(jìn)一步地嵌套。
我們?nèi)秶厥褂昧?LXD 資源限制,以防止一個用戶的行為影響其他用戶,并仔細(xì)監(jiān)控服務(wù)器的任何濫用跡象。
如果你想運行自己的類似的服務(wù)器,你可以獲取我們的網(wǎng)站和守護(hù)進(jìn)程的代碼:
- git clone https://github.com/lxc/linuxcontainers.org
- git clone https://github.com/lxc/lxd-demo-server
額外信息
LXD 的主站在: https://linuxcontainers.org/lxd
LXD 的 GitHub 倉庫: https://github.com/lxc/lxd
LXD 的郵件列表: https://lists.linuxcontainers.org
LXD 的 IRC 頻道: #lxcontainers on irc.freenode.net