问题描述
在Azure Batch的介绍文档中,提出了自动池的概念, 它可以在任务完成后,自动删除Pool资源,详细介绍:https://docs.azure.cn/zh-cn/batch/nodes-and-pools#autopools & https://learn.microsoft.com/zh-cn/rest/api/batchservice/job/add?tabs=HTTP#autopoolspecification
自动池是在提交作业时由 Batch 服务创建的池,而不是在创建将在池中运行的作业之前创建的。
Batch 服务将根据指定的特征管理自动池的生存期。
大多数情况下,这些池也设置为在其作业完成后自动删除。
虽然在自动池的文档中包含了示例,但是示例中的创建的Task需要一个 myprogram2.exe 应用,并且设置的 cloudServiceConfiguration 属性值已经不在可用。 所以,为了简化在中国区的Batch Account服务上实现自动池进行了本次实验。
问题解答
基于示例中,创建Task的 JSON 内容,需要做一下几点的修改:
1) POST 的请求 URL 从 Batch Account Overview 页面中 Account endpoint 复制。
account.region.batch.azure.com/jobs?api-version=2023-05-01.17.0
//变为
<your batch account>.<chinanorth3>.batch.chinacloudapi.cn/jobs?api-version=2023-05-01.17.0
2) Task中的commandLine内容修改为简单的CMD指令
"commandLine": "cmd /c \"echo %date% %time% & set AZ_BATCH & timeout /t 90 > NUL & timeout /nobreak /t 10000 & echo 'what is your name?' & echo %date% %time% \"",
3) Pool 的信息,指定 vm size 和 image
"pool": { "vmSize": "STANDARD_A1_V2", "virtualMachineConfiguration": { "imageReference": { "publisher": "microsoftwindowsserver", "offer": "windowsserver", "sku": "2019-datacenter-core-smalldisk", "version": "latest" }, "nodeAgentSKUId": "batch.node.windows amd64", "licenseType": null, "nodePlacementConfiguration": { "policy": "Regional" } },
4)此处,获取Authorization的方式为从Azure门户上(F12 --> Network Trace )抓取Batch Task页面的Token
完成以上4步后,即可通过Postman或其他REST API工具发送POST请求。
完整的Task Body内容为:
{ "id": "jobId-2", "priority": 100, "constraints": { "maxWallClockTime": "PT1H", "maxTaskRetryCount": -1 }, "jobManagerTask": { "id": "taskId", "commandLine": "cmd /c \"echo %date% %time% & set AZ_BATCH & timeout /t 90 > NUL & timeout /nobreak /t 10000 & echo 'what is your name?' & echo %date% %time% \"", "constraints": { "maxWallClockTime": "PT1H", "maxTaskRetryCount": 0, "retentionTime": "PT1H" }, "requiredSlots": 2, "killJobOnCompletion": false, "userIdentity": { "autoUser": { "scope": "task", "elevationLevel": "admin" } }, "runExclusive": true }, "poolInfo": { "autoPoolSpecification": { "autoPoolIdPrefix": "mypool", "poolLifetimeOption": "job", "pool": { "vmSize": "STANDARD_A1_V2", "virtualMachineConfiguration": { "imageReference": { "publisher": "microsoftwindowsserver", "offer": "windowsserver", "sku": "2019-datacenter-core-smalldisk", "version": "latest" }, "nodeAgentSKUId": "batch.node.windows amd64", "licenseType": null, "nodePlacementConfiguration": { "policy": "Regional" } }, "resizeTimeout": "PT15M", "targetDedicatedNodes": 1, "targetLowPriorityNodes": 0, "taskSlotsPerNode": 1, "taskSchedulingPolicy": { "nodeFillType": "spread" }, "enableAutoScale": false, "enableInterNodeCommunication": true, "metadata": [ { "name": "myproperty", "value": "myvalue" } ], "targetNodeCommunicationMode": "default" } } } }
请求的URL及Header:
POST https://XXXXXXXXXXXXXX.chinanorth3.batch.chinacloudapi.cn/jobs?api-version=2023-05-01.17.0
Accept: */*
User-Agent: Thunder Client (https://www.thunderclient.com)
Content-Type: application/json; odata=minimalmetadata
Authorization: Bearer Token
示例Demo:
参考资料
自动池: https://docs.azure.cn/zh-cn/batch/nodes-and-pools#autopools
Job - Add : https://learn.microsoft.com/zh-cn/rest/api/batchservice/job/add?tabs=HTTP#autopoolspecification