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

如何创建虚拟机或 VHD 的托管映像How to create a managed image of a virtual machine or VHD

若要创建虚拟机 (VM) 的多个副本以便在 Azure 中用于开发和测试,请捕获 VM 或 OS VHD 的托管映像。To create multiple copies of a virtual machine (VM) for use in Azure for development and test, capture a managed image of the VM or of the OS VHD. 若要大规模创建、存储和共享映像,请参阅共享映像库To create, store and share images at scale, see Shared Image Galleries.

一个托管映像最多支持 20 个同时部署。One managed image supports up to 20 simultaneous deployments. 尝试从同一托管映像同时创建超过 20 个 VM 可能会由于单个 VHD 的存储性能限制而导致预配超时。Attempting to create more than 20 VMs concurrently, from the same managed image, may result in provisioning timeouts due to the storage performance limitations of a single VHD. 若要同时创建超过 20 个 VM,请使用通过 1 个副本为每 20 个并发 VM 部署配置的共享映像库映像。To create more than 20 VMs concurrently, use a Shared Image Galleries image configured with 1 replica for every 20 concurrent VM deployments.

若要创建托管映像,需要删除个人帐户信息。To create a managed image, you'll need to remove personal account information. 执行下列步骤可取消预配现有 VM、将其解除分配,然后创建映像。In the following steps, you deprovision an existing VM, deallocate it and create an image. 可以使用此映像,在订阅内的任何资源组中创建 VM。You can use this image to create VMs across any resource group within your subscription.

若要创建一份现有 Linux VM 的副本以用于备份或调试,或从本地 VM 上传专用 Linux VHD,请参阅上传自定义磁盘映像并根据该映像创建 Linux VMTo create a copy of your existing Linux VM for backup or debugging, or to upload a specialized Linux VHD from an on-premises VM, see Upload and create a Linux VM from custom disk image.

可以使用 Azure VM 映像生成器(公共预览版)服务来生成自定义映像,无需了解任何工具或设置生成管道,只需提供映像配置,映像生成器即可创建映像。You can use the Azure VM Image Builder (Public Preview) service to build your custom image, no need to learn any tools, or setup build pipelines, simply providing an image configuration, and the Image Builder will create the Image. 有关详细信息,请参阅 Azure VM 映像生成器入门For more information, see Getting Started with Azure VM Image Builder.

创建映像前需要以下项:You'll need the following items before creating an image:

要改用教程吗?Prefer a tutorial instead?

有关本文的用于测试、评估或了解 Azure 中的 VM 的简化版本,请参阅使用 CLI 创建 Azure VM 的自定义映像For a simplified version of this article, and for testing, evaluating, or learning about VMs in Azure, see Create a custom image of an Azure VM by using the CLI. 否则,请继续阅读此处以获取完整的图片。Otherwise, keep reading here to get the full picture.

步骤 1:取消设置 VMStep 1: Deprovision the VM

首先,使用 Azure VM 代理取消预配 VM 以删除计算机特定文件和数据。First you'll deprovision the VM by using the Azure VM agent to delete machine-specific files and data. 在源 Linux VM 上,使用带 -deprovision+user 参数的 waagent 命令。Use the waagent command with the -deprovision+user parameter on your source Linux VM. 有关详细信息,请参阅 Azure Linux 代理用户指南For more information, see the Azure Linux Agent user guide.

  1. 使用 SSH 客户端连接到 Linux VM。Connect to your Linux VM with an SSH client.

  2. 在 SSH 窗口中,输入以下命令:In the SSH window, enter the following command:

    sudo waagent -deprovision+user
    

    备注

    仅在将捕获为映像的 VM 上运行此命令。Only run this command on a VM that you'll capture as an image. 此命令无法保证映像中的所有敏感信息均已清除,或该映像适合再分发。This command does not guarantee that the image is cleared of all sensitive information or is suitable for redistribution. +user 参数还会删除上次预配的用户帐户。The +user parameter also removes the last provisioned user account. 要保留 VM 中的用户帐户凭据,请仅使用 -deprovisionTo keep user account credentials in the VM, use only -deprovision.

  3. y 继续。Enter y to continue. 添加 -force 参数即可免除此确认步骤。You can add the -force parameter to avoid this confirmation step.

  4. 该命令完成后,请输入“退出”以关闭 SSH 客户端。After the command completes, enter exit to close the SSH client. VM 在此时仍会运行。The VM will still be running at this point.

步骤 2:创建 VM 映像Step 2: Create VM image

使用 Azure CLI 将 VM 标记为通用化并捕获映像。Use the Azure CLI to mark the VM as generalized and capture the image. 在以下示例中,请将示例参数名称替换为自己的值。In the following examples, replace example parameter names with your own values. 示例参数名称包括 myResourceGroupmyVnetmyVMExample parameter names include myResourceGroup, myVnet, and myVM.

  1. 对使用 az vm deallocate 取消设置的 VM 解除分配。Deallocate the VM that you deprovisioned with az vm deallocate. 以下示例在名为 myResourceGroup 的资源组中释放名为 myVM 的 VM 。The following example deallocates the VM named myVM in the resource group named myResourceGroup.

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

    等待 VM 完全解除分配,然后继续。Wait for the VM to completely deallocate before moving on. 完成此命令可能需要几分钟。This may take a few minutes to complete. VM 在解除分配期间关闭。The VM is shut down during deallocation.

  2. 使用 az vm generalize 将 VM 标记为通用化。Mark the VM as generalized with az vm generalize. 以下示例将名为 myResourceGroup 的资源组中名为 myVM 的 VM 标记为通用化 。The following example marks the VM named myVM in the resource group named myResourceGroup as generalized.

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

    已通用化的 VM 无法再重新启动。A VM that has been generalized can no longer be restarted.

  3. 使用 az image create 创建 VM 资源的映像。Create an image of the VM resource with az image create. 以下示例使用名为 myVM 的 VM 资源在名为 myResourceGroup 的资源组中创建名为 myImage 的映像 。The following example creates an image named myImage in the resource group named myResourceGroup using the VM resource named myVM.

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

    备注

    该映像在与源 VM 相同的资源组中创建。The image is created in the same resource group as your source VM. 可以在订阅内的任何资源组中从此映像创建虚拟机。You can create VMs in any resource group within your subscription from this image. 从管理角度来看,你可能希望为 VM 资源和映像创建特定的资源组。From a management perspective, you may wish to create a specific resource group for your VM resources and images.

    如果希望将映像存储在具有区域复原能力的存储中,需要在支持可用性区域的区域中创建该快照并包括 --zone-resilient true 参数。If you would like to store your image in zone-resilient storage, you need to create it in a region that supports availability zones and include the --zone-resilient true parameter.

此命令返回描述 VM 映像的 JSON。This command returns JSON that describes the VM image. 保存此输出以供日后参考。Save this output for later reference.

步骤 3:从捕获的映像创建 VMStep 3: Create a VM from the captured image

使用通过 az vm create 创建的映像来创建 VM。Create a VM by using the image you created with az vm create. 以下示例从名为 myImage 的映像创建名为 myVMDeployed 的 VM 。The following example creates a VM named myVMDeployed from the image named myImage.

az vm create \
   --resource-group myResourceGroup \
   --name myVMDeployed \
   --image myImage\
   --admin-username azureuser \
   --ssh-key-value ~/.ssh/id_rsa.pub

在另一个资源组中创建 VMCreating the VM in another resource group

可在订阅内的任何资源组中根据映像创建 VM。You can create VMs from an image in any resource group within your subscription. 要在与映像不同的资源组中创建 VM,请指定映像的完整资源 ID。To create a VM in a different resource group than the image, specify the full resource ID to your image. 使用 az image list 查看映像列表。Use az image list to view a list of images. 输出类似于以下示例。The output is similar to the following example.

"id": "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage",
   "location": "westus",
   "name": "myImage",

以下示例使用 az vm create,通过指定映像资源 ID,在与源映像不同的资源组中创建 VM。The following example uses az vm create to create a VM in a resource group other than the source image, by specifying the image resource ID.

az vm create \
   --resource-group myOtherResourceGroup \
   --name myOtherVMDeployed \
   --image "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage" \
   --admin-username azureuser \
   --ssh-key-value ~/.ssh/id_rsa.pub

步骤 4:验证部署Step 4: Verify the deployment

将 SSH 连接到创建的虚拟机以验证部署并开始使用新的 VM。SSH into the virtual machine you created to verify the deployment and start using the new VM. 要通过 SSH 连接,请使用 az vm show 查找 VM 的 IP 地址或 FQDN。To connect via SSH, find the IP address or FQDN of your VM with az vm show.

az vm show \
   --resource-group myResourceGroup \
   --name myVMDeployed \
   --show-details

后续步骤Next steps

若要大规模创建、存储和共享映像,请参阅共享映像库To create, store and share images at scale, see Shared Image Galleries.