非阿里云支持系统安装阿里云cloud-init

更新于: 2025-03-22 08:59:23 查看: 104

发布于: 2025-3-14 10:20 发布者: 大米

一、环境准备安装依赖工具确保系统已安装编译工具和 Python 环境:sudo yum install -y gcc make python3-devel python3-pip openssl-devel git安装 cloud-utils 组件根据系统类型安装必要扩展工具(用于磁盘扩展等 ...

一、环境准备
安装依赖工具
确保系统已安装编译工具和 Python 环境:
yum install -y gcc make python3-devel python3-pip openssl-devel git
安装 cloud-utils 组件
根据系统类型安装必要扩展工具(用于磁盘扩展等):
yum install -y cloud-utils-growpart

二、安装 cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-23.2.2-5.tar.gz
tar -zxvf cloud-init-23.2.2-5.tar.gz
cd cloud-init-23.2.2-5

安装 Python 依赖
pip3 install -r requirements.txt

编译并安装
使用 setup.py 安装,并指定初始化系统类型(如 systemd):
python3 setup.py build
python3 setup.py install --init-system systemd  # 根据系统选择 systemd/sysvinit 等
三、配置 cloud-init
修改主配置文件
编辑 /etc/cloud/cloud.cfg,确保阿里云数据源和关键模块启用:
vi /etc/cloud/cloud.cfg
将cloud_init_modules:之前的配置修改为以下内容
# Example datasource config
# The top level settings are used as module
# and system 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 below
users:
   - default
user:
    name: root
    lock_passwd: False
# If this is set, 'root' will not be able to ssh in and they 
# will get a message to login instead as the above $user
disable_root: false
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
syslog_fix_perms: root:root
datasource_list: [ AliYun ]
# Example datasource config
datasource:
    AliYun:
        support_xen: false
        timeout: 5 # (defaults to 50 seconds)
        max_wait: 60 # (defaults to 120 seconds)
#      metadata_urls: [ 'blah.com' ]
# The modules that run in the 'init' stage
cloud_init_modules:

运行以下命令,查看cloud-init的版本信息是否符合预期。
cloud-init --version

(可选)配置:
禁用阿里云自动配置网络初始化
运行以下命令,以打开cloud-init的默认配置文件,按i进入插入模式。
vi /etc/cloud/cloud.cfg
在Example datasource config之前增加disabled配置,禁用cloud-init的网络自动配置功能。
network:
  config: disabled
增加该配置之后,cloud-init不会再管理/etc/sysconfig/network-scripts/目录下的网络配置文件(例如ifcfg-eth0),需要您自行管理。
按Esc键,输入:wq,按Enter键关闭并保存配置文件。

根据需求自定义/etc/sysconfig/network-scripts/目录下的网络配置,例如IP地址、子网掩码、网关等

禁用阿里云自动设置主机名
默认情况下,cloud-init会在实例启动时自动设置主机名并更新/etc/hostname文件。如果您不希望修改,可以参考以下操作修改cloud-init的配置文件。

运行以下命令,以打开cloud-init的默认配置文件,按i进入编辑模式。
vi /etc/cloud/cloud.cfg
将配置文件中的preserve_hostname: false修改为preserve_hostname: true。
按Esc键,输入:wq,按Enter键关闭并保存配置文件。

四、配置服务自启动
安装cloud-init后,但系统初始化配置和实例自定义数据(User data)脚本无法正常执行,如何处理?
运行以下命令,检查cloud-init的四个服务是否设置为开机自启动。
systemctl is-enabled cloud-init-local.service
systemctl is-enabled cloud-init.service
systemctl is-enabled cloud-config.service
systemctl is-enabled cloud-final.service
如果出现报错信息或者disabled信息,表示cloud-init未设置开机自启动。
运行以下命令,设置cloud-init开机自启动。
systemctl enable cloud-init-local.service
systemctl enable cloud-init.service
systemctl enable cloud-config.service
systemctl enable cloud-final.service
在创建实例时对系统盘进行扩容,但在实例内部发现根分区大小并没有自动扩容,如何处理?

Linux实例分区自动扩容由cloud-init提供支持,需要确保系统安装growpart工具且cloud.cfg文件中配置正确。

运行以下命令,检查系统是否安装growpart工具。

放大查看复制代码
which growpart
(条件必选)如果没有安装,运行以下命令安装growpart工具。

RHEL系列:

放大查看复制代码
sudo yum -y install cloud-utils-growpart
Debian系列:

放大查看复制代码
sudo apt -y install cloud-guest-utils
运行以下命令,查看/etc/cloud/cloud.cfg文件中是否存在如下图中的内容。

放大查看复制代码
cat /etc/cloud/cloud.cfg
配置内容

(条件必选)如果不存在,请将该内容添加到/etc/cloud/cloud.cfg文件中。

五、验证安装
检查版本和服务状态

cloud-init --version  # 应显示 23.2.2
systemctl status cloud-init  # 状态应为 active (exited)

执行初始化测试
cloud-init init --local

# 无报错且生成 /var/lib/cloud/instance 目录即成功
六、注意事项
兼容性问题

确保 Python 版本 >= 3.6(推荐 3.7+),旧版系统需手动升级 Python 
若升级前存在旧版 cloud-init,
yum remove cloud-init -y

需彻底清理残留文件:
rm -rf /etc/cloud /var/lib/cloud /usr/local/bin/cloud*

阿里云适配
镜像制作时需禁用 cloud-init 缓存:修改 /etc/cloud/cloud.cfg 添加 disable_vmware_customization: true 

若使用自定义镜像,需在阿里云控制台配置初始化选项(如主机名策略) 


支持

反对
扫描二维码,手机查看
声明:部分数据/图片来源互联网,不代表欢乐你我,真实性请妥善甄别。