首页   

如何使用 cloud-init 来预配置 LXD 容器 | Linux 中国

Linux中国  · linux  · 6 年前
当你正在创建 LXD 容器的时候,你希望它们能被预先配置好。例如在容器一启动就自动执行 apt update来安装一些软件包,或者运行一些命令。
-- Simos Xenitellis

致谢
编译自 | https://blog.simos.info/how-to-preconfigure-lxd-containers-with-cloud-init/ 
 作者 | Simos Xenitellis
 译者 | Kane Gong (kaneg) 🌟 共计翻译:1 篇 贡献时间:2 天

当你正在创建 LXD 容器的时候,你希望它们能被预先配置好。例如在容器一启动就自动执行 apt update来安装一些软件包,或者运行一些命令。

这篇文章将讲述如何用 cloud-init[1] 来对 LXD 容器进行进行早期初始化[2]

接下来,我们将创建一个包含cloud-init指令的LXD profile,然后启动一个新的容器来使用这个profile。

如何创建一个新的 LXD profile

查看已经存在的 profile:

  1. $ lxc profile list

  2. +---------|---------+

  3. | NAME    | USED BY |

  4. +---------|---------+

  5. | default | 11      |

  6. +---------|---------+

我们把名叫 default 的 profile 复制一份,然后在其内添加新的指令:

  1. $ lxc profile copy default devprofile

  2. $ lxc profile list

  3. +------------|---------+

  4. | NAME       | USED BY |

  5. +------------|---------+

  6. | default    | 11      |

  7. +------------|---------+

  8. | devprofile | 0       |

  9. +------------|---------+

我们就得到了一个新的 profile: devprofile。下面是它的详情:

  1. $ lxc profile show devprofile

  2. config:

  3. environment.TZ: ""

  4. description: Default LXD profile

  5. devices:

  6. eth0:

  7. nictype: bridged

  8. parent: lxdbr0

  9. type: nic

  10. root:

  11. path: /

  12. pool: default

  13. type: disk

  14. name: devprofile

  15. used_by: []

注意这几个部分: config: 、 description: 、 devices: 、 name: 和 used_by:,当你修改这些内容的时候注意不要搞错缩进。(LCTT 译注:因为这些内容是 YAML 格式的,缩进是语法的一部分)

如何把 cloud-init 添加到 LXD profile 里

cloud-init[1] 可以添加到 LXD profile 的 config 里。当这些指令将被传递给容器后,会在容器第一次启动的时候执行。

下面是用在示例中的指令:

  1. package_upgrade: true

  2. packages:

  3. - build-essential

  4. locale: es_ES.UTF-8

  5. timezone: Europe/Madrid

  6. runcmd:

  7. - [touch, /tmp/simos_was_here]

package_upgrade: true 是指当容器第一次被启动时,我们想要 cloud-init 运行 sudo apt upgradepackages: 列出了我们想要自动安装的软件。然后我们设置了 locale 和 timezone。在 Ubuntu 容器的镜像里,root 用户默认的 locale 是 C.UTF-8,而 ubuntu 用户则是 en_US.UTF-8。此外,我们把时区设置为 Etc/UTC。最后,我们展示了如何使用 runcmd 来运行一个 Unix 命令[3]

我们需要关注如何将 cloud-init 指令插入 LXD profile。

我首选的方法是:

  1. $ lxc profile edit devprofile

它会打开一个文本编辑器,以便你将指令粘贴进去。结果应该是这样的[4]

  1. $ lxc profile show devprofile

  2. config:

  3.  environment.TZ: ""

  4.  user.user-data: |

  5.    #cloud-config

  6.    package_upgrade: true

  7.    packages:

  8.      - build-essential

  9.    locale: es_ES.UTF-8

  10.    timezone: Europe/Madrid

  11.    runcmd:

  12.      - [touch, /tmp/simos_was_here]

  13. description: Default LXD profile

  14. devices:

  15.  eth0:

  16.    nictype: bridged

  17.    parent: lxdbr0

  18.    type: nic

  19.  root:

  20.    path: /

  21.    pool: default

  22.    type: disk

  23. name: devprofile

  24. used_by: []

如何使用 LXD profile 启动一个容器

使用 profile devprofile 来启动一个新容器:

  1. $ lxc launch --profile devprofile ubuntu:x mydev

然后访问该容器来查看我们的指令是否生效:

  1. $ lxc exec mydev bash

  2. root@mydev:~# ps ax

  3. PID TTY STAT TIME COMMAND

  4. 1 ? Ss 0:00 /sbin/init

  5. ...

  6. 427 ? Ss 0:00 /usr/bin/python3 /usr/bin/cloud-init modules --mode=f

  7. 430 ? S 0:00 /bin/sh -c tee -a /var/log/cloud-init-output.log

  8. 431 ? S 0:00 tee -a /var/log/cloud-init-output.log

  9. 432 ? S 0:00 /usr/bin/apt-get --option=Dpkg::Options::=--force-con

  10. 437 ? S 0:00 /usr/lib/apt/methods/http

  11. 438 ? S 0:00 /usr/lib/apt/methods/http

  12. 440 ? S 0:00 /usr/lib/apt/methods/gpgv

  13. 570 ? Ss 0:00 bash

  14. 624 ? S 0:00 /usr/lib/apt/methods/store

  15. 625 ? R+ 0:00 ps ax

  16. root@mydev:~#

如果我们连接得够快,通过 ps ax 将能够看到系统正在更新软件。我们可以从 /var/log/cloud-init-output.log 看到完整的日志:

  1. Generating locales (this might take a while)...

  2. es_ES.UTF-8... done

  3. Generation complete.

以上可以看出 locale 已经被更改了。root 用户还是保持默认的 C.UTF-8,只有非 root 用户 ubuntu 使用了新的locale 设置。

  1. Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease

  2. Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]

  3. Get:3 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]

以上是安装软件包之前执行的 apt update

  1. The following packages will be upgraded:

  2. libdrm2 libseccomp2 squashfs-tools unattended-upgrades

  3. 4 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.

  4. Need to get 211 kB of archives.

以上是在执行 package_upgrade: true 和安装软件包。

  1. The following NEW packages will be installed:

  2. binutils build-essential cpp cpp-5 dpkg-dev fakeroot g++ g++-5 gcc gcc-5

  3. libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl

以上是我们安装 build-essential 软件包的指令。

runcmd 执行的结果如何?

  1. root@mydev:~# ls -l /tmp/

  2. total 1

  3. -rw-r--r-- 1 root root 0 Jan 3 15:23 simos_was_here

  4. root@mydev:~#

可见它已经生效了!

结论

当我们启动 LXD 容器的时候,我们常常需要默认启用一些配置,并且希望能够避免重复工作。通常解决这个问题的方法是创建 LXD profile,然后把需要的配置添加进去。最后,当我们启动新的容器时,只需要应用该 LXD profile 即可。


via: https://blog.simos.info/how-to-preconfigure-lxd-containers-with-cloud-init/

作者:Simos Xenitellis[6] 译者:kaneg 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


推荐文章
人力资源研究  ·  90后的你们,都在做什么?存了多少钱呢? ...  ·  11 月前  
神嘛事儿  ·  我回答了 @青花釉里红龙纹杯 ...  ·  4 年前  
© 2022 51好读
删除内容请联系邮箱 2879853325@qq.com