操作场景
Cloud-init 主要提供实例首次初始化时自定义配置的能力。如果导入的镜像没有安装 cloud-init 服务,基于该镜像启动的实例将无法被正常初始化,导致该镜像无法正常导入。本文档指导您安装 cloud-init 服务。
安装 cloud-init 推荐以下三种方式:
前提条件
安装 cloud-init 的服务器可正常访问外网。
操作步骤
说明
cloud-init 依赖于
qcloud-python, qcloud-python 是腾讯云重新编译打包的软件包,是单独的 python 环境,仅用于 cloud-init 运行环境,建议您根据对应操作系统下载安装包,并安装在 /usr/local/qcloud/python
目录下,与系统中默认的 python 不相冲突。cloud-init 是腾讯云基于社区20.1版本研发的,适配腾讯云运行环境的专属 cloud-init。
cloud-init 二进制包支持如下 OS:
OS | 版本 | x86_64 | arm64 | ||
| | qcloud-python | cloud-init | qcloud-python | cloud-init |
CentOS | 6 | NA | NA | ||
| 7 | ||||
| 8 | ||||
Fedora | 36 | NA | NA | ||
Kylin | 10sp1 | ||||
openSUSE | 15.4 | NA | NA | ||
Debian | 11 | ||||
| 10 | NA | NA | ||
| 9 | NA | NA | ||
| 8 | NA | NA | ||
Ubuntu | 24.04 | NA | NA | NA | |
| 22.04 | NA | NA | ||
| 20.04 | ||||
| 18.04 | ||||
| 16.04 | NA | NA | ||
UOS | 20 |
下载 cloud-init 二进制包
1. 下载上述安装包。
2. 如果系统中已经有 cloud-init,请排查并执行如下命令,清理残留。
rm -rf /var/lib/cloudrm -rf /etc/cloudrm -rf /usr/local/bin/cloud*
3. 根据操作系统,执行如下命令:
deb(Ubuntu、Debian等)系列,执行以下命令:
dpkg -i *.deb
rpm(CentOS、Red Hat Enterprise Linux、Fedora等)系列, 执行如下命令:
rpm -ivh *.rpm
4. 查询版本是否正确安装。
cloud-init qcloud -v/usr/bin/cloud-init qcloud 0011
5. 重启后生效。
下载 cloud-init 源码包
说明
在正常安装的情况下
,cloud-init-20.1.0011 版本与腾讯云的兼容性最佳,可以保证使用该镜像创建的云服务器的所有配置项都可以正常初始化。建议选择 cloud-init-20.1.0011.tar.gz 安装版本。您也可以 点此获取 其他版本的 cloud-init 源码包。本文以 cloud-init-20.1.0011 版本为例。执行以下命令,下载 cloud-init 源码包。
wget https://gerryguan-1306210569.cos.ap-chongqing.myqcloud.com/cloud-init/src/cloud-init-20.1.0011.tar.gz
安装 cloud-init
1. 执行以下命令,解压 cloud-init 安装包。
说明
如果您使用的操作系统为 Ubuntu,请切换至 root 账号。
tar -zxvf cloud-init-20.1.0011.tar.gz
2. 执行以下命令,进入已解压的 cloud-init 安装包目录(即进入 cloud-init-20.1.0011 目录)。
cd cloud-init
3. 根据操作系统版本,安装 Python-pip。
CentOS 6/7系列,执行以下命令:
yum install python3-pip -y
Ubuntu 系列,执行以下命令:
apt-get -y install python3-pip
OpenSUSE / SUSE系列,执行以下命令:
zypper -n install python3-pip
4. 执行以下命令,升级 pip。
python3 -m pip install --upgrade pip
5. 执行以下命令,安装依赖包。
注意
cloud-init 依赖组件 requests 2.20.0版本后,已弃用 Python 2.6。如果镜像环境的 Python 解释器为 Python 2.6及以下,在安装 cloud-init 依赖包之前,请执行
pip install 'requests<2.20.0'
命令,安装 requests 2.20.0 版本以下的版本。如果提示缺少
jinja2
或 yaml
模块,请手动安装:pip3 install jinja2 pyyaml
CentOS/RedHat:
yum install python3-jinja2 python3-PyYAML -y
Ubuntu/Debian:
apt-get install python3-jinja2 python3-yaml -y
通用方案:
pip3 install jinja2 pyyaml
6. 根据操作系统版本,安装 cloud-utils 组件。
CentOS 6系列,执行以下命令:
yum install cloud-utils-growpart dracut-modules-growroot -ydracut -f
CentOS 7系列,执行以下命令:
yum install cloud-utils-growpart -y
Ubuntu 系列,执行以下命令:
apt-get install cloud-guest-utils -y
OpenSUSE / SUSE 系列,执行以下命令:
zypper install -y growpart
7. 执行以下命令,安装 cloud-init。
python3 setup.py build
python3 setup.py install --init-system systemd
注意
--init-system 的可选参数有:(systemd, sysvinit, sysvinit_deb, sysvinit_freebsd, sysvinit_openrc, sysvinit_suse, upstart) [default: None]。请根据当前操作系统使用的自启动服务管理方式,进行选择。若选择错误,cloud-init 服务会无法开机自启动。
CentOS 6 及以下系统请选择 sysvinit,CentOS 7 及以上系统请选择 systemd。本文以 systemd 自启动服务管理为例。
执行以下命令,根据返回结果确认自启动服务管理方式:
systemd 系统:
strings /sbin/init | grep "/lib/system"
sysvinit 系统
strings /sbin/init | grep "sysvinit"
修改 cloud-init 配置文件
1. 根据不同操作系统,下载 cloud.cfg。
点此下载 Ubuntu 操作系统的 cloud.cfg。
点此下载 CentOS 操作系统的 cloud.cfg。
点此下载 OpenSUSE / SUSE 操作系统的 cloud.cfg。
2. 将
/etc/cloud/cloud.cfg
的内容替换为已下载的 cloud.cfg 文件内容。添加 syslog 用户
执行以下命令,添加 syslog 用户。
useradd syslog
设置 cloud-init 服务开机自启动
若操作系统是 systemd 自启动管理服务,则执行以下命令进行设置。
说明
您可执行
strings /sbin/init | grep "/lib/system"
命令,若有返回信息,则操作系统是 systemd 自启动管理服务。针对 Ubuntu 或 Debian 操作系统,需执行以下命令。
ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init
所有操作系统都需执行以下命令。
systemctl enable cloud-init-local.servicesystemctl start cloud-init-local.servicesystemctl enable cloud-init.servicesystemctl start cloud-init.servicesystemctl enable cloud-config.servicesystemctl start cloud-config.servicesystemctl enable cloud-final.servicesystemctl start cloud-final.servicesystemctl status cloud-init-local.servicesystemctl status cloud-init.servicesystemctl status cloud-config.servicesystemctl status cloud-final.service
针对 CentOS 7 及以上版本、Red Hat Enterprise Linux 7 及以上版本的操作系统,需执行以下命令。
备份原始文件:
cp /usr/lib/systemd/system/cloud-init-local.service /usr/lib/systemd/system/cloud-init-local.service.bak
使用 root 权限编辑文件:
sudo
vim
/usr/lib/systemd/system/cloud-init.service
将
/usr/lib/systemd/system/cloud-init-local.service
文件替换为如下内容:[Unit]Description=Initial cloud-init job (pre-networking)Wants=network-pre.targetAfter=systemd-remount-fs.serviceBefore=NetworkManager.serviceBefore=network-pre.targetBefore=shutdown.targetConflicts=shutdown.targetRequiresMountsFor=/var/lib/cloud[Service]Type=oneshotExecStart=/usr/bin/cloud-init init --localExecStart=/bin/touch /run/cloud-init/network-config-readyRemainAfterExit=yesTimeoutSec=0# Output needs to appear in instance console outputStandardOutput=journal+console[Install]WantedBy=cloud-init.target
针对使用 systemd 初始化系统的发行版(包括 CentOS 7+、RHEL 7+、Fedora 22+、OpenSUSE 15+等),需执行以下操作:
将
/usr/lib/systemd/system/cloud-init.service
文件替换为如下内容:[Unit]Description=Initial cloud-init job (metadata service crawler)Wants=cloud-init-local.serviceWants=sshd-keygen.serviceWants=sshd.serviceAfter=cloud-init-local.serviceAfter=systemd-networkd-wait-online.serviceAfter=networking.serviceAfter=systemd-hostnamed.serviceBefore=network-online.targetBefore=sshd-keygen.serviceBefore=sshd.serviceBefore=systemd-user-sessions.serviceConflicts=shutdown.target[Service]Type=oneshotExecStart=/usr/bin/cloud-init initRemainAfterExit=yesTimeoutSec=0# Output needs to appear in instance console outputStandardOutput=journal+console[Install]WantedBy=cloud-init.target
替换内容后验证 unit 文件语法:
systemd-analyze verify /usr/lib/systemd/system/cloud-init.service
若使用非 x86_64架构,需确认 Service 单元中的执行路径是否存在差异。
若操作系统是 sysvinit 自启动管理服务,则执行以下命令进行设置。
说明
您可执行
strings /sbin/init | grep "sysvinit"
命令,若有返回信息,则操作系统是 sysvinit 自启动管理服务。chkconfig --add cloud-init-localchkconfig --add cloud-initchkconfig --add cloud-configchkconfig --add cloud-finalchkconfig cloud-init-local onchkconfig cloud-init onchkconfig cloud-config onchkconfig cloud-fin
安装 cloud-init
执行以下命令,安装 cloud-init
。对于 Debian/Ubuntu 系统(使用 apt):
sudo apt-get updatesudo apt-get install -y cloud-init
对于 RHEL/CentOS 系统(使用 yum):
sudo yum install -y cloud-init
对于 openSUSE/SLES 系统(使用 zypper):
sudo zypper refreshsudo zypper install -y cloud-init
对于 Alpine Linux 系统(使用 apk):
sudo apk add cloud-init
说明
通过 apt-get 或 yum 命令安装的 cloud-init 默认为当前操作系统配置的软件源中默认的 cloud-init 版本。使用该方式安装的镜像创建的实例可能会存在部分配置项初始化不符合预期的情况,建议使用 手工下载 cloud-init 源码包方式 进行安装。
修改 cloud-init 配置文件
方法一:直接替换
1. 根据不同操作系统,下载 cloud.cfg。
点此下载 Ubuntu 操作系统的 cloud.cfg。
点此下载 CentOS 操作系统的 cloud.cfg。
点此下载 OpenSUSE / SUSE 操作系统的 cloud.cfg。
点此下载 Red Hat 操作系统的 cloud.cfg。
2. 将
/etc/cloud/cloud.cfg
的内容替换为已下载的 cloud.cfg 文件内容。
方法二:手动编辑
1. 打开配置文件。
sudo vim /etc/cloud/cloud.cfg
2. 将
cloud_init_modules
之前的内容改为如下配置:# The top level settings are used as module# and base configuration.# A set of users which may be applied and/or used by various modules# when a 'default' entry is found it will reference the 'default_user'# from the distro configuration specified belowusers:- default# If this is set, 'root' will not be able to ssh in and they# will get a message to login instead as the default $userdisable_root: falseresize_rootfs_tmp: /devssh_pwauth: false# This will cause the set+update hostname module to not operate (if true)preserve_hostname: false# If you use datasource_list array, keep array items in a single line.# If you use multi line array, ds-identify script won't read array items.# Example datasource config# datasource:# Ec2:# metadata_urls: [ 'blah.com' ]# timeout: 5 # (defaults to 50 seconds)# max_wait: 10 # (defaults to 120 seconds)datasource_list: [ ConfigDrive ]# The modules that run in the 'init' stagecloud_init_modules:
3. 修改
update_etc_hosts
的配置:cloud_init_modules:- migrator- seed_random- bootcmd- write_files- growpart- resizefs- set_hostname- update_hostname# 修改这一行- ['update_etc_hosts', 'once-per-instance']- rsyslog- users_groups- ssh
4. 确保系统默认账户符合预期。
说明:
此处只检查 default_user 中的 name 字段,请勿直接复制。
# System and/or distro specific settings# (not accessible to handlers/transforms)system_info:# This will affect which distro class gets useddistro: ubuntu# Default user name + that default users groups (if added/used)default_user:name: ubuntu # 这里是 ubuntu 账号lock_passwd: falsegecos: Cloud Usergroups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video]sudo: ["ALL=(ALL) NOPASSWD:ALL"]shell: /bin/bash
# System and/or distro specific settings# (not accessible to handlers/transforms)system_info:# This will affect which distro class gets useddistro: rhel # CentOS/RedHat系列# Default user name + that default users groups (if added/used)default_user:name: root # 这里是 root 账号lock_passwd: falsegecos: Cloud Usergroups: [wheel, adm, systemd-journal]sudo: ["ALL=(ALL) NOPASSWD:ALL"]shell: /bin/bash
执行
ls /etc/cloud/cloud.cfg
确认文件路径是否存在。 如果路径不存在,请通过
sudo find / -name cloud.cfg 2>/dev/null
搜索实际路径。执行
strings /sbin/init | grep "sysvinit"
检查init系统类型。如果提示缺少 `jinja2` 或 `yaml` 模块,请手动安装:
pip3 install jinja2 pyyaml
。相关操作
注意
以下操作执行完成后,请勿重启服务器,否则需重新执行以下操作。
1. 执行以下命令,检查 cloud-init 相关配置是否成功。
cloud-init init --local
返回类似如下信息,则说明已成功配置 cloud-init。
Cloud-init v. 20.1.0011 running 'init-local' at Fri, 01 Apr 2022 01:26:11 +0000. Up 38.70 seconds.
2. 执行以下命令,删除 cloud-init 的缓存记录。
rm -rf /var/lib/cloud
3. 针对 Ubuntu 或 Debian 操作系统,需执行以下命令。
rm -rf /etc/network/interfaces.d/50-cloud-init.cfg
4. 针对 Ubuntu 或 Debian 操作系统,需将
/etc/network/interfaces
修改为以下内容:# This file describes the network interfaces available on your system# and how to activate them. For more information, see interfaces(5).source /etc/network/interfaces.d/*
附录
解决无法安装 Python-pip 问题
若在安装 Python-pip 出现无此安装包或无法安装的错误,可对应实际使用的操作系统,参考以下步骤进行解决:
1. 执行以下命令,设置 EPEL 存储库。
yum install epel-release -y
2. 执行以下命令,安装 Python-pip。
yum install python3-pip -y
1. 执行以下命令,清除缓存。
apt-get clean all
2. 执行以下命令,更新软件包列表。
apt-get update -y
3. 执行以下命令,安装 Python-pip。
apt-get -y install python3-pip