准备与 cloud-init 配合使用的现有 Linux Azure VM 映像Prepare an existing Linux Azure VM image for use with cloud-init

本文介绍如何选择一个现有的 Azure 虚拟机,使其准备好重新部署并可使用 cloud-init。This article shows you how to take an existing Azure virtual machine and prepare it to be redeployed and ready to use cloud-init. 生成的映像可用于部署新的虚拟机或虚拟机规模集 - 然后,可以在部署时通过 cloud-init 进一步对其进行自定义。The resulting image can be used to deploy a new virtual machine or virtual machine scale sets - either of which could then be further customized by cloud-init at deployment time. Azure 预配资源后,这些 cloud-init 脚本即会在第一次启动时运行。These cloud-init scripts run on first boot once the resources have been provisioned by Azure. 有关 cloud-init 如何在 Azure 以及受支持的 Linux 发行版中本机工作的详细信息,请参阅 cloud-init 概述For more information about how cloud-init works natively in Azure and the supported Linux distros, see cloud-init overview

先决条件Prerequisites

本文档假设已有一个运行受支持 Linux 操作系统版本的 Azure 虚拟机。This document assumes you already have a running Azure virtual machine running a supported version of the Linux operating system. 已根据需要配置该计算机,已安装所需的所有模块,已处理所需的所有更新并已对其进行测试,确保其满足要求。You have already configured the machine to suit your needs, installed all the required modules, processed all the required updates and have tested it to ensure it meets your requirements.

准备 CentOS 7.6Preparing CentOS 7.6

需要通过 SSH 连接到 Linux VM,并运行以下命令安装 cloud-init。You need to SSH into your Linux VM and run the following commands in order to install cloud-init.

sudo yum makecache fast
sudo yum install -y gdisk cloud-utils-growpart
sudo yum install - y cloud-init 

更新 /etc/cloud/cloud.cfg 中的 cloud_init_modules 节以包含以下模块:Update the cloud_init_modules section in /etc/cloud/cloud.cfg to include the following modules:

- disk_setup
- mounts

以下示例显示某个通用 cloud_init_modules 节的大致内容。Here is a sample of what a general-purpose cloud_init_modules section looks like.

cloud_init_modules:
 - migrator
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - disk_setup
 - mounts
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - rsyslog
 - users-groups
 - ssh

需要在 /etc/waagent.conf 中更新一些与预配和处理临时磁盘相关的任务。A number of tasks relating to provisioning and handling ephemeral disks need to be updated in /etc/waagent.conf. 运行以下命令更新相应的设置。Run the following commands to update the appropriate settings.

sed -i 's/Provisioning.Enabled=y/Provisioning.Enabled=n/g' /etc/waagent.conf
sed -i 's/Provisioning.UseCloudInit=n/Provisioning.UseCloudInit=y/g' /etc/waagent.conf
sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.conf
sed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf
cloud-init clean

使用所选的编辑器创建包含以下行的新文件 /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg,以便只允许将 Azure 用作 Azure Linux 代理的数据源:Allow only Azure as a datasource for the Azure Linux Agent by creating a new file /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg using an editor of your choice with the following line:

# Azure Data Source config
datasource_list: [ Azure ]

如果现有 Azure 映像中配置了交换文件,而你想要使用 cloud-init 更改新映像的交换文件配置,则需要删除现有的交换文件。If your existing Azure image has a swap file configured and you want to change the swap file configuration for new images using cloud-init, you need to remove the existing swap file.

对于已启用交换文件的 CentOS 映像,可运行以下命令来关闭交换文件:For CentOS images with swapfile enabled, you can run the following command to turn off the swapfile:

sudo swapoff /mnt/resource/swapfile

确保从 /etc/fstab 中删除交换文件引用 - 应返回如下所示的输出:Ensure the swapfile reference is removed from /etc/fstab - it should look something like the following output:

# /etc/fstab
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=99cf66df-2fef-4aad-b226-382883643a1c / xfs defaults 0 0
UUID=7c473048-a4e7-4908-bad3-a9be22e9d37d /boot xfs defaults 0 0

若要节省空间并删除交换文件,可运行以下命令:To save space and remove the swap file you can run the following command:

rm /mnt/resource/swapfile

针对 cloud-init 准备的映像的附加步骤Extra step for cloud-init prepared image

备注

如果映像是事先已由 cloud-init 准备且配置的映像,则需要执行以下步骤。If your image was previously a cloud-init prepared and configured image, you need to do the following steps.

仅当想要自定义为新专用源映像的 VM 事先已由 cloud-init 预配时,才需要使用以下三个命令。The following three commands are only used if the VM you are customizing to be a new specialized source image was previously provisioned by cloud-init. 如果映像是使用 Azure Linux 代理配置的,则不需要运行这些命令。You do NOT need to run these if your image was configured using the Azure Linux Agent.

sudo cloud-init clean --logs
sudo waagent -deprovision+user -force

完成 Linux 代理设置Finalizing Linux Agent setting

所有 Azure 平台映像都装有 Azure Linux 代理,不管它们是否已由 cloud-init 配置。All Azure platform images have the Azure Linux Agent installed, regardless if it was configured by cloud-init or not. 运行以下命令完成从 Linux 计算机中取消预配用户。Run the following command to finish deprovisioning the user from the Linux machine.

sudo waagent -deprovision+user -force

有关 Azure Linux 代理取消预配命令的详细信息,请参阅 Azure Linux 代理For more information about the Azure Linux Agent deprovision commands, see the Azure Linux Agent for more details.

退出 SSH 会话,然后从 bash shell 运行以下 AzureCLI 命令,以解除分配、通用化并创建新的 Azure VM 映像。Exit the SSH session, then from your bash shell, run the following AzureCLI commands to deallocate, generalize and create a new Azure VM image. 请将 myResourceGroupsourceVmName 替换为反映源 VM 的相应信息。Replace myResourceGroup and sourceVmName with the appropriate information reflecting your sourceVM.

az vm deallocate --resource-group myResourceGroup --name sourceVmName
az vm generalize --resource-group myResourceGroup --name sourceVmName
az image create --resource-group myResourceGroup --name myCloudInitImage --source sourceVmName

后续步骤Next steps

有关配置更改的其他 cloud-init 示例,请参阅以下文章:For additional cloud-init examples of configuration changes, see the following: