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

使用托管映像创建虚拟机池Use a managed image to create a pool of virtual machines

若要为 Batch 池的虚拟机(Vm)创建自定义映像,可以使用共享映像库托管映像资源。To create a custom image for your Batch pool's virtual machines (VMs), you can use either the Shared Image Gallery, or a managed image resource.

提示

在大多数情况下,应使用共享映像库创建自定义映像。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.

必备组件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. 有关准备托管映像的详细信息和步骤,请参阅以下部分。For more information and steps to prepare a managed image, see the following section.

    • 对创建的每个池使用唯一的自定义映像。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. 若要使用门户,请使用映像的名称To use the portal, use the name of the image.
    • 托管映像资源应该在池的生存期内存在,以便能够纵向扩展,并可在删除池后将其删除。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 (AAD) 身份验证Azure Active Directory (AAD) authentication. Batch 客户端 API 必须使用 AAD 身份验证。The Batch client API must use AAD authentication. 有关 Azure Batch 对 AAD 的支持,请参阅使用 Active Directory 对 Batch 服务解决方案进行身份验证Azure Batch support for AAD is documented in Authenticate Batch service solutions with Active Directory.

准备自定义映像Prepare a custom 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
  • 已将通用本地 VHD 上传到云A generalized on-premises VHD uploaded to the cloud

若要使用自定义映像可靠缩放 Batch 池,我们建议仅使用第一种方法创建托管映像:使用 VM 磁盘的快照。To scale Batch pools reliably with a custom image, we recommend creating a managed image using only the first method: using snapshots of the VM's disks. 参阅以下步骤来准备 VM、创建快照,然后基于该快照创建映像。See the following steps to prepare a VM, take a snapshot, and create an image from the snapshot.

准备 VMPrepare a VM

如果要为映像创建新的 VM,请使用 Batch 支持的第一方 Azure Marketplace 映像作为托管映像的基础映像。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 Marketplace 映像引用的完整列表,请参阅列出节点代理 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.

在 Azure 门户中使用自定义映像创建池Create a pool from a custom image in the portal

如果已保存自定义映像,并且知道其资源 ID 或名称,请基于该映像创建 Batch 池。Once you have saved your custom image and you know its resource ID or name, create a Batch pool from that image. 以下步骤说明如何从 Azure 门户创建池。The following steps show you how to create a pool from the Azure portal.

备注

若要使用某个 Batch API 创建池,请确保用于 AAD 身份验证的标识对映像资源拥有权限。If you are creating the pool using one of the Batch APIs, make sure that the identity you use for AAD 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.

  1. 导航到 Azure 门户中的批处理帐户。Navigate to your Batch account in the Azure portal. 此帐户必须与包含自定义映像的资源组在同一订阅和区域中。This account must be in the same subscription and region as the resource group containing the custom image.

  2. 在左侧的“设置”窗口中,选择“池”菜单项。In the Settings window on the left, select the Pools menu item.

  3. 在“池”窗口中,选择“添加”命令。In the Pools window, select the Add command.

  4. 在“添加池”窗口中,从“映像类型”下拉列表中选择“自定义映像(Linux/Windows)”。On the Add Pool window, select Custom Image (Linux/Windows) from the Image Type dropdown. 在“自定义 VM 映像”下拉列表中,选择映像名称(资源 ID 的短格式)。From the Custom VM image dropdown, select the image name (short form of the resource ID).

  5. 为自定义映像选择正确的“发布服务器/产品/SKU”。Select the correct Publisher/Offer/Sku for your custom image.

  6. 指定剩余的必需设置,包括节点大小目标专用节点低优先级节点,以及任何所需的可选设置。Specify the remaining required settings, including the Node size, Target dedicated nodes, and Low-priority nodes, as well as any desired optional settings.

    例如,对于 Microsoft Windows Server Datacenter 2016 自定义映像,会显示“添加池”窗口,如下所示:For example, for a Microsoft Windows Server Datacenter 2016 custom image, the Add Pool window appears as shown below:

    从自定义 Windows 映像添加池

要检查现有池是否基于自定义映像,请查看“池”窗口的资源摘要部分中的“操作系统”属性。To check whether an existing pool is based on a custom image, see the Operating System property in the resource summary section of the Pool window. 如果池是从自定义映像创建的,该属性会设置为“自定义 VM 映像”。If the pool was created from a custom image, it is set to Custom VM Image.

与池关联的所有自定义映像已显示在池的“属性”窗口中。All custom images associated with a pool are displayed on the pool's Properties window.

大型池的注意事项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 个专用计算节点,或 1000 个低优先级节点。Size limits - Batch limits the pool size to 2500 dedicated compute nodes, or 1000 low-priority 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

只能使用用户订阅模式批处理帐户来创建直接使用 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. 根据池分配模式(用户订阅或批处理服务),创建托管映像资源的步骤会有所不同。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

有关 Batch 深入概述的信息,请参阅使用 Batch 开发大规模并行计算解决方案For an in-depth overview of Batch, see Develop large-scale parallel compute solutions with Batch.