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

快速入门:使用 Azure CLI 运行第一个 Batch 作业Quickstart: Run your first Batch job with the Azure CLI

Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 本快速入门介绍如何使用 Azure CLI 创建 Batch 帐户、包含计算节点(虚拟机)的以及在池中运行任务作业This quickstart shows how to use the Azure CLI to create a Batch account, a pool of compute nodes (virtual machines), and a job that runs tasks on the pool. 每个示例任务在一个池节点上运行一个基本命令。Each sample task runs a basic command on one of the pool nodes. 完成本快速入门以后,你会了解 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.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 通过 Cloud Shell 可以将 bashPowerShell 与 Azure 服务配合使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To launch Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 选择“试用” 不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell” 按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上方菜单栏中的“Cloud Shell” 按钮。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Launch Cloud Shell.

  2. 选择代码块上的“复制”按钮 以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上使用 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上使用 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.

  4. Enter 运行此代码。Press Enter to run the code.

如果选择在本地安装并使用 CLI,本快速入门要求运行 Azure CLI 2.0.20 版或更高版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.20 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

使用 az group create 命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

以下示例在“eastus2” 位置创建名为“myResourceGroup”的资源组 。The following example creates a resource group named myResourceGroup in the eastus2 location.

az group create \
    --name myResourceGroup \
    --location eastus2

创建存储帐户Create a storage account

可以将 Azure 存储帐户与 Batch 帐户相关联。You can link an Azure Storage account with your 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. 使用 az storage account create 命令在资源组中创建存储帐户。Create a storage account in your resource group with the az storage account create command.

az storage account create \
    --resource-group myResourceGroup \
    --name mystorageaccount \
    --location eastus2 \
    --sku Standard_LRS

创建批处理帐户Create a Batch account

使用 az batch account create 命令创建 Batch 帐户。Create a Batch account with the az batch account create command. 需要一个帐户来创建计算资源(计算节点池)和 Batch 作业。You need an account to create compute resources (pools of compute nodes) and Batch jobs.

以下示例在 myResourceGroup 中创建名为 mybatchaccount 的 Batch 帐户,并链接已创建的存储帐户。The following example creates a Batch account named mybatchaccount in myResourceGroup, and links the storage account you created.

az batch account create \
    --name mybatchaccount \
    --storage-account mystorageaccount \
    --resource-group myResourceGroup \
    --location eastus2

若要创建和管理计算池和作业,需使用 Batch 进行身份验证。To create and manage compute pools and jobs, you need to authenticate with Batch. 使用 az batch account login 命令登录到帐户。Log in to the account with the az batch account login command. 登录后,az batch 命令使用此帐户上下文。After you log in, your az batch commands use this account context.

az batch account login \
    --name mybatchaccount \
    --resource-group myResourceGroup \
    --shared-key-auth

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

有了 Batch 帐户以后,即可使用 az batch pool create 命令创建包含 Linux 计算节点的示例池。Now that you have a Batch account, create a sample pool of Linux compute nodes using the az batch pool create command. 以下示例创建名为 mypool 的池,其中包含 2 个大小为 Standard_A1_v2 且运行 Ubuntu 16.04 LTS 的节点。The following example creates a pool named mypool of 2 size Standard_A1_v2 nodes running Ubuntu 16.04 LTS. 就此快速示例来说,建议的节点大小在性能和成本之间达成了很好的平衡。The suggested node size offers a good balance of performance versus cost for this quick example.

az batch pool create \
    --id mypool --vm-size Standard_A1_v2 \
    --target-dedicated-nodes 2 \
    --image canonical:ubuntuserver:16.04-LTS \
    --node-agent-sku-id "batch.node.ubuntu 16.04" 

Batch 会立即创建池,但分配和启动计算节点则需要数分钟。Batch creates the pool immediately, but it takes a few minutes to allocate and start the compute nodes. 在此期间,池处于resizing状态。During this time, the pool is in the resizing state. 若要查看池的状态,请运行 az batch pool show 命令。To see the status of the pool, run the az batch pool show command. 此命令显示池的所有属性,你可以查询特定的属性。This command shows all the properties of the pool, and you can query for specific properties. 以下命令获取池的分配状态:The following command gets the allocation state of the pool:

az batch pool show --pool-id mypool \
    --query "allocationState"

继续以下步骤,在池状态更改的情况下创建作业和任务。Continue the following steps to create a job and tasks while the pool state is changing. 如果分配状态为steady且所有节点处于运行状态,则说明池已做好运行任务的准备。The pool is ready to run tasks when the allocation state is steady and all the nodes are running.

创建作业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. 使用 az batch job create 命令创建 Batch 作业。Create a Batch job by using the az batch job create command. 以下示例在 mypool 池中创建 myjob 作业。The following example creates a job myjob on the pool mypool. 作业一开始没有任务。Initially the job has no tasks.

az batch job create \
    --id myjob \
    --pool-id mypool

创建任务Create tasks

现在,请使用 az batch task create 命令创建一些在作业中运行的任务。Now use the az batch task create command to create some tasks to run in the job. 此示例创建四个相同的任务。In this example, you create four identical tasks. 每个任务都运行command-line,用于显示计算节点上的 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, this 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.

以下 Bash 脚本创建 4 个并行任务(mytask1mytask4)。The following Bash script creates 4 parallel tasks (mytask1 to mytask4).

for i in {1..4}
do
   az batch task create \
    --task-id mytask$i \
    --job-id myjob \
    --command-line "/bin/bash -c 'printenv | grep AZ_BATCH; sleep 90s'"
done

命令输出显示每个任务的设置。The command output shows settings for each of the tasks. Batch 将任务分发到计算节点。Batch distributes the tasks to the compute nodes.

查看任务状态View task status

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

使用 az batch task show 命令查看 Batch 任务的状态。Use the az batch task show command to view the status of the Batch tasks. 以下示例显示在一个池节点上运行的 mytask1 的详细信息。The following example shows details about mytask1 running on one of the pool nodes.

az batch task show \
    --job-id myjob \
    --task-id mytask1

命令输出包括许多详细信息,但请记下任务命令行的 exitCode 以及 nodeIdThe command output includes many details, but take note of the exitCode of the task command line and the nodeId. exitCode 为 0 指示任务命令行已成功完成。An exitCode of 0 indicates that the task command line completed successfully. nodeId 指示任务运行时所在池节点的 ID。The nodeId indicates the ID of the pool node on which the task ran.

查看任务输出View task output

若要列出计算节点上的任务所创建的文件,请使用 az batch task file list 命令。To list the files created by a task on a compute node, use the az batch task file list command. 以下命令列出 mytask1 创建的文件:The following command lists the files created by mytask1:

az batch task file list \
    --job-id myjob \
    --task-id mytask1 \
    --output table

输出与下面类似:Output is similar to the following:

Name        URL                                                                                         Is Directory      Content Length
----------  ------------------------------------------------------------------------------------------  --------------  ----------------
stdout.txt  https://mybatchaccount.eastus2.batch.azure.com/jobs/myjob/tasks/mytask1/files/stdout.txt  False                  695
certs       https://mybatchaccount.eastus2.batch.azure.com/jobs/myjob/tasks/mytask1/files/certs       True
wd          https://mybatchaccount.eastus2.batch.azure.com/jobs/myjob/tasks/mytask1/files/wd          True
stderr.txt  https://mybatchaccount.eastus2.batch.azure.com/jobs/myjob/tasks/mytask1/files/stderr.txt  False                     0

若要将某个输出文件下载到本地目录,请使用 az batch task file download 命令。To download one of the output files to a local directory, use the az batch task file download command. 在此示例中,任务输出位于 stdout.txtIn this example, task output is in stdout.txt.

az batch task file download \
    --job-id myjob \
    --task-id mytask1 \
    --file-path stdout.txt \
    --destination ./stdout.txt

可以在文本编辑器中查看 stdout.txt 的内容。You can view the contents of stdout.txt in a text editor. 内容显示在节点上设置的 Azure Batch 环境变量。The contents show the Azure Batch environment variables that are set on the node. 创建自己的 Batch 作业时,可以在任务命令行中引用这些环境变量,并在命令行运行的应用和脚本中引用它们。When you create your own Batch jobs, you can reference these environment variables in task command lines, and in the apps and scripts run by the command lines. 例如:For example:

AZ_BATCH_TASK_DIR=/mnt/batch/tasks/workitems/myjob/job-1/mytask1
AZ_BATCH_NODE_STARTUP_DIR=/mnt/batch/tasks/startup
AZ_BATCH_CERTIFICATES_DIR=/mnt/batch/tasks/workitems/myjob/job-1/mytask1/certs
AZ_BATCH_ACCOUNT_URL=https://mybatchaccount.eastus2.batch.azure.com/
AZ_BATCH_TASK_WORKING_DIR=/mnt/batch/tasks/workitems/myjob/job-1/mytask1/wd
AZ_BATCH_NODE_SHARED_DIR=/mnt/batch/tasks/shared
AZ_BATCH_TASK_USER=_azbatch
AZ_BATCH_NODE_ROOT_DIR=/mnt/batch/tasks
AZ_BATCH_JOB_ID=myjobl
AZ_BATCH_NODE_IS_DEDICATED=true
AZ_BATCH_NODE_ID=tvm-257509324_2-20180703t215033z
AZ_BATCH_POOL_ID=mypool
AZ_BATCH_TASK_ID=mytask1
AZ_BATCH_ACCOUNT_NAME=mybatchaccount
AZ_BATCH_TASK_USER_IDENTITY=PoolNonAdmin

清理资源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 pools while the nodes are running, even if no jobs are scheduled. 不再需要池时,请使用 az batch pool delete 命令将其删除。When you no longer need a pool, delete it with the az batch pool delete command. 删除池时会删除节点上的所有任务输出。When you delete the pool, all task output on the nodes is deleted.

az batch pool delete --pool-id mypool

如果不再需要资源组、Batch 帐户、池和所有相关的资源,则可以使用 az group delete 命令将其删除。When no longer needed, you can use the az group delete command to remove the resource group, Batch account, pools, and all related resources. 删除资源,如下所示:Delete the resources as follows:

az group delete --name myResourceGroup

后续步骤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.