使用托管映像创建自定义映像池Use a managed image to create a custom image pool

若要为 Batch 池的虚拟机 (VM) 创建自定义映像池,可以使用托管映像创建共享映像库映像To create a custom image pool for your Batch pool's virtual machines (VMs), you can use a managed image to create a Shared Image Gallery image. 还支持只使用托管映像,但仅适用于最高 2019-08-01(含)的 API 版本。Using just a managed image is also supported, but only for API versions up to and including 2019-08-01.

重要

在大多数情况下,应使用共享映像库创建自定义映像。In most cases, you should create custom images using the Shared Image Gallery. 使用共享映像库可以更快地预配池、缩放更大数量的 VM 以及在预配 VM 时提高可靠性。By using the Shared Image Gallery, you can provision pools faster, scale larger quantities of VMs, and have improved reliability when provisioning VMs. 若要了解详细信息,请参阅使用共享映像库创建自定义池To learn more, see Use the Shared Image Gallery to create a custom pool.

本主题说明如何只使用托管映像创建自定义映像池。This topic explains how to create a custom image pool using only a managed image.

先决条件Prerequisites

  • 托管映像资源A managed image resource . 若要使用自定义映像创建虚拟机池,需在 Batch 帐户所在的同一 Azure 订阅和区域中使用或创建托管映像资源。To create a pool of virtual machines using a custom image, you need to have or create a managed image resource in the same Azure subscription and region as the Batch account. 应该基于 VM 的 OS 磁盘快照及其附加的数据磁盘(可选)创建该映像。The image should be created from snapshots of the VM's OS disk and optionally its attached data disks.

    • 对创建的每个池使用唯一的自定义映像。Use a unique custom image for each pool you create.
    • 若要使用 Batch API 创建包含映像的池,请指定映像的 资源 ID ,其格式为 /subscriptions/xxxx-xxxxxx-xxxxx-xxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myImageTo create a pool with the image using the Batch APIs, specify the resource ID of the image, which is of the form /subscriptions/xxxx-xxxxxx-xxxxx-xxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myImage.
    • 托管映像资源应该在池的生存期内存在,以便能够纵向扩展,并可在删除池后将其删除。The managed image resource should exist for the lifetime of the pool to allow scale-up and can be removed after the pool is deleted.
  • Azure Active Directory (Azure AD) 身份验证。Azure Active Directory (Azure AD) authentication . Batch 客户端 API 必须使用 Azure AD 身份验证。The Batch client API must use Azure AD authentication. 有关 Azure AD 的 Azure Batch 支持,请参阅使用 Active Directory 对 Batch 服务解决方案进行身份验证Azure Batch support for Azure AD is documented in Authenticate Batch service solutions with Active Directory.

准备托管映像Prepare a managed image

在 Azure 中,可以通过以下项准备托管映像:In Azure, you can prepare a managed image from:

  • Azure VM 的 OS 和数据磁盘快照Snapshots of an Azure VM's OS and data disks
  • 带托管磁盘的通用 Azure VMA generalized Azure VM with managed disks
  • 已上传到云的通用本地 VHDA generalized on-premises VHD uploaded to the cloud

若要使用托管映像可靠地缩放 Batch 池,建议仅使用第一种方法创建托管映像:使用 VM 磁盘的快照。To scale Batch pools reliably with a managed image, we recommend creating the managed image using only the first method: using snapshots of the VM's disks. 以下步骤展示了如何准备 VM、创建快照,然后基于该快照创建托管映像。The following steps show how to prepare a VM, take a snapshot, and create a managed image from the snapshot.

准备 VMPrepare a VM

若要为映像创建新 VM,请使用 Batch 支持的第一方 Azure 市场映像作为托管映像的基础映像。If you are creating a new VM for the image, use a first party Azure Marketplace image supported by Batch as the base image for your managed image. 仅第一方映像可以用作基础映像。Only first party images can be used as a base image. 若要获取 Azure Batch 支持的 Azure 市场映像参考的完整列表,请参阅列出节点代理 SKU 操作。To get a full list of Azure Marketplace image references supported by Azure Batch, see the List node agent SKUs operation.

备注

不能使用具有附加许可和购买条款的第三方映像作为基础映像。You can't use a third-party image that has additional license and purchase terms as your base image. 有关这些市场映像的信息,请参阅 LinuxWindows VM 指南。For information about these Marketplace images, see the guidance for Linux or Windows VMs.

  • 确保使用托管磁盘创建 VM。Ensure the VM is created with a managed disk. 这是创建 VM 时的默认存储设置。This is the default storage setting when you create a VM.
  • 不要在 VM 上安装自定义脚本扩展等 Azure 扩展。Do not install Azure extensions, such as the Custom Script extension, on the VM. 如果映像包含预装的扩展,在部署 Batch 池时 Azure 可能会遇到问题。If the image contains a pre-installed extension, Azure may encounter problems when deploying the Batch pool.
  • 使用附加的数据磁盘时,需要从 VM 中装载和格式化磁盘,才能使用它们。When using attached data disks, you need to mount and format the disks from within a VM to use them.
  • 确保所提供的基础 OS 映像使用默认临时驱动器。Ensure that the base OS image you provide uses the default temp drive. Batch 节点代理目前需要使用默认的临时驱动器。The Batch node agent currently expects the default temp drive.
  • VM 开始运行后,请通过 RDP(适用于 Windows)或 SSH(适用于 Linux)进行连接。Once the VM is running, connect to it via RDP (for Windows) or SSH (for Linux). 安装所需的任何软件,或复制所需的数据。Install any necessary software or copy desired data.

创建 VM 快照Create a VM snapshot

快照是 VHD 的完整只读副本。A snapshot is a full, read-only copy of a VHD. 若要创建 VM OS 磁盘或数据磁盘的快照,可以使用 Azure 门户或命令行工具。To create a snapshot of a VM's OS or data disks, you can use the Azure portal or command-line tools. 有关创建快照的步骤和选项,请参阅适用于 LinuxWindows VM 的指导。For steps and options to create a snapshot, see the guidance for Linux or Windows VMs.

基于一个或多个快照创建映像Create an image from one or more snapshots

若要基于快照创建托管映像,请使用 Azure 命令行工具,例如 az image create 命令。To create a managed image from a snapshot, use Azure command-line tools such as the az image create command. 可以通过指定 OS 磁盘快照并选择性地指定一个或多个数据磁盘快照来创建映像。You can create an image by specifying an OS disk snapshot and optionally one or more data disk snapshots.

从托管映像创建池Create a pool from a managed image

找到托管映像的资源 ID 后,便可通过该映像创建自定义映像池。Once you have found the resource ID of your managed image, create a custom image pool from that image. 以下步骤演示如何使用 Batch 服务或 Batch 管理创建自定义映像池。The following steps show you how to create a custom image pool using either Batch Service or Batch Management.

备注

确保用于 Azure AD 身份验证的标识对映像资源拥有权限。Make sure that the identity you use for Azure AD authentication has permissions to the image resource. 请参阅使用 Active Directory 对 Batch 服务解决方案进行身份验证See Authenticate Batch service solutions with Active Directory.

托管映像的资源必须在池的生存期内存在。The resource for the managed image must exist for the lifetime of the pool. 如果删除了基础资源,则无法缩放池。If the underlying resource is deleted, the pool cannot be scaled.

Batch 服务 .NET SDKBatch Service .NET SDK

private static VirtualMachineConfiguration CreateVirtualMachineConfiguration(ImageReference imageReference)
{
    return new VirtualMachineConfiguration(
        imageReference: imageReference,
        nodeAgentSkuId: "batch.node.windows amd64");
}

private static ImageReference CreateImageReference()
{
    return new ImageReference(
        virtualMachineImageId: "/subscriptions/{sub id}/resourceGroups/{resource group name}/providers/Microsoft.Compute/images/{image definition name}");
}

private static void CreateBatchPool(BatchClient batchClient, VirtualMachineConfiguration vmConfiguration)
{
    try
    {
        CloudPool pool = batchClient.PoolOperations.CreatePool(
            poolId: PoolId,
            targetDedicatedComputeNodes: PoolNodeCount,
            virtualMachineSize: PoolVMSize,
            virtualMachineConfiguration: vmConfiguration);

        pool.Commit();
    }

Batch 管理 REST APIBatch Management REST API

REST API URIREST API URI

 PUT https://management.chinacloudapi.cn/subscriptions/{sub id}/resourceGroups/{resource group name}/providers/Microsoft.Batch/batchAccounts/{account name}/pools/{pool name}?api-version=2020-03-01

请求正文Request Body

 {
   "properties": {
     "vmSize": "{VM size}",
     "deploymentConfiguration": {
       "virtualMachineConfiguration": {
         "imageReference": {
           "id": "/subscriptions/{sub id}/resourceGroups/{resource group name}/providers/Microsoft.Compute/images/{image name}"
         },
         "nodeAgentSkuId": "{Node Agent SKU ID}"
       }
     }
   }
 }

大型池的注意事项Considerations for large pools

如果你打算使用自定义映像创建包含数百个或更多 VM 的池,必须遵照前面的指导使用基于 VM 快照创建的映像。If you plan to create a pool with hundreds of VMs or more using a custom image, it is important to follow the preceding guidance to use an image created from a VM snapshot.

另请注意以下注意事项:Also note the following considerations:

  • 大小限制 - 当你使用自定义映像时,Batch 会将池大小限制为 2500 个专用计算节点。Size limits - Batch limits the pool size to 2500 dedicated compute nodes, when you use a custom image.

    如果使用相同的映像(或基于同一基础快照的多个映像)来创建多个池,则池中的计算节点总数不能超过上述限制。If you use the same image (or multiple images based on the same underlying snapshot) to create multiple pools, the total compute nodes in the pools can't exceed the preceding limits. 不建议将某个映像或其基础快照用于多个池。We don't recommend using an image or its underlying snapshot for more than a single pool.

    如果使用入站 NAT 池来配置池,可以降低限制。Limits may be reduced if you configure the pool with inbound NAT pools.

  • 调整超时 - 如果池包含固定数目的节点(不会自动缩放),请增大 resizeTimeout 属性的值,例如 20-30 分钟。Resize timeout - If your pool contains a fixed number of nodes (doesn't autoscale), increase the resizeTimeout property of the pool to a value such as 20-30 minutes. 如果在超时期限内池未达到其目标大小,请再次执行调整大小操作If your pool doesn't reach its target size within the timeout period, perform another resize operation.

    如果你打算创建包含 300 个以上的计算节点的池,可能需要多次调整池大小才能达到目标大小。If you plan a pool with more than 300 compute nodes, you might need to resize the pool multiple times to reach the target size.

使用共享映像库可以通过自定义映像和更多的共享映像副本创建更大的池。By using the Shared Image Gallery, you can create larger pools with your customized images along with more Shared Image replicas. 使用共享映像,池达到稳定状态所用的时间最多可加快 25%,并且 VM 空闲延迟时间最多可缩短 30%。Using Shared Images, the time it takes for the pool to reach the steady state is up to 25% faster, and the VM idle latency is up to 30% shorter.

使用 Packer 的注意事项Considerations for using Packer

只能通过用户订阅模式 Batch 帐户来直接使用 Packer 创建托管映像资源。Creating a managed image resource directly with Packer can only be done with user subscription mode Batch accounts. 对于 Batch 服务模式帐户,需要首先创建 VHD,然后将 VHD 导入托管映像资源。For Batch service mode accounts, you need to create a VHD first, then import the VHD to a managed image resource. 根据池分配模式(用户订阅或 Batch 服务),创建托管映像资源的步骤会有所不同。Depending on your pool allocation mode (user subscription, or Batch service), your steps to create a managed image resource will vary.

确保用于创建托管映像的资源在引用自定义映像的任何池的生存期内存在。Ensure that the resource used to create the managed image exists for the lifetimes of any pool referencing the custom image. 否则可能会导致池分配失败和/或重设大小失败。Failure to do so can result in pool allocation failures and/or resize failures.

如果删除了图像或基础资源,则可能会收到类似于以下内容的错误:There was an error encountered while performing the last resize on the pool. Please try resizing the pool again. Code: AllocationFailedIf the image or the underlying resource is removed, you may get an error similar to: There was an error encountered while performing the last resize on the pool. Please try resizing the pool again. Code: AllocationFailed. 如果收到此错误,请确保未删除基础资源。If you get this error, ensure that the underlying resource has not been removed.

有关使用 Packer 创建 VM 的详细信息,请参阅使用 Packer 构建 Linux 映像使用 Packer 构建 Windows 映像For more information on using Packer to create a VM, see Build a Linux image with Packer or Build a Windows image with Packer.

后续步骤Next steps