使用 cloud-init 在 Azure 的 Linux VM 中更新和安装包Use cloud-init to update and install packages in a Linux VM in Azure

本文演示如何在 Azure 中使用 cloud-init 在预配时间更新 Linux 虚拟机 (VM) 或虚拟机规模集上的包。This article shows you how to use cloud-init to update packages on a Linux virtual machine (VM) or virtual machine scale sets at provisioning time in Azure. 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

使用 cloud-init 更新 VMUpdate a VM with cloud-init

出于安全目的,你需要配置 VM,以便在首次启动时应用最新的更新。For security purposes, you may want to configure a VM to apply the latest updates on first boot. 由于 cloud-init 支持不同 Linux 发行版,因此无需为包管理器指定 aptyumAs cloud-init works across different Linux distros, there is no need to specify apt or yum for the package manager. 相反,你需要定义 package_upgrade,并让 cloud-init 进程确定正在使用的发行版的适当机制。Instead, you define package_upgrade and let the cloud-init process determine the appropriate mechanism for the distro in use. 此工作流允许跨发行版使用相同的 cloud-init 脚本。This workflow allows you to use the same cloud-init scripts across distros.

若要查看操作中的升级进程,请在当前 shell 中创建一个名为“cloud_init_upgrade.txt”的文件并粘贴下面的配置 。To see upgrade process in action, create a file in your current shell named cloud_init_upgrade.txt and paste the following configuration. 对于此示例,请在本地计算机中创建文件。For this example, create the file on your local machine. 可使用任何想要使用的编辑器。You can use any editor you wish. 输入 sensible-editor cloud_init_upgrade.txt 以创建文件并查看可用编辑器的列表。Enter sensible-editor cloud_init_upgrade.txt to create the file and see a list of available editors. 选择 #1 以使用 nano 编辑器 。Choose #1 to use the nano editor. 请确保已正确复制整个 cloud-init 文件,尤其是第一行。Make sure that the whole cloud-init file is copied correctly, especially the first line.

package_upgrade: true
- httpd

在部署此映像之前,需要使用 az group create 命令创建资源组。Before deploying this image, you need to create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在“chinaeast”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the chinaeast location.

az group create --name myResourceGroup --location chinaeast

现在,使用 az vm create 创建 VM,并通过 --custom-data cloud_init_upgrade.txt 指定 cloud-init 文件,如下所示:Now, create a VM with az vm create and specify the cloud-init file with --custom-data cloud_init_upgrade.txt as follows:

az vm create \
  --resource-group myResourceGroup \
  --name centos74 \
  --image OpenLogic:CentOS:7-CI:latest \
  --custom-data cloud_init_upgrade.txt \

通过 SSH 连接到 VM 的公共 IP 地址显示在先前命令的输出中。SSH to the public IP address of your VM shown in the output from the preceding command. 按如下所示输入自己的 publicIpAddressEnter your own publicIpAddress as follows:

ssh <publicIpAddress>

运行包管理工具并检查更新。Run the package management tool and check for updates.

sudo yum update

由于 cloud-init 在启动时已检查和安装更新,因此,应没有要应用的其他更新。As cloud-init checked for and installed updates on boot, there should be no additional updates to apply. 你可以通过运行 yum history 来查看更新过程、更改的程序包数量以及 httpd 的安装,并查看类似于以下内容的输出。You see the update process, number of altered packages as well as the installation of httpd by running yum history and review the output similar to the one below.

Loaded plugins: fastestmirror, langpacks
ID     | Command line             | Date and time    | Action(s)      | Altered
     3 | -t -y install httpd      | 2018-04-20 22:42 | Install        |    5
     2 | -t -y upgrade            | 2018-04-20 22:38 | I, U           |   65
     1 |                          | 2017-12-12 20:32 | Install        |  522

后续步骤Next steps

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