如何使用 cloud-init 来预配置 LXD 容器
创始人
2024-03-01 21:57:07
0

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

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

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

如何创建一个新的 LXD profile

查看已经存在的 profile:

$ lxc profile list
+---------|---------+
| NAME    | USED BY |
+---------|---------+
| default | 11      |
+---------|---------+

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

$ lxc profile copy default devprofile

$ lxc profile list
+------------|---------+
| NAME       | USED BY |
+------------|---------+
| default    | 11      |
+------------|---------+
| devprofile | 0       |
+------------|---------+

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

$ lxc profile show devprofile
config:
 environment.TZ: ""
description: Default LXD profile
devices:
 eth0:
 nictype: bridged
 parent: lxdbr0
 type: nic
 root:
 path: /
 pool: default
 type: disk
name: devprofile
used_by: []

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

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

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

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

 package_upgrade: true
 packages:
 - build-essential
 locale: es_ES.UTF-8
 timezone: Europe/Madrid
 runcmd:
 - [touch, /tmp/simos_was_here]

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

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

我首选的方法是:

$ lxc profile edit devprofile

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

$ lxc profile show devprofile
config:
  environment.TZ: ""
  user.user-data: |
    #cloud-config
    package_upgrade: true
    packages:
      - build-essential
    locale: es_ES.UTF-8
    timezone: Europe/Madrid
    runcmd:
      - [touch, /tmp/simos_was_here]
description: Default LXD profile
devices:
  eth0:
    nictype: bridged
    parent: lxdbr0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: devprofile
used_by: []

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

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

$ lxc launch --profile devprofile ubuntu:x mydev

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

$ lxc exec mydev bash
root@mydev:~# ps ax
 PID TTY STAT TIME COMMAND
 1 ? Ss 0:00 /sbin/init
 ...
 427 ? Ss 0:00 /usr/bin/python3 /usr/bin/cloud-init modules --mode=f
 430 ? S 0:00 /bin/sh -c tee -a /var/log/cloud-init-output.log
 431 ? S 0:00 tee -a /var/log/cloud-init-output.log
 432 ? S 0:00 /usr/bin/apt-get --option=Dpkg::Options::=--force-con
 437 ? S 0:00 /usr/lib/apt/methods/http
 438 ? S 0:00 /usr/lib/apt/methods/http
 440 ? S 0:00 /usr/lib/apt/methods/gpgv
 570 ? Ss 0:00 bash
 624 ? S 0:00 /usr/lib/apt/methods/store
 625 ? R+ 0:00 ps ax
root@mydev:~#

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

Generating locales (this might take a while)...
 es_ES.UTF-8... done
Generation complete.

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

Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
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

The following packages will be upgraded:
 libdrm2 libseccomp2 squashfs-tools unattended-upgrades
4 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 211 kB of archives.

以上是在执行 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 执行的结果如何?

root@mydev:~# ls -l /tmp/
total 1
-rw-r--r-- 1 root root 0 Jan 3 15:23 simos_was_here
root@mydev:~#

可见它已经生效了!

结论

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


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

作者:Simos Xenitellis 译者:kaneg 校对:wxy

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

相关内容

泰禾智能:参股公司科亿智能...
金融界7月15日消息,有投资者在互动平台向泰禾智能提问:董秘您好,...
2025-07-15 17:14:13
在CentOS系统中为Do...
理解Docker存储的核心机制:驱动与分层管理 在CentOS系统...
2025-06-16 07:41:58
我的docker随笔39:...
本文介绍在容器中修改时间而不影响宿主机的系统时间。 问题提出 某容...
2025-06-01 18:45:21
容器安全配置
本博客地址:https://security.blo...
2025-06-01 06:33:19
HTML - 容器布局,使...
HTML - 容器布局,使容器充满屏幕高度ÿ...
2025-06-01 02:27:27
docker 容器相互访问...
我们都知道docker是一个个容器,他们相互独立互不...
2025-05-30 11:55:49

热门资讯

Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
《Apex 英雄》正式可在 S... 《Apex 英雄》现已通过 Steam Deck 验证,这使其成为支持 Linux 的顶级多人游戏之...
从 Yum 更新中排除特定/某... 作为系统更新的一部分,你也许需要在基于 Red Hat 系统中由于应用依赖排除一些软件包。如果是,如...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...
如何检查你的 Linux 系统... 不知道在使用哪个初始化系统?以下是方法。每个主流 Linux 发行版(包括 Ubuntu、Fedor...