您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure CLI 创建 Azure VM 的自定义映像Tutorial: Create a custom image of an Azure VM with the Azure CLI

自定义映像类似于市场映像,不同的是自定义映像的创建者是自己。Custom images are like marketplace images, but you create them yourself. 自定义映像可用于启动配置,例如预加载应用程序、应用程序配置和其他 OS 配置。Custom images can be used to bootstrap configurations such as preloading applications, application configurations, and other OS configurations. 在本教程中,将创建自己的 Azure 虚拟机自定义映像。In this tutorial, you create your own custom image of an Azure virtual machine. 学习如何:You learn how to:

  • 预配和通用化 VMDeprovision and generalize VMs
  • 创建自定义映像Create a custom image
  • 从自定义映像创建 VMCreate a VM from a custom image
  • 列出订阅中的所有映像List all the images in your subscription
  • 删除映像Delete an image

打开 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的操作步骤。Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Cloud Shell 中预安装并配置了常用的 Azure 工具以供你的帐户使用。Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. 需选择“复制”按钮即可复制代码,将其粘贴到 Cloud Shell 中,然后按回车来运行它。Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. 可通过多种方式打开 Cloud Shell:There are a few ways to open Cloud Shell:

选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/bash
选择 Azure 门户右上角菜单上的“Cloud Shell”按钮。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.30 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

开始之前Before you begin

下列步骤详细说明如何将现有 VM 转换为可重用自定义映像,以便将其用于创建新 VM 实例。The steps below detail how to take an existing VM and turn it into a reusable custom image that you can use to create new VM instances.

若要完成本教程中的示例,必须具备现有虚拟机。To complete the example in this tutorial, you must have an existing virtual machine. 必要时,此脚本示例可为你创建一个。If needed, this script sample can create one for you. 通过教程操作时,根据需要替换资源组和 VM 名称。When working through the tutorial, replace the resource group and VM names where needed.

创建自定义映像Create a custom image

要创建虚拟机的映像,需通过以下方式准备 VM:取消设置、解除分配,然后将源 VM 标记为通用化。To create an image of a virtual machine, you need to prepare the VM by deprovisioning, deallocating, and then marking the source VM as generalized. 准备好 VM 后,可以创建映像。Once the VM has been prepared, you can create an image.

取消设置 VMDeprovision the VM

取消设置可通过删除特定于计算机的信息通用化 VM。Deprovisioning generalizes the VM by removing machine-specific information. 通过此通用化,可从单个映像中部署多个 VM。This generalization makes it possible to deploy many VMs from a single image. 在取消预配期间,主机名将重置为“localhost.localdomain”。During deprovisioning, the host name is reset to localhost.localdomain. 还会删除 SSH 主机密钥、名称服务器配置、根密码和缓存的 DHCP 租约。SSH host keys, nameserver configurations, root password, and cached DHCP leases are also deleted.

若要取消设置 VM,请使用 Azure VM 代理 (waagent)。To deprovision the VM, use the Azure VM agent (waagent). Azure VM 代理安装在 VM 上,并管理预配及其与 Azure 结构控制器的交互。The Azure VM agent is installed on the VM and manages provisioning and interacting with the Azure Fabric Controller. 有关详细信息,请参阅 Azure Linux 代理用户指南For more information, see the Azure Linux Agent user guide.

使用 SSH 连接到 VM 并运行命令以取消设置 VM。Connect to your VM using SSH and run the command to deprovision the VM. 使用 +user 参数还会删除上次设置的用户帐户和任何关联数据。With the +user argument, the last provisioned user account and any associated data are also deleted. 将示例 IP 地址替换为 VM 的公共 IP 地址。Replace the example IP address with the public IP address of your VM.

SSH 到 VM。SSH to the VM.

ssh azureuser@52.174.34.95

取消预配 VM。Deprovision the VM.

sudo waagent -deprovision+user -force

关闭 SSH 会话。Close the SSH session.

exit

解除分配并将 VM 标记为通用化Deallocate and mark the VM as generalized

若要创建映像,需要解除分配 VM。To create an image, the VM needs to be deallocated. 使用 az vm deallocate 解除分配 VM。Deallocate the VM using az vm deallocate.

az vm deallocate --resource-group myResourceGroup --name myVM

最后,使用 az vm generalize 将 VM 的状态设置为“已通用化”,以便 Azure 平台知道已通用化 VM。Finally, set the state of the VM as generalized with az vm generalize so the Azure platform knows the VM has been generalized. 只能从通用化 VM 创建映像。You can only create an image from a generalized VM.

az vm generalize --resource-group myResourceGroup --name myVM

创建映像Create the image

现在,可使用 az image create 创建 VM 的映像。Now you can create an image of the VM by using az image create. 以下示例从名为“myVM”的 VM 创建名为“myImage”的映像。The following example creates an image named myImage from a VM named myVM.

az image create \
    --resource-group myResourceGroup \
    --name myImage \
    --source myVM

从映像创建 VMCreate VMs from the image

在具有映像后,便可以使用 az vm create 从映像创建一个或多个新 VM。Now that you have an image, you can create one or more new VMs from the image using az vm create. 以下示例从名为“myImage”的映像创建名为“myVMfromImage”的映像。The following example creates a VM named myVMfromImage from the image named myImage.

az vm create \
    --resource-group myResourceGroup \
    --name myVMfromImage \
    --image myImage \
    --admin-username azureuser \
    --generate-ssh-keys

映像管理Image management

下面是一些常见的映像管理任务示例,并说明如何使用 Azure CLI 完成这些任务。Here are some examples of common image management tasks and how to complete them using the Azure CLI.

以表格格式按名称列出所有映像。List all images by name in a table format.

az image list \
    --resource-group myResourceGroup

删除映像。Delete an image. 此示例将从 myResourceGroup 中删除名为 myOldImage 的映像。This example deletes the image named myOldImage from the myResourceGroup.

az image delete \
    --name myOldImage \
    --resource-group myResourceGroup

后续步骤Next steps

在本教程中,已创建自定义 VM 映像。In this tutorial, you created a custom VM image. 你已了解如何:You learned how to:

  • 预配和通用化 VMDeprovision and generalize VMs
  • 创建自定义映像Create a custom image
  • 从自定义映像创建 VMCreate a VM from a custom image
  • 列出订阅中的所有映像List all the images in your subscription
  • 删除映像Delete an image

请转到下一教程,了解高度可用的虚拟机。Advance to the next tutorial to learn about highly available virtual machines.