使用 Batch 应用程序包将应用程序部署到计算节点Deploy applications to compute nodes with Batch application packages

应用程序包可以简化 Azure Batch 解决方案中的代码,使你可以更轻松地管理你的任务运行的应用程序。Application packages can simplify the code in your Azure Batch solution and make it easier to manage the applications that your tasks run. 使用应用程序包可以上传和管理任务运行的多个应用程序版本,包括其支持文件。With application packages, you can upload and manage multiple versions of applications your tasks run, including their supporting files. 然后,可以将一个或多个此类应用程序自动部署到池中的计算节点。You can then automatically deploy one or more of these applications to the compute nodes in your pool.

用于创建和管理应用程序包的 API 属于 Batch Management .NET 库。The APIs for creating and managing application packages are part of the Batch Management .NET library. 用于在计算节点上安装应用程序包的 API 属于 Batch .NET 库。The APIs for installing application packages on a compute node are part of the Batch .NET library. 其他语言的可用 Batch API 中提供了对应的功能。Comparable features are in the available Batch APIs for other languages.

本文介绍如何在 Azure 门户中上传和管理应用程序包。This article explains how to upload and manage application packages in the Azure portal. 另外还介绍如何使用 Batch .NET 库将应用程序包安装到池的计算节点上。It also shows how to install them on a pool's compute nodes with the Batch .NET library.

应用程序包要求Application package requirements

要使用应用程序包,需要将 Azure 存储帐户链接到批处理帐户。To use application packages, you need to link an Azure Storage account to your Batch account.

批处理帐户中的应用程序和应用程序包数目,以及应用程序包的大小上限有其限制。There are restrictions on the number of applications and application packages within a Batch account and on the maximum application package size. 有关详细信息,请参阅 Azure Batch 服务的配额和限制For more information, see Quotas and limits for the Azure Batch service.

备注

在 2017 年 7 月 5 日以前创建的 Batch 池不支持应用程序包(除非这些池是在 2016 年 3 月 10 日之后使用云服务配置创建的)。Batch pools created prior to July 5, 2017 do not support application packages (unless they were created after March 10, 2016 using Cloud Services Configuration). 此处所述的应用程序包功能替换了旧版服务中的“批处理应用”功能。The application packages feature described here supersedes the Batch Apps feature available in previous versions of the service.

了解应用程序和应用程序包Understand applications and application packages

在 Azure Batch 中,应用程序是指一组已创建版本的二进制文件,这些文件可自动下载到池中的计算节点。Within Azure Batch, an application refers to a set of versioned binaries that can be automatically downloaded to the compute nodes in your pool. 一个应用程序包含一个或多个应用程序包,这些包代表应用程序的不同版本。An application contains one or more application packages , which represent different versions of the application.

每个应用程序包都是一个 .zip 文件,其中包含应用程序二进制文件和任何支持文件。Each application package is a .zip file that contains the application binaries and any supporting files. 只支持 .zip 格式。Only the .zip format is supported.

此图显示了应用程序和应用程序包的概要视图。

可以在池或任务级别指定应用程序包。You can specify application packages at the pool or task level.

  • 池应用程序包 部署到池中的每个节点。Pool application packages are deployed to every node in the pool. 当节点加入池以及重新启动或重置映像时,就会部署应用程序。Applications are deployed when a node joins a pool, and when it is rebooted or reimaged.

    当池中的所有节点都将执行作业的任务时,便适合使用池应用程序包。Pool application packages are appropriate when all nodes in a pool will execute a job's tasks. 在创建池时,可以指定一个或多个要部署的应用程序包。You can specify one or more application packages to deploy when you create a pool. 还可以添加或更新现有池的包。You can also add or update an existing pool's packages. 若要将新包安装到现有池,必须重启其节点。To install a new package to an existing pool, you must restart its nodes.

  • 在运行任务的命令行之前, 任务应用程序包 只部署到计划要运行任务的计算节点。Task application packages are deployed only to a compute node scheduled to run a task, just before running the task's command line. 如果节点上已有指定的应用程序包和版本,则不会重新部署,而是使用现有包。If the specified application package and version is already on the node, it is not redeployed and the existing package is used.

    任务应用程序包在共享池环境(其中,不同的作业在一个池中运行,一个作业完成时并不删除该池)中很有用。Task application packages are useful in shared-pool environments, where different jobs run on one pool, and the pool isn't deleted when a job completes. 如果作业中的任务数小于池中的节点数,任务应用程序包可以尽量减少数据传输,因为应用程序只部署到运行任务的节点。If your job has fewer tasks than nodes in the pool, task application packages can minimize data transfer, since your application is deployed only to the nodes that run tasks.

    其他可受益于任务应用程序包的方案为,运行大型应用程序但只执行少数任务的作业。Other scenarios that can benefit from task application packages are jobs that run a large application, but for only a few tasks. 例如,任务应用程序可以用于工作负荷较重的预处理阶段或合并任务。For example, task applications may be useful for a heavyweight pre-processing stage or a merge task.

有了应用程序包,池的启动任务不需要指定在节点上安装一长串的单个资源文件。With application packages, your pool's start task doesn't have to specify a long list of individual resource files to install on the nodes. 不需要在 Azure 存储中或在节点上手动管理应用程序的多个版本。You don't have to manually manage multiple versions of your application files in Azure Storage, or on your nodes. 而且,也不必费心生成 SAS URL 来提供对你的存储帐户中的文件的访问权限。And you don't need to worry about generating SAS URLs to provide access to the files in your Storage account. Batch 在后台与 Azure 存储协作来存储应用程序包,并将其部署到计算节点。Batch works in the background with Azure Storage to store application packages and deploy them to compute nodes.

备注

启动任务的总大小必须小于或等于 32768 个字符,其中包括资源文件和环境变量。The total size of a start task must be less than or equal to 32768 characters, including resource files and environment variables. 如果你的启动任务超出此限制,备选方案就是使用应用程序包。If your start task exceeds this limit, using application packages is another option. 还可以创建一个包含资源文件的 .zip 文件,将它作为 blob 上传到 Azure 存储,然后从启动任务的命令行中对该文件进行解压缩。You can also create a .zip file containing your resource files, upload it as a blob to Azure Storage, and then unzip it from the command line of your start task.

上传和管理应用程序Upload and manage applications

可以使用 Azure 门户或 Batch 管理 API 来管理 Batch 帐户中的应用程序包。You can use the Azure portal or the Batch Management APIs to manage the application packages in your Batch account. 以下部分介绍如何在 Azure 门户中关联存储帐户,以及如何添加和管理应用程序和应用程序包。The following sections explain how to link a storage account, and how to add and manage applications and application packages in the Azure portal.

若要使用应用程序包,必须将 Azure 存储帐户关联到 Batch 帐户。To use application packages, you must link an Azure Storage account to your Batch account. Batch 服务将使用关联的存储帐户存储应用程序包。The Batch service will use the associated storage account to store your application packages. 建议专门创建一个与 Batch 帐户配合使用的存储帐户。We recommend that you create a storage account specifically for use with your Batch account.

如果尚未配置存储帐户,第一次选择 Batch 帐户中的“应用程序”时,Azure 门户会显示警告。If you have not yet configured a storage account, the Azure portal displays a warning the first time you select Applications in your Batch account. 若要将存储帐户关联到 Batch 帐户,请选择“警告”窗口中的“存储帐户”,然后再次选择“存储帐户”。 To link a storage account to your Batch account, select Storage account on the Warning window, and then select Storage Account again.

链接两个帐户后,Batch 便能将存储在链接之存储帐户中的包自动部署到计算节点。After you've linked the two accounts, Batch can automatically deploy the packages stored in the linked Storage account to your compute nodes.

重要

不能将应用程序包与配置了防火墙规则的 Azure 存储帐户配合使用,或与已设置为“启用”的“分层命名空间”配合使用。 You can't use application packages with Azure Storage accounts configured with firewall rules, or with Hierarchical namespace set to Enabled .

Batch 服务使用 Azure 存储将应用程序包存储为块 blob。The Batch service uses Azure Storage to store your application packages as block blobs. 块 blob 数据正常收费,且每个包的大小不能超过最大块 blob 大小。You are charged as normal for the block blob data, and the size of each package can't exceed the maximum block blob size. 有关详细信息,请参阅存储帐户的 Azure 存储可伸缩性和性能目标For more information, see Azure Storage scalability and performance targets for storage accounts. 为了最大限度地降低成本,请务必考虑应用程序包的大小和数目,并定期删除弃用的包。To minimize costs, be sure to consider the size and number of your application packages, and periodically remove deprecated packages.

查看当前应用程序View current applications

若要查看 Batch 帐户中的应用程序,请在左侧导航菜单中选择“应用程序”。To view the applications in your Batch account, select Applications in the left navigation menu.

Azure 门户中的“应用程序”菜单项的屏幕截图。

选择此菜单选项可打开“应用程序”窗口。Selecting this menu option opens the Applications window. 此窗口显示帐户中每个应用程序的 ID,以及以下属性:This window displays the ID of each application in your account and the following properties:

  • :与此应用程序关联的版本号。Packages : The number of versions associated with this application.
  • 默认版本 :如果在部署应用程序时未指定版本,则此项为将要安装的应用程序版本(如果适用)。Default version : If applicable, the application version that will be installed if no version is specified when deploying the application.
  • 允许更新 :指定是否允许更新和删除包。Allow updates : Specifies whether package updates and deletions are allowed.

若要查看计算节点上应用程序包的文件结构,请导航到 Azure 门户中的 Batch 帐户。To see the file structure of the application package on a compute node, navigate to your Batch account in the Azure portal. 选择“池”。Select Pools . 然后选择包含计算节点的池。then select the pool that contains the compute node. 选择在其上安装应用程序包的计算节点,打开 applications 文件夹。Select the compute node on which the application package is installed and open the applications folder.

查看应用程序详细信息View application details

若要查看某个应用程序的详细信息,请在“应用程序”窗口中选择它。To see the details for an application, select it in the Applications window. 可以配置应用程序的以下设置。You can configure the following settings for your application.

  • 允许更新 :指示能否 更新或删除应用程序包。Allow updates : Indicates whether application packages can be updated or deleted. 默认值为“是”。The default is Yes . 如果设置为“否”,则不能更新或删除现有的应用程序包,但仍可添加新的应用程序包版本。If set to No , existing application packages can't be updated or deleted, but new application package versions can still be added.
  • 默认版本 :部署应用程序时要使用的默认应用程序包(如果未指定任何版本)。Default version : The default application package to use when the application is deployed, if no version is specified.
  • 显示名称 :Batch 解决方案在显示应用程序相关信息时可以使用的易记名称。Display name : A friendly name that your Batch solution can use when it displays information about the application. 例如,在通过 Batch 提供给客户的服务的 UI 中使用的名称。For example, this name can be used in the UI of a service that you provide to your customers through Batch.

添加新的应用程序Add a new application

若要创建新应用程序,请添加应用程序包并指定唯一的应用程序 ID。To create a new application, you add an application package and specify a unique application ID.

在 Batch 帐户中,选择“应用程序”,然后选择“添加”。 In your Batch account, select Applications and then select Add .

在 Azure 门户中新建应用程序的过程的屏幕截图。

输入以下信息:Enter the following information:

  • 应用程序 ID :新应用程序的 ID。Application ID : The ID of your new application.
  • 版本 :要上传的应用程序包的版本。Version ": The version for the application package you are uploading.
  • 应用程序包:.zip 文件,其中包含应用程序二进制文件和执行应用程序所需的支持文件。Application package : The .zip file containing the application binaries and supporting files that are required to execute the application.

输入的 应用程序 ID版本 必须满足以下要求:The Application ID and Version you enter must follow these requirements:

  • 在 Windows 节点上,ID 可以包含字母数字字符、连字符和下划线的任意组合。On Windows nodes, the ID can contain any combination of alphanumeric characters, hyphens, and underscores. 在 Linux 节点上,仅允许使用字母数字字符和下划线。On Linux nodes, only alphanumeric characters and underscores are permitted.
  • 包含的字符不能超过 64 个。Can't contain more than 64 characters.
  • 在 Batch 帐户内必须唯一。Must be unique within the Batch account.
  • ID 保留大小写,但不区分大小写。IDs are case-preserving and case-insensitive.

准备就绪后,请选择“提交”。When you're ready, select Submit . 将 .zip 文件上传到 Azure 存储帐户后,门户会显示一个通知。After the .zip file has been uploaded to your Azure Storage account, the portal displays a notification. 这可能需要一些时间,具体取决于上传文件的大小和网络连接的速度。Depending on the size of the file that you are uploading and the speed of your network connection, this may take some time.

添加新应用程序包Add a new application package

若要为现有应用程序添加应用程序包版本,请在 Batch 帐户的“应用程序”部分选择该应用程序,然后选择“添加”。To add an application package version for an existing application, select the application in the Applications section of your Batch account, then select Add .

如前面针对新应用程序所做的那样,指定新包的“版本”,在“应用程序包”字段中上传 .zip 文件,然后选择“提交” 。As you did for the new application, specify the Version for your new package, upload your .zip file in the Application package field, and then select Submit .

更新或删除应用程序包Update or delete an application package

若要更新或删除现有的应用程序包,请在 Batch 帐户的“应用程序”部分选择该应用程序。To update or delete an existing application package, select the application in the Applications section of your Batch account. 选择要修改的应用程序包行中的省略号,然后选择要执行的操作。Select the ellipsis in the row of the application package that you want to modify, then select the action that you want to perform.

屏幕截图,显示 Azure 门户中应用程序包的更新和删除选项。

如果选择“更新”,则可以上传新的 .zip 文件。If you select Update , you'll be able to upload a new .zip file. 这将替换已为该版本上传的上一个 .zip 文件。This will replace the previous .zip file that you uploaded for that version.

如果你选择“删除”,系统会提示你确认是否要删除该版本。If you select Delete , you'll be prompted to confirm the deletion of that version. 选择“确定”后,Batch 会从你的 Azure 存储帐户中删除该 .zip 文件。Once you select OK , Batch will delete the .zip file from your Azure Storage account. 如果删除应用程序的默认版本,则该应用程序的“默认版本”设置会被删除。If you delete the default version of an application, the Default version setting is removed for that application.

将应用程序安装在计算节点上Install applications on compute nodes

你了解如何在 Azure 门户中管理应用程序包之后,我们就可以介绍如何将应用程序包部署到计算节点并使用 Batch 任务运行它们。Now that you've learned how to manage application packages in the Azure portal, we can discuss how to deploy them to compute nodes and run them with Batch tasks.

安装池应用程序包Install pool application packages

要将应用程序包安装在池中的所有计算节点上,需要为池指定一个或多个应用程序包引用。To install an application package on all compute nodes in a pool, specify one or more application package references for the pool. 为池指定的应用程序包会安装在每个加入池的计算节点上,以及已重启或重置映像的任何节点上。The application packages that you specify for a pool are installed on each compute node that joins the pool, and on any node that is rebooted or reimaged.

在 Batch.NET 中,在创建新池时指定一个或多个 CloudPool.ApplicationPackageReferences,或者为现有池这样做。In Batch .NET, specify one or more CloudPool.ApplicationPackageReferences when you create a new pool, or for an existing pool. ApplicationPackageReference 类指定要安装在池的计算节点上的应用程序 ID 和版本。The ApplicationPackageReference class specifies an application ID and version to install on a pool's compute nodes.

// Create the unbound CloudPool
CloudPool myCloudPool =
    batchClient.PoolOperations.CreatePool(
        poolId: "myPool",
        targetDedicatedComputeNodes: 1,
        virtualMachineSize: "standard_d1_v2",
        cloudServiceConfiguration: new CloudServiceConfiguration(osFamily: "5"));

// Specify the application and version to install on the compute nodes
myCloudPool.ApplicationPackageReferences = new List<ApplicationPackageReference>
{
    new ApplicationPackageReference {
        ApplicationId = "litware",
        Version = "1.1001.2b" }
};

// Commit the pool so that it's created in the Batch service. As the nodes join
// the pool, the specified application package is installed on each.
await myCloudPool.CommitAsync();

重要

如果应用程序包部署失败,Batch 服务会将该节点标记为不可用,并且不会在该节点上计划执行任何任务。If an application package deployment fails, the Batch service marks the node unusable, and no tasks are scheduled for execution on that node. 如果发生这种情况,请重启节点以重新启动包部署。If this happens, restart the node to reinitiate the package deployment. 重启节点也会在节点上再次启用任务计划。Restarting the node also enables task scheduling again on the node.

安装任务应用程序包Install task application packages

类似于池,可以为任务指定应用程序包引用。Similar to a pool, you specify application package references for a task. 在节点上计划要运行的任务时,请先下载并解压缩包,然后执行任务的命令行。When a task is scheduled to run on a node, the package is downloaded and extracted just before the task's command line is executed. 如果节点上已安装指定的包和版本,则不会下载包,而是使用现有包。If a specified package and version is already installed on the node, the package is not downloaded and the existing package is used.

若要安装任务应用程序包,请配置任务的 CloudTask.ApplicationPackageReferences 属性:To install a task application package, configure the task's CloudTask.ApplicationPackageReferences property:

CloudTask task =
    new CloudTask(
        "litwaretask001",
        "cmd /c %AZ_BATCH_APP_PACKAGE_LITWARE%\\litware.exe -args -here");

task.ApplicationPackageReferences = new List<ApplicationPackageReference>
{
    new ApplicationPackageReference
    {
        ApplicationId = "litware",
        Version = "1.1001.2b"
    }
};

执行安装的应用程序Execute the installed applications

为池或任务指定的包下载并解压缩到节点的 AZ_BATCH_ROOT_DIR 中的命名目录。The packages that you've specified for a pool or task are downloaded and extracted to a named directory within the AZ_BATCH_ROOT_DIR of the node. Batch 还会创建包含命名目录路径的环境变量。Batch also creates an environment variable that contains the path to the named directory. 在引用节点上的应用程序时,任务的命令行使用此环境变量。Your task command lines use this environment variable when referencing the application on the node.

在 Windows 节点上,变量格式如下:On Windows nodes, the variable is in the following format:

Windows:
AZ_BATCH_APP_PACKAGE_APPLICATIONID#version

在 Linux 节点上,该格式略有不同。On Linux nodes, the format is slightly different. 句点 (.)、连字符 (-) 和数字符号 (#) 在环境变量中都会转换为下划线。Periods (.), hyphens (-) and number signs (#) are flattened to underscores in the environment variable. 另请注意,请保留应用程序 ID 的大小写。Also, note that the case of the application ID is preserved. 例如:For example:

Linux:
AZ_BATCH_APP_PACKAGE_applicationid_version

APPLICATIONIDversion 是对应于为部署指定的应用程序和包版本的值。APPLICATIONID and version are values that correspond to the application and package version you've specified for deployment. 例如,如果指定应在 Windows 节点上安装 2.7 版的 blender 应用程序,任务命令行可使用此环境变量来访问其文件:For example, if you specified that version 2.7 of application blender should be installed on Windows nodes, your task command lines would use this environment variable to access its files:

Windows:
AZ_BATCH_APP_PACKAGE_BLENDER#2.7

在 Linux 节点上,按以下格式指定环境变量。On Linux nodes, specify the environment variable in this format. 将句点 (.)、连字符 (-) 和数字符号 (#) 平展为下划线,并保留应用程序 ID 的大小写:Flatten the periods (.), hyphens (-) and number signs (#) to underscores, and preserve the case of the application ID:

Linux:
AZ_BATCH_APP_PACKAGE_blender_2_7

在上传应用程序包时,可以指定要部署到计算节点的默认版本。When you upload an application package, you can specify a default version to deploy to your compute nodes. 如果已指定应用程序的默认版本,可以在引用该应用程序时省略版本后缀。If you have specified a default version for an application, you can omit the version suffix when you reference the application. 可以在 Azure 门户的“应用程序”窗口中指定默认的应用程序版本,如上传和管理应用程序中所述。You can specify the default application version in the Azure portal, in the Applications window, as shown in Upload and manage applications.

例如,如果设置“2.7”作为 blender 应用程序的默认版本,并且任务引用以下环境变量,则 Windows 节点将会执行 2.7 版:For example, if you set "2.7" as the default version for application blender , and your tasks reference the following environment variable, then your Windows nodes will execute version 2.7:

AZ_BATCH_APP_PACKAGE_BLENDER

以下代码片段显示任务命令行示例,其可启动 blender 应用程序的默认版本:The following code snippet shows an example task command line that launches the default version of the blender application:

string taskId = "blendertask01";
string commandLine =
    @"cmd /c %AZ_BATCH_APP_PACKAGE_BLENDER%\blender.exe -args -here";
CloudTask blenderTask = new CloudTask(taskId, commandLine);

提示

有关计算节点环境设置的详细信息,请参阅任务的环境设置For more information about compute node environment settings, see Environment settings for tasks.

更新池的应用程序包Update a pool's application packages

如果已配置现有池的应用程序包,可以指定池的新包。If an existing pool has already been configured with an application package, you can specify a new package for the pool. 这意味着:This means:

  • Batch 服务在联接池的所有新节点上以及任何重新启动或重置映像的现有节点上都会安装新指定的包。The Batch service installs the newly specified package on all new nodes that join the pool and on any existing node that is rebooted or reimaged.
  • 在更新包引用时已存在池中的计算节点不自动安装新应用程序包。Compute nodes that are already in the pool when you update the package references do not automatically install the new application package. 这些计算节点必须重新启动或重置映像才能接收新包。These compute nodes must be rebooted or reimaged to receive the new package.
  • 部署新包后,创建的环境变量将反映新的应用程序包引用。When a new package is deployed, the created environment variables reflect the new application package references.

在此示例中,现有池已将 blender 应用程序的 2.7 版配置为其 CloudPool.ApplicationPackageReferences 之一。In this example, the existing pool has version 2.7 of the blender application configured as one of its CloudPool.ApplicationPackageReferences. 要将池的节点更新为 2.76b 版,请将 ApplicationPackageReference 指定为新版本并提交更改。To update the pool's nodes with version 2.76b, specify a new ApplicationPackageReference with the new version, and commit the change.

string newVersion = "2.76b";
CloudPool boundPool = await batchClient.PoolOperations.GetPoolAsync("myPool");
boundPool.ApplicationPackageReferences = new List<ApplicationPackageReference>
{
    new ApplicationPackageReference {
        ApplicationId = "blender",
        Version = newVersion }
};
await boundPool.CommitAsync();

配置新版本后,Batch 服务将在任何联接池的新节点上安装 2.76b 版。Now that the new version has been configured, the Batch service installs version 2.76b to any new node that joins the pool. 要将 2.76b 安装到已在池中的节点上,请重新启动或重置映像这些节点。To install 2.76b on the nodes that are already in the pool, reboot or reimage them. 请注意,重启的节点会保留前面的包部署中的文件。Note that rebooted nodes retain files from previous package deployments.

列出 Batch 帐户中的应用程序List the applications in a Batch account

可以使用 ApplicationOperations.ListApplicationSummaries 方法列出 Batch 帐户中的应用程序及其包。You can list the applications and their packages in a Batch account by using the ApplicationOperations.ListApplicationSummaries method.

// List the applications and their application packages in the Batch account.
List<ApplicationSummary> applications = await batchClient.ApplicationOperations.ListApplicationSummaries().ToListAsync();
foreach (ApplicationSummary app in applications)
{
    Console.WriteLine("ID: {0} | Display Name: {1}", app.Id, app.DisplayName);

    foreach (string version in app.Versions)
    {
        Console.WriteLine("  {0}", version);
    }
}

后续步骤Next steps