在 Ubuntu 上体验 LXD 容器
LXD LXC 容器
![](/data/attachment/album/201801/18/113902apgo7es2ocs2fgtz.jpg)


本文的主角是容器，一种类似虚拟机但更轻量级的构造。你可以轻易地在你的 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 系列（二）：安装与配置](/article-7687-1.html) 来进行一些操作，然后再回来。


确保已经更新了包列表：



```
sudo apt update
sudo apt upgrade

```

安装 `lxd` 包：



```
sudo apt install lxd

```

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


安装 `zfsutils-linux` 包（若你安装的是 Ubuntu 16.04.x）：



```
sudo apt install zfsutils-linux

```

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


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


运行下面命令：



```
$ sudo lxd init
Name of the storage backend to use (dir or zfs): zfs
Create a new ZFS pool (yes/no)? yes
Name of the new ZFS pool: lxd-pool
Would you like to use an existing block device (yes/no)? no
Size in GB of the new loop device (1GB minimum): 30
Would you like LXD to be available over the network (yes/no)? no
Do you want to configure the LXD bridge (yes/no)? yes 
> You will be asked about the network bridge configuration. Accept all defaults and continue.
Warning: Stopping lxd.service, but it can still be activated by:
 lxd.socket
 LXD has been successfully configured.
$ _

```

我们在一个（单独）的文件而不是块设备（即分区）中构建了一个文件系统来作为 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` 不同参数来管理容器。



```
lxc list

```

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



```
lxc image list

```

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



```
lxc image list ubuntu：

```

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



```
lxc image list images：

```

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


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



```
$ lxc launch ubuntu：x c1
Creating c1
Starting c1
$ 

```

我们使用 `launch` 动作，然后选择镜像 `ubuntu：x` （`x` 表示 Xenial/16.04 镜像），最后我们使用名字 `c1` 作为容器的名称。


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



```
$ lxc list

+---------|---------|----------------------|------|------------|-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+---------|---------|----------------------|------|------------|-----------+
| c1 | RUNNING | 10.173.82.158 (eth0) | | PERSISTENT | 0 |
+---------|---------|----------------------|------|------------|-----------+

```

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


### 安装 web 服务器


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



```
$ lxc exec c1 -- uptime
 11：47：25 up 2 min，0 users，load average：0.07，0.05，0.04
$ _

```

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


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



```
$ lxc exec c1 -- df -h

```

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


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



```
$ lxc exec c1 bash
root@c1:~# apt update
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
...
Hit http://archive.ubuntu.com trusty/universe Translation-en 
Fetched 11.2 MB in 9s (1228 kB/s) 
Reading package lists... Done
root@c1:~# apt upgrade
Reading package lists... Done
Building dependency tree 
...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up dpkg (1.17.5ubuntu5.7) ...
root@c1:~# _

```

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



```
root@c1:~# apt install nginx
Reading package lists... Done
Building dependency tree
...
Setting up nginx-core (1.4.6-1ubuntu3.5) ...
Setting up nginx (1.4.6-1ubuntu3.5) ...
Processing triggers for libc-bin (2.19-0ubuntu6.9) ...
root@c1:~# _

```

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


[![lxd-nginx](/data/attachment/album/201801/18/113912hc9pa3agdypmc0cg.png)](https://i2.wp.com/blog.simos.info/wp-content/uploads/2016/06/lxd-nginx.png?ssl=1)


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



```
root@c1:~# cd /var/www/html/
root@c1:/var/www/html# ls -l
total 2
-rw-r--r-- 1 root root 612 Jun 25 12:15 index.nginx-debian.html
root@c1:/var/www/html#

```

使用 nano 编辑文件，然后保存：


[![lxd-nginx-nano](/data/attachment/album/201801/18/113915j4awrpi4yxgvdew4.png)](https://i2.wp.com/blog.simos.info/wp-content/uploads/2016/06/lxd-nginx-nano.png?ssl=1)


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


[![lxd-nginx-modified](/data/attachment/album/201801/18/113920uoxiic6zluz6vofo.png)](https://i1.wp.com/blog.simos.info/wp-content/uploads/2016/06/lxd-nginx-modified.png?ssl=1)


### 清理


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



```
$ lxc list
+---------+---------+----------------------+------+------------+-----------+
| NAME | STATE   | IPV4                 | IPV6 | TYPE       | SNAPSHOTS    |
+---------+---------+----------------------+------+------------+-----------+
| c1   | RUNNING | 10.173.82.169 (eth0) |      | PERSISTENT | 0            |
+---------+---------+----------------------+------+------------+-----------+
$ lxc stop c1
$ lxc delete c1
$ lxc list
+---------+---------+----------------------+------+------------+-----------+
| NAME | STATE   | IPV4                 | IPV6 | TYPE       | SNAPSHOTS    |
+---------+---------+----------------------+------+------------+-----------+
+---------+---------+----------------------+------+------------+-----------+

```

我们停止（关闭）这个容器，然后删掉它了。


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




---


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


作者：[Simos Xenitellis](https://blog.simos.info/author/simos/) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
This post is about *containers*, a construct similar to *virtual machines (VM)* but so much lightweight that you can easily create a dozen on your desktop Ubuntu!

A VM virtualizes a whole computer and then you install in there the guest operating system. **In contrast**, a *container* **reuses** the host Linux kernel and simply **contains** just the root filesystem (aka runtimes) of our choice. The Linux kernel has several features that rigidly separate the running Linux container from our host computer (i.e. our desktop Ubuntu).

By themselves, Linux containers would need some manual work to manage them directly. Fortunately, there is LXD (pronounced Lex-deeh), a service that manages Linux containers for us.

We will see how to

- setup our Ubuntu desktop for containers,
- create a container,
- install a Web server,
- test it a bit, and
- clear everything up.

## Set up your Ubuntu for containers

If you have Ubuntu 16.04, then you are ready to go. Just install a couple of extra packages that we see below. If you have Ubuntu 14.04.x or Ubuntu 15.10, see [LXD 2.0: Installing and configuring LXD [2/12]](https://www.stgraber.org/2016/03/15/lxd-2-0-installing-and-configuring-lxd-212/) for some extra steps, then come back.

Make sure the package list is up-to-date:

sudo apt update sudo apt upgrade

Install the **lxd** package:

sudo apt install lxd

If you have Ubuntu 16.04, you can enable the feature to store your container files in a ZFS filesystem. The Linux kernel in Ubuntu 16.04 includes the necessary kernel modules for ZFS. For LXD to use ZFS for storage, we just need to install a package with ZFS utilities. Without ZFS, the containers would be stored as separate files on the host filesystem. With ZFS, we have features like *copy-on-write* which makes the tasks much faster.

Install the **zfsutils-linux** package (if you have Ubuntu 16.04.x):

sudo apt install zfsutils-linux

Once you installed the LXD package on the desktop Ubuntu, the package installation scripts should have added you to the **lxd** group. If your desktop account is a member of that group, then your account can manage containers with LXD and can avoid adding *sudo* in front of all commands. The way Linux works, **you would need to log out from the desktop session and then log in again** to activate the **lxd** group membership. (If you are an advanced user, you can avoid the re-login by *newgrp lxd *in your current shell).

Before use, LXD should be initialized with our storage choice and networking choice.

Initialize **lxd** for storage and networking by running the following command:

$sudo lxd initName of the storage backend to use (dir or zfs):zfsCreate a new ZFS pool (yes/no)?yesName of the new ZFS pool:lxd-poolWould you like to use an existing block device (yes/no)?noSize in GB of the new loop device (1GB minimum):30Would you like LXD to be available over the network (yes/no)?noDo you want to configure the LXD bridge (yes/no)?yes> You will be asked about the network bridge configuration. Accept all defaults and continue.Warning: Stopping lxd.service, but it can still be activated by:lxd.socketLXD has been successfully configured. $ _

We created the *ZFS pool* as a filesystem inside a (single) file, not a block device (i.e. in a partition), thus no need for extra partitioning. In the example I specified 30GB, and this space will come from the root (/) filesystem. If you want to look at this file, it is at /var/lib/lxd/zfs.img.


That’s it! The initial configuration has been completed. For troubleshooting or background information, see https://www.stgraber.org/2016/03/15/lxd-2-0-installing-and-configuring-lxd-212/

## Create your first container

All management commands with LXD are available through the **lxc** command. We run **lxc** with some parameters and that’s how we manage containers.

lxc list

to get a list of installed containers. Obviously, the list will be empty but it verifies that all are fine.

lxc image list

shows the list of (cached) images that we can use to launch a container. Obviously, the list will be empty but it verifies that all are fine.

lxc image list ubuntu:

shows the list of available remote images that we can use to download and launch as containers. This specific list shows Ubuntu images.

lxc image list images:

shows the list of available remote images for various distributions that we can use to download and launch as containers. This specific list shows all sort of distributions like Alpine, Debian, Gentoo, Opensuse and Fedora.

Let’s launch a container with Ubuntu 16.04 and call it *c1*:

$ lxc launch ubuntu:x c1 Creating c1 Starting c1 $ _

We used the *launch* action, then selected the image ** ubuntu:x** (x is an alias for the Xenial/16.04 image) and lastly we use the name

*c1*for our container.

Let’s view our first installed container,

$ lxc list +---------+---------+----------------------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +---------+---------+----------------------+------+------------+-----------+ | c1 | RUNNING | 10.173.82.158 (eth0) | | PERSISTENT | 0 | +---------+---------+----------------------+------+------------+-----------+

Our first container c1 is running and it has an IP address (accessible locally). It is ready to be used!

## Install a Web server

We can run commands in our container. The action for running commands, is **exec**.

$ lxc exec c1 -- uptime 11:47:25 up 2 min, 0 users, load average: 0.07, 0.05, 0.04 $ _

After the action exec, we specify the container and finally we type command to run inside the container. The uptime is just 2 minutes, it’s a fresh container :-).

The — thing on the command line has to do with parameter processing of our shell. If our command does not have any parameters, we can safely omit the –.

$ lxc exec c1 -- df -h

This is an example that requires the –, because for our command we use the parameter -h. If you omit the –, you get an error.

Let’s get a shell in the container, and update the package list already.

$ lxc exec c1 bash root@c1:~#apt updateIgn http://archive.ubuntu.com trusty InRelease Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB] Get:2 http://security.ubuntu.com trusty-security InRelease [65.9 kB] ... Hit http://archive.ubuntu.com trusty/universe Translation-en Fetched 11.2 MB in 9s (1228 kB/s) Reading package lists... Done root@c1:~#apt upgradeReading package lists... Done Building dependency tree ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Setting up dpkg (1.17.5ubuntu5.7) ... root@c1:~# _

We are going to install **nginx** as our Web server. nginx is somewhat cooler than Apache Web server.

root@c1:~# apt install nginx Reading package lists... Done Building dependency tree ... Setting up nginx-core (1.4.6-1ubuntu3.5) ... Setting up nginx (1.4.6-1ubuntu3.5) ... Processing triggers for libc-bin (2.19-0ubuntu6.9) ... root@c1:~# _

Let’s view our Web server with our browser. Remeber the IP address you got 10.173.82.158, so I enter it into my browser.

Let’s make a small change in the text of that page. Back inside our container, we enter the directory with the default HTML page.

root@c1:~#cd /var/www/html/root@c1:/var/www/html#ls -ltotal 2 -rw-r--r-- 1 root root 612 Jun 25 12:15 index.nginx-debian.html root@c1:/var/www/html#

We can edit the file with nano, then save

Finally, let’s check the page again,

## Clearing up

Let’s clear up the container by deleting it. We can easily create new ones when we need them.

$lxc list+---------+---------+----------------------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +---------+---------+----------------------+------+------------+-----------+ | c1 | RUNNING | 10.173.82.169 (eth0) | | PERSISTENT | 0 | +---------+---------+----------------------+------+------------+-----------+ $lxc stop c1$lxc delete c1$lxc list+---------+---------+----------------------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +---------+---------+----------------------+------+------------+-----------+ +---------+---------+----------------------+------+------------+-----------+

We stopped (shutdown) the container, then we deleted it.

That’s all. There are many more ideas on what do with containers. Here are the first steps on setting up our Ubuntu desktop and trying out one such container.

## 12 pings

Skip to comment form

[…] can have LXD containers on your home computer, you can also have them on your Virtual-Private Server (VPS). If you have any further questions on […]

[…] have set up LXD on either our personal computer or on the cloud (like DigitalOcean and Scaleway). Actually, we can even try LXD online for free at […]

[…] previous posts, we saw how to configure LXD/LXC containers on a VPS on DigitalOcean and Scaleway. There are many more VPS […]

[…] Trying out LXD containers on our Ubuntu and set up LXD on your system. Then, come back here and try the following […]

[…] Trying out LXD containers on our Ubuntu in order to make the initial (one-time) configuration of LXD on your Ubuntu […]

[…] you first configure LXD, you need to make important decisions. Decisions that relate to where you are storing the […]

[…] Let’s create the new container for LXD. If this is the first time you use LXD, have a look at Trying out LXD containers on our Ubuntu. […]

[…] game. For best results, select ZFS as the storage backend, and place the space on an SSD disk. Also Trying out LXD containers on our Ubuntu may […]

[…] see how to setup LXD on your Ubuntu desktop. There are extra resources if you want to install LXD on other […]

[…] https://blog.simos.info/trying-out-lxd-containers-on-our-ubuntu/ on how to set up and test LXD on Ubuntu (or another Linux […]

[…] are going to compile and install retdec in an LXD container. In this way, it will not mess up our Linux installation with additional packages and installed […]

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