diff --git a/translated/tech/20180103 How to preconfigure LXD containers with cloud-init.md b/published/20180103 How to preconfigure LXD containers with cloud-init.md similarity index 59% rename from translated/tech/20180103 How to preconfigure LXD containers with cloud-init.md rename to published/20180103 How to preconfigure LXD containers with cloud-init.md index 919efe4a26..4e70cd3bcb 100644 --- a/translated/tech/20180103 How to preconfigure LXD containers with cloud-init.md +++ b/published/20180103 How to preconfigure LXD containers with cloud-init.md @@ -1,12 +1,15 @@ -如何使用cloud-init来预配置LXD容器 +如何使用 cloud-init 来预配置 LXD 容器 ====== -当你正在创建LXD容器的时候,你希望它们能被预先配置好。例如在容器一启动就自动执行 **apt update**来安装一些软件包,或者运行一些命令。 -这篇文章将讲述如何用[**cloud-init**][1]来对[LXD容器进行进行早期初始化][2]。 + +当你正在创建 LXD 容器的时候,你希望它们能被预先配置好。例如在容器一启动就自动执行 `apt update`来安装一些软件包,或者运行一些命令。 + +这篇文章将讲述如何用 [cloud-init][1] 来对 [LXD 容器进行进行早期初始化][2]。 + 接下来,我们将创建一个包含cloud-init指令的LXD profile,然后启动一个新的容器来使用这个profile。 -### 如何创建一个新的LXD profile +### 如何创建一个新的 LXD profile -查看已经存在的profile: +查看已经存在的 profile: ```shell $ lxc profile list @@ -17,7 +20,7 @@ $ lxc profile list +---------|---------+ ``` -我们把名叫default的profile复制一份,然后在其内添加新的指令: +我们把名叫 `default` 的 profile 复制一份,然后在其内添加新的指令: ```shell $ lxc profile copy default devprofile @@ -32,7 +35,7 @@ $ lxc profile list +------------|---------+ ``` -我们就得到了一个新的profile: **devprofile**。下面是它的详情: +我们就得到了一个新的 profile: `devprofile`。下面是它的详情: ```yaml $ lxc profile show devprofile @@ -52,11 +55,12 @@ name: devprofile used_by: [] ``` -注意这几个部分: **config:** , **description:** , **devices:** , **name:** 和 **used_by:**,当你修改这些内容的时候注意不要搞错缩进。(译者注:因为这些内容是YAML格式的,缩进是语法的一部分) +注意这几个部分: `config:` 、 `description:` 、 `devices:` 、 `name:` 和 `used_by:`,当你修改这些内容的时候注意不要搞错缩进。(LCTT 译注:因为这些内容是 YAML 格式的,缩进是语法的一部分) -### 如何把cloud-init添加到LXD profile里 +### 如何把 cloud-init 添加到 LXD profile 里 + +[cloud-init][1] 可以添加到 LXD profile 的 `config` 里。当这些指令将被传递给容器后,会在容器第一次启动的时候执行。 -[cloud-init][1]可以添加到LXD profile的 **config** 里。当这些指令将被传递给容器后,会在容器第一次启动的时候执行。 下面是用在示例中的指令: ```yaml @@ -69,11 +73,9 @@ used_by: [] - [touch, /tmp/simos_was_here] ``` -**package_upgrade: true** 是指当容器第一次被启动时,我们想要**cloud-init** 运行 **sudo apt upgrade**。 -**packages:** 列出了我们想要自动安装的软件。然后我们设置了**locale** and **timezone**。在Ubuntu容器的镜像里,root用户默认的 locale 是**C.UTF-8**,而**ubuntu** 用户则是 **en_US.UTF-8**。此外,我们把时区设置为**Etc/UTC**。 -最后,我们展示了[如何使用**runcmd**来运行一个Unix命令][3]。 +`package_upgrade: true` 是指当容器第一次被启动时,我们想要 `cloud-init` 运行 `sudo apt upgrade`。`packages:` 列出了我们想要自动安装的软件。然后我们设置了 `locale` 和 `timezone`。在 Ubuntu 容器的镜像里,root 用户默认的 `locale` 是 `C.UTF-8`,而 `ubuntu` 用户则是 `en_US.UTF-8`。此外,我们把时区设置为 `Etc/UTC`。最后,我们展示了[如何使用 runcmd 来运行一个 Unix 命令][3]。 -我们需要关注如何将**cloud-init**指令插入LXD profile。 +我们需要关注如何将 `cloud-init` 指令插入 LXD profile。 我首选的方法是: @@ -110,15 +112,15 @@ name: devprofile used_by: [] ``` -### 如何使用LXD profile启动一个容器 +### 如何使用 LXD profile 启动一个容器 -使用profile **devprofile**来启动一个新容器: +使用 profile `devprofile` 来启动一个新容器: ``` $ lxc launch --profile devprofile ubuntu:x mydev ``` -然后访问该容器来查看我们的的指令是否生效: +然后访问该容器来查看我们的指令是否生效: ```shell $ lxc exec mydev bash @@ -139,7 +141,7 @@ root@mydev:~# ps ax root@mydev:~# ``` -如果我们连接得够快,通过**ps ax**将能够看到系统正在更新软件。我们可以从/var/log/cloud-init-output.log看到完整的日志: +如果我们连接得够快,通过 `ps ax` 将能够看到系统正在更新软件。我们可以从 `/var/log/cloud-init-output.log` 看到完整的日志: ``` Generating locales (this might take a while)... @@ -147,7 +149,7 @@ Generating locales (this might take a while)... Generation complete. ``` -以上可以看出locale已经被更改了。root 用户还是保持默认的**C.UTF-8**,只有非root用户**ubuntu**使用了新的locale。 +以上可以看出 `locale` 已经被更改了。root 用户还是保持默认的 `C.UTF-8`,只有非 root 用户 `ubuntu` 使用了新的`locale` 设置。 ``` Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease @@ -155,7 +157,7 @@ Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB] Get:3 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB] ``` -以上是安装软件包之前执行的**apt update**。 +以上是安装软件包之前执行的 `apt update`。 ``` The following packages will be upgraded: @@ -163,16 +165,18 @@ The following packages will be upgraded: 4 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 211 kB of archives. ``` -以上是在执行**package_upgrade: true**和安装软件包。 + +以上是在执行 `package_upgrade: true` 和安装软件包。 ``` The following NEW packages will be installed: binutils build-essential cpp cpp-5 dpkg-dev fakeroot g++ g++-5 gcc gcc-5 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl ``` -以上是我们安装**build-essential**软件包的指令。 -**runcmd** 执行的结果如何? +以上是我们安装 `build-essential` 软件包的指令。 + +`runcmd` 执行的结果如何? ``` root@mydev:~# ls -l /tmp/ @@ -185,7 +189,7 @@ root@mydev:~# ### 结论 -当我们启动LXD容器的时候,我们常常需要默认启用一些配置,并且希望能够避免重复工作。通常解决这个问题的方法是创建LXD profile,然后把需要的配置添加进去。最后,当我们启动新的容器时,只需要应用该LXD profile即可。 +当我们启动 LXD 容器的时候,我们常常需要默认启用一些配置,并且希望能够避免重复工作。通常解决这个问题的方法是创建 LXD profile,然后把需要的配置添加进去。最后,当我们启动新的容器时,只需要应用该 LXD profile 即可。 -------------------------------------------------------------------------------- @@ -193,7 +197,7 @@ via: https://blog.simos.info/how-to-preconfigure-lxd-containers-with-cloud-init/ 作者:[Simos Xenitellis][a] 译者:[kaneg](https://github.com/kaneg) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出