使用共享映像库创建自定义映像池Use the Shared Image Gallery to create a custom image pool

使用虚拟机配置创建 Azure Batch 池时,需指定一个虚拟机 (VM) 映像,为池中每个计算节点提供操作系统。When you create an Azure Batch pool using the Virtual Machine Configuration, you specify a VM image that provides the operating system for each compute node in the pool. 可以使用受支持的 Azure 市场映像创建虚拟机池,也可以使用共享映像库映像创建自定义映像。You can create a pool of virtual machines either with a supported Azure Marketplace image or create a custom image with a Shared Image Gallery image.

将共享映像库用于自定义映像时,可以控制操作系统类型和配置,以及数据磁盘的类型。When you use the Shared Image Gallery for your custom image, you have control over the operating system type and configuration, as well as the type of data disks. 共享映像可以包含应用程序和引用数据,Batch 池节点预配好后即可使用这些数据。Your Shared Image can include applications and reference data that become available on all the Batch pool nodes as soon as they are provisioned.

也可根据环境的需要创建多个映像版本。You can also have multiple versions of an image as needed for your environment. 在使用某个映像版本创建 VM 时,将使用该映像版本来创建 VM 的新磁盘。When you use an image version to create a VM, the image version is used to create new disks for the VM.

在准备池的计算节点以运行 Batch 工作负荷时,使用共享映像可以节省时间。Using a Shared Image saves time in preparing your pool's compute nodes to run your Batch workload. 可以在每个预配后的计算节点上使用 Azure 市场映像并安装软件,但通常情况下,使用共享映像会更加高效。It's possible to use an Azure Marketplace image and install software on each compute node after provisioning, but using a Shared Image is typically more efficient. 另外,可以为共享映像指定多个副本。这样,当你创建包含多个 VM(600 个 VM 以上)的池时,就会节省创建池的时间。Additionally, you can specify multiple replicas for the Shared Image so when you create pools with many VMs (more than 600 VMs), you'll save time on pool creation.

使用根据方案配置的共享映像有几个优点:Using a Shared Image configured for your scenario can provide several advantages:

  • 在不同的区域中使用相同的映像。Use the same images across the regions. 可以跨不同的区域创建共享映像副本,这样所有的池就可以利用同一映像。You can create Shared Image replicas across different regions so all your pools utilize the same image.
  • 配置操作系统 (OS)。Configure the operating system (OS). 可以自定义映像操作系统磁盘的配置。You can customize the configuration of the image's operating system disk.
  • 预安装应用程序。Pre-install applications. 在 OS 磁盘中预装应用程序,与使用启动任务预配计算节点后再安装应用程序相比,这种方法更加高效,且不容易出错。Pre-installing applications on the OS disk is more efficient and less error-prone than installing applications after provisioning the compute nodes with a start task.
  • 一次复制大量的数据。Copy large amounts of data once. 将静态数据复制到托管映像的数据磁盘,使这些数据成为托管的共享映像的一部分。Make static data part of the managed Shared Image by copying it to a managed image's data disks. 只需执行此操作一次,然后,数据可供池的每个节点使用。This only needs to be done once and makes data available to each node of the pool.
  • 扩大池的大小。Grow pools to larger sizes. 利用共享映像库,可以通过自定义映像和更多的共享映像副本创建更大的池。With the Shared Image Gallery, you can create larger pools with your customized images along with more Shared Image replicas.
  • 性能优于仅使用托管映像作为自定义映像的情况。Better performance than using just a managed image as a custom image. 对于共享映像自定义映像池,达到稳定状态所需的时间最多可以缩短 25%,VM 空闲延迟最多可以缩短 30%。For a Shared Image custom image pool, the time to reach the steady state is up to 25% faster, and the VM idle latency is up to 30% shorter.
  • 进行映像版本控制和分组,以便于管理。Image versioning and grouping for easier management. 映像分组定义包含的信息涉及创建映像的原因、它适用于哪个 OS,以及映像的用法。The image grouping definition contains information about why the image was created, what OS it is for, and information about using the image. 对映像分组可以方便映像的管理。Grouping images allows for easier image management. 有关详细信息,请参阅映像定义For more information, see Image definitions.

先决条件Prerequisites

备注

需要使用 Azure AD 进行身份验证。You need to authenticate using Azure AD. 如果使用共享密钥身份验证,则会出现身份验证错误。If you use shared-key-auth, you will get an authentication error.

  • 一个 Azure Batch 帐户。An Azure Batch account. 若要创建 Batch 帐户,请参阅 Batch 快速入门(使用 Azure 门户Azure CLI)。To create a Batch account, see the Batch quickstarts using the Azure portal or Azure CLI.

  • 共享映像库映像。A Shared Image Gallery image. 若要创建共享映像,需要具有或创建托管映像资源。To create a Shared Image, you need to have or create a managed image resource. 应该基于 VM 的 OS 磁盘快照及其附加的数据磁盘(可选)创建该映像。The image should be created from snapshots of the VM's OS disk and optionally its attached data disks.

备注

如果共享映像与 Batch 帐户不在同一订阅中,则必须注册用于该订阅的 Microsoft.Batch 资源提供程序If the Shared Image is not in the same subscription as the Batch account, you must register the Microsoft.Batch resource provider for that subscription. 两个订阅都必须属于同一 Azure AD 租户。The two subscriptions must be in the same Azure AD tenant.

只要映像的副本与 Batch 帐户位于同一区域,该映像就可以位于不同区域中。The image can be in a different region as long as it has replicas in the same region as your Batch account.

如果使用 Azure AD 应用程序创建具有共享映像库映像的自定义映像池,则必须向该应用程序授予允许其访问共享映像的 Azure 内置角色If you use an Azure AD application to create a custom image pool with a Shared Image Gallery image, that application must have been granted an Azure built-in role that gives it access to the the Shared Image. 可以通过导航到共享映像,选择“访问控制(IAM)”,并为应用程序添加角色分配,在 Azure 门户中授予此访问权限。You can grant this access in the Azure portal by navigating to the Shared Image, selecting Access control (IAM) and adding a role assignment for the application.

准备共享映像Prepare a Shared Image

在 Azure 中,可以基于托管映像准备共享映像,托管映像可通过以下方式创建:In Azure, you can prepare a shared image from a managed image, which can be created 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 仅支持通用共享映像;专用共享映像无法用于创建池。Batch only supports generalized Shared Images; a specialized Shared Image can't be used to create a pool.

以下步骤展示了如何准备 VM、创建快照,然后基于该快照创建映像。The following steps show how to prepare a VM, take a snapshot, and create an 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.
  • 确保 OS 磁盘未加密。Ensure that the OS disk is not encrypted.
  • 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 磁盘快照并有选择性地指定一个或多个数据磁盘快照来创建映像。Create an image by specifying an OS disk snapshot and optionally one or more data disk snapshots.

成功创建托管映像后,需要创建一个共享映像库,以便可以使用自定义映像。Once you have successfully created your managed image, you need to create a Shared Image Gallery to make your custom image available. 若要了解如何为映像创建共享映像库,请参阅使用 Azure CLI 创建共享映像库使用 Azure 门户创建共享映像库To learn how to create a Shared Image Gallery for your images, see Create a Shared Image Gallery with Azure CLI or Create a Shared Image Gallery using the Azure portal.

使用 Azure CLI 通过共享映像创建池Create a pool from a Shared Image using the Azure CLI

若要使用 Azure CLI 从共享映像创建池,请使用 az batch pool create 命令。To create a pool from your Shared Image using the Azure CLI, use the az batch pool create command. --image 字段中指定共享映像 ID。Specify the Shared Image ID in the --image field. 确保 OS 类型和 SKU 与 --node-agent-sku-id 指定的版本匹配Make sure the OS type and SKU matches the versions specified by --node-agent-sku-id

备注

需要使用 Azure AD 进行身份验证。You need to authenticate using Azure AD. 如果使用共享密钥身份验证,则会出现身份验证错误。If you use shared-key-auth, you will get an authentication error.

az batch pool create \
    --id mypool --vm-size Standard_A1_v2 \
    --target-dedicated-nodes 2 \
    --image "/subscriptions/{sub id}/resourceGroups/{resource group name}/providers/Microsoft.Compute/galleries/{gallery name}/images/{image definition name}/versions/{version id}" \
    --node-agent-sku-id "batch.node.ubuntu 16.04"

使用 C# 通过共享映像创建池Create a pool from a Shared Image using C#

或者,可以使用 C# SDK 通过共享映像创建池。Alternatively, you can create a pool from a Shared Image using the C# 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/galleries/{gallery name}/images/{image definition name}/versions/{version id}");
}

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

        pool.Commit();
    }
    ...
}

使用 Python 通过共享映像创建池Create a pool from a Shared Image using Python

还可以使用 Python SDK 通过共享映像创建池:You also can create a pool from a Shared Image by using the Python SDK:

# Import the required modules from the
# Azure Batch Client Library for Python
import azure.batch as batch
import azure.batch.models as batchmodels
from azure.common.credentials import ServicePrincipalCredentials

# Specify Batch account and service principal account credentials
account = "{batch-account-name}"
batch_url = "{batch-account-url}"
ad_client_id = "{sp-client-id}"
ad_tenant = "{tenant-id}"
ad_secret = "{sp-secret}"

# Pool settings
pool_id = "LinuxNodesSamplePoolPython"
vm_size = "STANDARD_D2_V3"
node_count = 1

# Initialize the Batch client with Azure AD authentication
creds = ServicePrincipalCredentials(
    client_id=ad_client_id,
    secret=ad_secret,
    tenant=ad_tenant,
    resource="https://batch.core.chinacloudapi.cn/"
)
client = batch.BatchServiceClient(creds, batch_url)

# Configure the start task for the pool
start_task = batchmodels.StartTask(
    command_line="printenv AZ_BATCH_NODE_STARTUP_DIR"
)
start_task.run_elevated = True

# Create an ImageReference which specifies the image from
# Shared Image Gallery to install on the nodes.
ir = batchmodels.ImageReference(
    virtual_machine_image_id="/subscriptions/{sub id}/resourceGroups/{resource group name}/providers/Microsoft.Compute/galleries/{gallery name}/images/{image definition name}/versions/{version id}"
)

# Create the VirtualMachineConfiguration, specifying
# the VM image reference and the Batch node agent to
# be installed on the node.
vmc = batchmodels.VirtualMachineConfiguration(
    image_reference=ir,
    node_agent_sku_id="batch.node.ubuntu 18.04"
)

# Create the unbound pool
new_pool = batchmodels.PoolAddParameter(
    id=pool_id,
    vm_size=vm_size,
    target_dedicated_nodes=node_count,
    virtual_machine_configuration=vmc,
    start_task=start_task
)

# Create pool in the Batch service
client.pool.add(new_pool)

使用 Azure 门户通过共享映像创建池Create a pool from a Shared Image using the Azure portal

在 Azure 门户中,使用以下步骤从共享映像创建池。Use the following steps to create a pool from a Shared Image in the Azure portal.

  1. 打开 Azure 门户Open the Azure portal.

  2. 转到“Batch 帐户”,然后选择你的帐户。Go to Batch accounts and select your account.

  3. 选择“池”,然后选择“添加”,以便创建新池。 Select Pools and then Add to create a new pool.

  4. 在“映像类型”部分,选择“共享映像库”。 In the Image Type section, select Shared Image Gallery.

  5. 使用托管映像的相关信息完成剩余部分。Complete the remaining sections with information about your managed image.

  6. 选择“确定” 。Select OK.

    通过门户使用共享映像创建池。

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

如果打算使用共享映像创建包含数百或数千 VM 或更多 VM 的池,请按以下指南操作。If you plan to create a pool with hundreds or thousands of VMs or more using a Shared Image, use the following guidance.

  • 共享映像库副本数目。Shared Image Gallery replica numbers. 对于实例数多达 300 的每个池,我们建议至少保留一个副本。For every pool with up to 300 instances, we recommend you keep at least one replica. 例如,如果创建的池包含 3000 个 VM,则应保留至少 10 个映像副本。For example, if you are creating a pool with 3000 VMs, you should keep at least 10 replicas of your image. 我们始终建议保留比最低要求更多的副本,以便获得更好的性能。We always suggest keeping more replicas than minimum requirements for better performance.

  • 调整超时。Resize timeout. 如果池包含固定数目的节点(如果池不会自动缩放),请根据池大小增大池的 resizeTimeout 属性的值。If your pool contains a fixed number of nodes (if it doesn't autoscale), increase the resizeTimeout property of the pool depending on the pool size. 对于每 1000 个 VM,建议的调整超时值至少为 15 分钟。For every 1000 VMs, the recommended resize timeout is at least 15 minutes. 例如,如果池包含 2000 个 VM,建议将重设大小超时设置为至少 30 分钟。For example, the recommended resize timeout for a pool with 2000 VMs is at least 30 minutes.

后续步骤Next steps