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

快速入门:在 Azure 门户中运行第一个 Batch 作业Quickstart: Run your first Batch job in the Azure portal

本快速入门介绍如何使用 Azure 门户创建 Batch 帐户、包含计算节点(虚拟机)的以及在池中运行基本任务作业This quickstart shows how to use the Azure portal to create a Batch account, a pool of compute nodes (virtual machines), and a job that runs basic tasks on the pool. 完成本快速入门以后,你会了解 Batch 服务的重要概念,并可使用更逼真的工作负荷进行更大规模的 Batch 试用。After completing this quickstart, you will understand the key concepts of the Batch service and be ready to try Batch with more realistic workloads at larger scale.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

登录 AzureSign in to Azure

https://portal.azure.com 中登录 Azure 门户。Sign in to the Azure portal at https://portal.azure.com.

创建批处理帐户Create a Batch account

请按以下步骤创建一个用于测试的示例 Batch 帐户。Follow these steps to create a sample Batch account for test purposes. 创建池和作业需要 Batch 帐户。You need a Batch account to create pools and jobs. 如此处所示,可以将 Azure 存储帐户与 Batch 帐户相关联。As shown here, you can link an Azure storage account with the Batch account. 虽然本快速入门不需要,但存储帐户可以用于为大多数现实世界的工作负荷部署应用程序和存储输入和输出数据。Although not required for this quickstart, the storage account is useful to deploy applications and store input and output data for most real-world workloads.

  1. 选择“创建资源” > “计算” > “Batch 服务”。Select Create a resource > Compute > Batch Service.


  2. 在“帐户名称”和“资源组”中输入值 。Enter values for Account name and Resource group. 帐户名称必须在所选 Azure 位置中唯一,只能使用小写字符或数字,包含 3-24 个字符。The account name must be unique within the Azure Location selected, use only lowercase characters or numbers, and contain 3-24 characters.

  3. 存储帐户中选择一个现有存储帐户,或创建一个新的存储帐户。In Storage account, select an existing storage account or create a new one.

  4. 让其余设置保留默认值,然后选择“创建”以创建该帐户。 Keep the defaults for remaining settings, and select Create to create the account.


出现“部署成功”消息后,转到门户中的 Batch 帐户。 When the Deployment succeeded message appears, go to the Batch account in the portal.

创建计算节点池Create a pool of compute nodes

有了 Batch 帐户以后,请创建一个示例池,其中包含用于测试的 Windows 计算节点。Now that you have a Batch account, create a sample pool of Windows compute nodes for test purposes. 此快速示例的池包含 2 个运行 Windows Server 2012 R2 映像的节点,该映像来自 Azure 市场。The pool for this quick example consists of 2 nodes running a Windows Server 2012 R2 image from the Azure Marketplace.

  1. 在 Batch 帐户中,选择“池” > “添加”。In the Batch account, select Pools > Add.

  2. 输入名为“mypool”的池 IDEnter a Pool ID called mypool.

  3. 在“操作系统”中选择以下设置(可以浏览其他选项)。 In Operating System, select the following settings (you can explore other options).

    设置Setting Value
    映像类型Image Type 市场 (Linux/Windows)Marketplace (Linux/Windows)
    发布者Publisher MicrosoftWindowsServerMicrosoftWindowsServer
    产品Offer WindowsServerWindowsServer
    SkuSku 2012-R2-Datacenter-smalldisk2012-R2-Datacenter-smalldisk


  4. 向下滚动,以便输入“节点大小”和“规模”设置。 Scroll down to enter Node Size and Scale settings. 就此快速示例来说,建议的节点大小在性能和成本之间达成了很好的平衡。The suggested node size offers a good balance of performance versus cost for this quick example.

    设置Setting Value
    节点定价层Node pricing tier Standard_A1Standard_A1
    目标专用节点Target dedicated nodes 22


  5. 让其余设置保留默认值,然后选择“确定”以创建该池。 Keep the defaults for remaining settings, and select OK to create the pool.

Batch 会立即创建池,但分配和启动计算节点则需要数分钟。Batch creates the pool immediately, but it takes a few minutes to allocate and start the compute nodes. 在此期间,池的“分配状态” 为“调整大小” 。During this time, the pool's Allocation state is Resizing. 当池在调整大小时,可以继续操作,创建作业和任务。You can go ahead and create a job and tasks while the pool is resizing.


数分钟后,池的状态为“稳定”,节点启动。 After a few minutes, the state of the pool is Steady, and the nodes start. 选择“节点”,查看节点的状态。 Select Nodes to check the state of the nodes. 节点在状态为“空闲”时可以运行任务。 When a node's state is Idle, it is ready to run tasks.

创建作业Create a job

有了池之后,请创建可在其中运行的作业。Now that you have a pool, create a job to run on it. Batch 作业是适用于一个或多个任务的逻辑组。A Batch job is a logical group for one or more tasks. 作业包含任务的公用设置,例如优先级以及运行任务的池。A job includes settings common to the tasks, such as priority and the pool to run tasks on. 作业一开始没有任务。Initially the job has no tasks.

  1. 在 Batch 帐户视图中,选择“作业” > “添加”。In the Batch account view, select Jobs > Add.

  2. 输入名为“myjob”的作业 IDEnter a Job ID called myjob. 在“池”中,选择“mypool”。 In Pool, select mypool. 其余设置保留默认值,然后选择“确定” 。Keep the defaults for the remaining settings, and select OK.


创建作业后,“任务” 页会打开。After the job is created, the Tasks page opens.

创建任务Create tasks

现在创建要在作业中运行的示例任务。Now create sample tasks to run in the job. 通常创建多个可在计算节点上运行的任务,由 Batch 进行排队和分发。Typically you create multiple tasks that Batch queues and distributes to run on the compute nodes. 此示例创建两个相同的任务。In this example, you create two identical tasks. 每个任务都运行一个命令行,用于显示计算节点上的 Batch 环境变量,然后等待 90 秒。Each task runs a command line to display the Batch environment variables on a compute node, and then waits 90 seconds.

使用 Batch 时,可以在命令行中指定应用或脚本。When you use Batch, the command line is where you specify your app or script. Batch 提供多种将应用和脚本部署到计算节点的方式。Batch provides a number of ways to deploy apps and scripts to compute nodes.

若要创建第一个任务,请执行以下操作:To create the first task:

  1. 选择 添加Select Add.

  2. 输入名为“mytask”的任务 IDEnter a Task ID called mytask.

  3. 在“命令行”中输入 cmd /c "set AZ_BATCH & timeout /t 90 > NUL"In Command line, enter cmd /c "set AZ_BATCH & timeout /t 90 > NUL". 其余设置保留默认值,然后选择“确定” 。Keep the defaults for the remaining settings, and select OK.


创建任务后,Batch 会让其排队在池中运行。After you create a task, Batch queues it to run on the pool. 可以使用节点来运行该任务后,任务就会运行。When a node is available to run it, the task runs.

若要创建第二个任务,请回到步骤 1。To create a second task, go back to step 1. 输入另一任务 ID,但请指定相同的命令行。Enter a different Task ID, but specify an identical command line. 如果第一个任务仍在运行,Batch 会在池中的另一节点上启动第二个任务。If the first task is still running, Batch starts the second task on the other node in the pool.

查看任务输出View task output

前面的任务示例在数分钟内完成。The preceding task examples complete in a couple of minutes. 若要查看已完成任务的输出,请选择“节点上的文件” ,然后选择 stdout.txt 文件。To view the output of a completed task, select Files on node, and then select the file stdout.txt. 此文件显示任务的标准输出。This file shows the standard output of the task. 内容如下所示:The contents are similar to the following:


内容显示在节点上设置的 Azure Batch 环境变量。The contents show the Azure Batch environment variables that are set on the node. 创建自己的 Batch 作业和任务时,可以在任务命令行中引用这些环境变量,并在命令行运行的应用和脚本中引用它们。When you create your own Batch jobs and tasks, you can reference these environment variables in task command lines, and in the apps and scripts run by the command lines.

清理资源Clean up resources

若要继续学习 Batch 教程和示例,请使用在本快速入门中创建的 Batch 帐户和关联的存储帐户。If you want to continue with Batch tutorials and samples, use the Batch account and linked storage account created in this quickstart. Batch 帐户本身不收费。There is no charge for the Batch account itself.

只要有节点在运行,就会对池收费,即使没有计划作业。You are charged for the pool while the nodes are running, even if no jobs are scheduled. 不再需要池时,请将其删除。When you no longer need the pool, delete it. 在帐户视图中,选择“池” 和池的名称。In the account view, select Pools and the name of the pool. 然后选择“删除” 。Then select Delete. 删除池时会删除节点上的所有任务输出。When you delete the pool, all task output on the nodes is deleted.

若不再需要资源组、Batch 帐户以及所有相关资源,请将其删除。When no longer needed, delete the resource group, Batch account, and all related resources. 为此,请选择 Batch 帐户所在的资源组,然后选择“删除资源组”。 To do so, select the resource group for the Batch account and select Delete resource group.

后续步骤Next steps

本快速入门创建了 Batch 帐户、Batch 池和 Batch 作业。In this quickstart, you created a Batch account, a Batch pool, and a Batch job. 作业运行示例任务,你查看了在其中一个节点上产生的输出。The job ran sample tasks, and you viewed output created on one of the nodes. 了解 Batch 服务的重要概念以后,即可使用更逼真的工作负荷进行更大规模的 Batch 试用。Now that you understand the key concepts of the Batch service, you are ready to try Batch with more realistic workloads at larger scale. 若要详细了解 Azure Batch,请继续学习 Azure Batch 教程。To learn more about Azure Batch, continue to the Azure Batch tutorials.