安装cloud-init
cloud-init提供了ECS实例在启动阶段完成系统初始化配置的能力。如果您的自定义镜像未安装cloud-init,请手动安装,保证运行该镜像的ECS实例能成功完成初始化配置。本文介绍如何安装并配置cloud-init。
关于cloud-init的更多信息,请参见cloud-init官方文档。
操作场景
阿里云所有公共镜像默认安装cloud-init。为保证使用自定义镜像创建的ECS实例能自动初始化系统配置,建议您在以下场景中为Linux服务器安装阿里云版cloud-init。
准备迁移上云的,但未安装cloud-init的Linux服务器。
说明不准备迁移上云的服务器需谨慎安装。
已安装cloud-init,但版本低于0.7.9的Linux服务器。
已在阿里云运行的,但未安装cloud-init的ECS实例。
步骤一:检查是否需要升级安装cloud-init
登录源服务器。
运行以下命令检查是否已安装cloud-init。
CentOS系列
rpm -qa | grep -i cloud-init pip list | grep -i cloud-init
Ubuntu系列
dpkg -l | grep -i cloud-init pip list | grep -i cloud-init
若无任何输出或版本低于社区0.7.9版本:您需要步骤二:安装cloud-init。
说明0.7.9版本初期的社区版cloud-init,不适用于初始化ECS实例,必须升级至较高版本。
若版本为18或高于18版本:无需安装cloud-init,但cloud-init在初始化实例时可以自动配置网络,如果默认配置不符合您的需求,您可按需自定义网络配置。
其他:无需再安装cloud-init,可直接执行后续步骤。
步骤二:安装cloud-init
阿里云版cloud-init 19.1.21:推荐,依赖于Python 3.6。
阿里云版cloud-init 0.7.6a:若操作系统为CentOS 6、Debian 9及SUSE Linux Enterprise Server 12等,请选择该版本,依赖于Python 2.7。
说明由于Python社区停止对Python 2.7的技术支持,建议您尽量使用高版本cloud-init,避免依赖库隐患。
社区版本cloud-init:社区版cloud-init由社区维护。阿里云cloud-init的最新版本为19.1.21,如果您需要使用更高版本的cloud-init,可以安装社区版本cloud-init。
社区版的cloud-init是cloud-init项目的官方版本,而阿里云版的cloud-init是针对阿里云平台进行优化的版本,可以更好地支持阿里云的平台服务,因此推荐您使用阿里云版cloud-init。
为避免误操作导致数据丢失,建议您先备份源服务器数据(例如创建快照)。
(推荐)安装阿里云版cloud-init
阿里云cloud-init的最新版本为19.1.21,数据源为Aliyun
。您可以在cloud-init官方网站自行下载其他版本的cloud-init。
确保源服务器已安装Python PIP依赖库。
以安装Python3-pip依赖库为例,Linux部分发行版的安装命令如下。
CentOS/Red Hat Enterprise Linux:
yum -y install python3-pip
Ubuntu/Debian:
apt-get -y install python3-pip
OpenSUSE/SUSE:
zypper -n install python3-pip
运行以下命令下载阿里云版cloud-init。
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
运行以下命令解压cloud-init安装包到当前目录。
tar -zxvf cloud-init-19.1.21.tgz
进入cloud-init目录下,并安装依赖库。
cd ./cloud-init-19.1.21 pip3 install -r ./requirements.txt
进入cloud-init的tools目录。
cd ./tools
运行以下命令执行安装cloud-init的脚本deploy.sh。
bash ./deploy.sh <issue> <major_version>
deploy.sh脚本的参数说明和取值示例如下:
参数
说明
示例
<issue>
操作系统平台类型。取值范围:centos | redhat |rhel | debian | ubuntu | opensuse | sles。参数取值均大小写敏感,其中sles表示SUSE/SLES。
centos
<major_version>
操作系统平台主要版本号。
说明Ubuntu 14不支持安装阿里云版cloud-init 19.1.21。
CentOS 7.6的主要版本号为7
例如,您当前的操作系统为CentOS 7,则需要运行的命令为
bash ./deploy.sh centos 7
。确认cloud-init是否安装成功。
若返回
"description": "success"
,表示安装成功。
不同Linux发行平台安装阿里云cloud-init的Shell脚本示例如下,供您参考。实际安装时,请根据您的操作系统适当调整脚本。
CentOS 7/8
# 检查安装python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
yum -y install python3-pip
fi
# 备份旧版cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# 下载并解压阿里云版cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# 安装cloud-init
issue_major=$( cat /etc/redhat-release | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh centos "$issue_major"
Red Hat Enterprise Linux 7/8
# 检查安装python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
yum -y install python3-pip
fi
# 备份旧版cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# 下载并解压阿里云版cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# 安装cloud-init
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh rhel "$issue_major"
Ubuntu 16/18/20
# 检查安装python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
apt-get install python36 python3-pip -y
fi
# 备份旧版cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# 下载并解压阿里云版cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# 安装cloud-init
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh ubuntu "$issue_major"
Debian 9/10
# 检查安装python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
apt-get -y install python3-pip
fi
# 备份旧版cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# 下载并解压阿里云版cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# 安装cloud-init
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh debian "$issue_major"
SUSE 12/15
# 检查安装python3-pip
if ! python3 -c 'import setuptools'>& /dev/null; then
zypper -n install python3-pip
fi
# 备份旧版cloud-init
test -d /etc/cloud && mv /etc/cloud/etc/cloud-old
# 下载并解压阿里云版cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# 安装cloud-init
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh sles "$issue_major"
OpenSUSE 15
# 检查安装python3-pip
if ! python3 -c 'import setuptools'>& /dev/null; then
zypper -n install python3-pip
fi
# 备份旧版cloud-init
test -d /etc/cloud && mv /etc/cloud/etc/cloud-old
# 下载并解压阿里云版cloud-init
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# 安装cloud-init
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh opensuse"$issue_major"
安装阿里云版cloud-init 0.7.6a15
部分低版本操作系统(例如CentOS 6、Debian 9及SUSE Linux Enterprise Server 12等)仍旧保持该版本cloud-init。
阿里云公共镜像CentOS 6、Debian 9及SUSE Linux Enterprise Server 12默认已安装cloud-init-0.7.6a15
。如果您需要进行测试,请先运行命令mv /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg_bak备份配置文件。
运行以下命令,检查操作系统的版本为CentOS 6、Debian 9及SUSE Linux Enterprise Server 12。
cat /etc/issue
运行以下命令,下载并解压阿里云版cloud-init 0.7.6a15。
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloud-init-0.7.6a15.tgz tar -zxvf cloud-init-0.7.6a15.tgz
进入cloud-init的tools目录。
cd cloud-init-0.7.6a15/tools/
运行以下命令。安装cloud-init。
bash ./deploy.sh <issue> <major_version>
deploy.sh脚本的参数说明和取值示例如下:
参数
说明
示例
<issue>
操作系统平台类型。取值范围:centos | debian | sles。参数取值均大小写敏感,其中sles表示SUSE/SLES。
centos
<major_version>
操作系统平台主要版本号。
CentOS 6.5的主要版本号为6
例如,您当前的操作系统为CentOS 6,则需要运行的命令为
bash ./deploy.sh centos 6
。
安装社区版cloud-init
阿里云cloud-init的最新版本为19.1.21,如果您需要使用更高版本的cloud-init,可以安装社区版本cloud-init。
确保源服务器已安装Git、Python和Python PIP依赖库。
以安装Git、Python3.6和Python3-pip依赖库为例,Linux部分发行版的安装命令如下。
CentOS/Red Hat Enterprise Linux
yum -y install git python36 python3-pip
Ubuntu/Debian
apt-get -y install git python36 python3-pip
OpenSUSE/SUSE
zypper -n install git python36 python3-pip
运行以下命令使用Git下载cloud-init源码包。
git clone https://git.launchpad.net/cloud-init
进入cloud-init目录。
cd ./cloud-init
运行以下命令安装所有依赖库。
sudo pip3 install -r ./requirements.txt
运行以下命令安装cloud-init。
python3 setup.py install
修改配置文件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版本为18或者高于18,会自动完成网络的初始化配置,自动配置的网络为BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes STARTMODE=auto TYPE=Ethernet USERCTL=no
。如果默认的网络配置不符合您的业务需求,可以参考以下操作自定义网络。
安装cloud-init后,打开/etc/cloud/cloud.cfg文件。
vim /etc/cloud/cloud.cfg
按i进入编辑模式,在
Example datasource config
之前增加disabled配置。network: config: disabled
说明增加该配置之后,cloud-init不会管理/etc/sysconfig/network-scripts/下网络配置,需要您自行管理。
编辑完成后,按Esc键退出编辑模式,然后输入
:wq
并回车,保存退出文件。
保留主机名以及/etc/hosts配置文件
cloud-init的配置文件中,默认设置了不保留主机名。如果您不希望实例的主机名以及/etc/hosts配置文件被修改,可以参考以下操作修改cloud-init的配置文件。
安装cloud-init后,打开/etc/cloud/cloud.cfg文件。
vim /etc/cloud/cloud.cfg
按i进入编辑模式,将
preserve_hostname: false
修改为preserve_hostname: true
。编辑完成后,按Esc键退出编辑模式,然后输入
:wq
并回车,保存退出文件。当
preserve_hostname: true
时,cloud-init将不会自动修改主机名以及/etc/hosts配置文件。
故障排查
后续步骤
对于准备迁移上云的Linux服务器,您可以使用迁云工具迁移上云。
对于已在阿里云上运行Linux自定义镜像的ECS实例,您可以重启系统验证结果。如果系统自动配置了主机名、软件源和NTP等配置,则表示已成功安装cloud-init。
例如查看网络配置文件结果如下:
[testuser@iZbp1ios3psx4hoi******Z ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 # Created by cloud-init on instance boot automatically, do not edit. # BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes STARTMODE=auto TYPE=Ethernet USERCTL=no
对于正在制作镜像的源服务器,你需要继续安装virtio驱动、检测镜像是否符合规范、获取Linux镜像文件。