Cloud-init 是一种用于跨平台云实例初始化的行业标准多发行版方法。它受到所有主要的公共云提供商、私有云基础设施的配置系统以及裸机安装的支持。
介绍
在启动过程中,Cloud-init 会识别它正在运行的云环境,并相应地初始化系统。云实例在首次启动时将自动配置网络、存储、SSH 密钥、软件包和各种其他系统方面。
cloud-init 工具在系统引导过程中自动初始化云实例。您可以配置 cloud-init 来执行各种任务:
- 设置主机名
- 解析和处理用户数据
- 在实例上安装软件包
- 运行脚本
- 限制默认虚拟机的行为
- 生成主机私有 SSH 密钥
- 将用户的公有 SSH 密钥添加到 .ssh/authorized_keys 以便于登录和管理
- 准备存储库以进行软件包管理
- 处理用户数据中定义的软件包操作
- 运行用户数据中的用户脚本
- 装载实例存储卷 s
阶段
在系统引导过程中,cloud-init 工具在五个阶段运行,以确定 cloud-init 是否运行,以及在哪里找到数据源和其它任务。这些阶段包括:
- 生成器(cloud_init_modules)阶段:通过使用 systemd 服务,此阶段决定在引导时是否运行 cloud-init 工具。
- 本地阶段:cloud-init 搜索本地数据源,并应用网络配置,包括基于 DHCP 的回退机制。
- 网络阶段:cloud-init 通过运行 /etc/cloud/cloud.cfg 文件中 cloud_init_modules 下列出的模块来处理用户数据。您可以在 cloud_init_modules 部分种添加、删除、启用或禁用模块。
- 配置(cloud_config_modules)阶段:cloud-init 运行 /etc/cloud/cloud.cfg 文件中 cloud_config_modules 部分下列出的模块。您可以在 cloud_config_modules 部分种添加、删除、启用或禁用模块。
- 最后(cloud_final_modules)阶段:cloud-init 运行 /etc/cloud/cloud.cfg 文件的 cloud_final_modules 部分中包含的模块和配置。它可以包括特定软件包的安装,以及触发配置管理插件和用户定义的脚本。您可以在 cloud_final_modules 部分种添加、删除、启用或禁用模块。
模块频率值
- Per instance 意味着该模块在实例的第一次引导时运行
- 例如,如果您克隆一个实例或从保存的镜像创建了一个新实例,指定为 per instance 的模块会再次运行
- Per once 表示该模块只运行一次
- 例如,如果您克隆实例或从保存的镜像创建新实例,指定为 “Per once” 的实例不会在这些实例上再次运行
- per always 表示该模块在每次引导时都运行
cloud-init 目录和文件
| 目录或文件 |
描述 |
/etc/cloud/cloud.cfg |
cloud.cfg 文件包含基本的 cloud-init 配置,您可以了解到模块会在哪个阶段运行。 |
/etc/cloud/cloud.cfg.d |
cloud.cfg.d 目录,您可以在其中为 cloud-init 添加附加指令。 |
/var/lib/cloud |
当 cloud-init 运行时,它会在 /var/lib/cloud 下创建一个目录布局。布局包括特定于您的实例配置的目录和文件。 |
/usr/share/doc/cloud-init/examples |
examples 目录包含多个示例。您可以使用它们来帮助建模您自己的指令。 |
/etc/cloud/templates |
这个目录包括您可以在特定情况下,在 cloud-init 中启用的模板。模板提供启用的指示。 |
/var/log/cloud-init.log |
cloud-init.log 文件提供了有助于调试的日志信息。 |
/run/cloud-init |
/run/cloud-init 目录包含有关数据源和 ds-identify 脚本的日志信息。 |
服务
cloud-init 包提供了四个 systemd.service(5),两个 systemd.target(5) 和一个 systemd.generator(7),并通过依赖关系配置为按如下顺序激活:
- cloud-init-generator:判断可用数据源,并启用或禁用 cloud-init.target
- cloud-init-local.service:需要文件系统可用。将执行 cloud-init init –local
- cloud-init-main.service:需要网络可用。将执行 cloud-init init –all-stages
- cloud-config.target:对应 cloud-config 启动事件“to inform third parties that - cloud-config is available”
- cloud-config.service:执行 cloud-init modules –mode=config
- cloud-final.service:执行 cloud-init modules –mode=final
- cloud-init.target:当所有服务启动后激活
安装
rm -rf /var/lib/cloud/*
rm -f /var/log/cloud-init*
sudo apt update
sudo apt install wget
apt-get install cloud-guest-utils -y
apt-get install python3-pip -y
apt-get install python3-devel
wget https://launchpad.net/cloud-init/trunk/23.2.2/+download/cloud-init-23.2.2.tar.gz
tar -zxvf cloud-init-23.2.2.tar.gz
cd cloud-init-23.2.2
pip3 install -r requirements.txt
python3 setup.py install
systemctl restart cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
cloud-init -v
cloud-init init --local
使用
# 查看状态
$ cloud-init status
$cat user-data.txt
#cloud-config
password: ubuntu
ssh_pwauth: True
ssh_authorized_keys:
- ssh-rsa AAA...fhHQ== me@xiexianbin.cn
# chpasswd: {expire: False}
chpasswd:
list: |
root:myrootpassword
cloud-user:mypassword
expire: False
#cloud-config
users:
- default
- name: user2
gecos: User N. Ame
selinux-user: staff_u
groups: users,wheel
ssh_pwauth: True
ssh_authorized_keys:
- ssh-rsa AA..vz user@domain.com
chpasswd:
list: |
root:password
cloud-user:mypassword
user2:mypassword2
expire: False
bootcmd:
- echo New MOTD >> /etc/motd
runcmd:
- echo New MOTD2 >> /etc/motd
network:
version: 1
config:
- type: physical
name: interface0
mac_address: '00:11:22:33:44:55'
subnets:
- type: static
address: 192.168.23.14/27
gateway: 192.168.23.1
dns_nameservers:
- 192.168.23.2
- 8.8.8.8
dns_search:
- exemplary.maas
network:
version: 1
config:
- type: physical
name: interface0
mac_address: '00:11:22:33:44:55'
subnets:
- type: static
address: 192.168.23.14/27
gateway: 192.168.23.1
- type: nameserver
interface: interface0 # Ties nameserver to interface0 only
address:
- 192.168.23.2
- 8.8.8.8
search:
- exemplary