【Azure Batch】在中国区批处理服务(Mooncake Batch Account)上实验自动池(Auto Pool)的创建/删除

Pool,Batch,Azure,Moon · 浏览次数 : 2

小编点评

## 生成Task JSON内容的步骤: 1. **获取资源信息**: - 获取Batch Account Overview页面中的Job ID和Priority。 - 通过Azure门户(F12 --> Network Trace )抓取Batch Task页面的Token。 2. **创建Task信息**: - 创建一个Task,并设置优先级为100。 - 设置任务约束,指定最大运行时间、最大任务重试次数等。 - 设置任务管理器任务信息,包括命令行、运行环境配置等。 - 设置池信息,包括虚拟机尺寸、镜像、节点配置等。 3. **设置 Authorization**: - 获取Pool的信息中的"autoPoolSpecification"属性值。 - 使用AuthHeader中获取Authorization的token。 4. **拼凑请求URL和Header**: - 拼接请求URL,包含Job ID、Priority、Constraints等参数。 - 设置请求Header,包含Authorization token和请求内容。 5. **发送请求**: - 使用Postman或其他REST API工具发送POST请求,传入请求URL和Header。 ## 示例代码: ```json { "id": "jobId-2", "priority": 100, "constraints": { "maxWallClockTime": "PT1H" }, "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" } } } } ``` **注意:** * 此代码仅供参考,可能需要根据实际需求进行调整。 * 请确保在发送请求之前获取和设置Authorization token。

正文

问题描述

在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.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

 

 

与【Azure Batch】在中国区批处理服务(Mooncake Batch Account)上实验自动池(Auto Pool)的创建/删除相似的内容:

【Azure Batch】在中国区批处理服务(Mooncake Batch Account)上实验自动池(Auto Pool)的创建/删除

问题描述 在Azure Batch的介绍文档中,提出了自动池的概念, 它可以在任务完成后,自动删除Pool资源,详细介绍:https://docs.azure.cn/zh-cn/batch/nodes-and-pools#autopools & https://learn.microsoft.com

【Azure Batch】在批处理的Task中如何让它执行多个CMD指令呢

cmd /c "echo %date% %time% & set AZ_BATCH & timeout /t 90 > NUL & timeout /nobreak /t 10 & echo 'what is your name?' & echo %date% %time% & for %I in (1,2,3,4,5,6) do echo '%time%' "

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)

在前一篇文章中,我们是把.NET 8应用读取SSL证书(X509)示例部署在App Service Windows环境中,那么如果部署在Linux环境,以及Linux Container中呢? 根据前文中的第一种方法,直接在把证书文件包含在源文件中,通过相对路径读取证书文件的方式,经测试,可以正常工

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)

在使用App Service服务部署业务应用,因为有些第三方的接口需要调用者携带TLS/SSL证书(X509 Certificate),在官方文档中介绍了两种方式在代码中使用证书: 1) 直接使用证书文件路径加载证书 new X509Certificate2 2) 从系统的证书库中通过指纹加载...

Azure Service Principals ----- Azure 上最好保守的秘密的服务

一,引言 Azure Service Principals 是 Azure Active Directory (AAD) 中的一种标识,代表应用程序,服务,自动化流程。Service Principals 支持各种 Azure 服务和资源之家的安全通信,为应用程序提供了一种进行身份验证并于 Azur

【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码

问题描述 对于Azure资源进行配置操作,门户上可以正常操作。但是想通过Python代码实现,这样可以批量处理。那么在没有SDK的情况下,是否有快速办法呢? 问题解答 当然可以,Azure Portal上操作的所有资源都是通过REST API来实现的,所以只要找到正确的API,就可以通过浏览器中抓取

【Azure APIM】列举几种在APIM 策略中的主动生产的错误语句

问题描述 在为APIM服务配置了诊断日志(Diagnostic Setting),把日志收集在Log A Workspace中,需要验证日志中是否能查看到请求的错误信息。 所以想人为的来制造一些错误。经过网络搜索,参考Policy的文档介绍后,完成了以下3种错误 第一种:使用 return-resp

【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG

问题描述 看见一个有趣的页面,可以把输入的文字信息,直接输出SVG图片,还可以实现动图模式。 示例URL: https://readme-typing-svg.demolab.com/?font=Fira+Code&pause=1000&color=F7F7F7&background=233911F

Azure DevOps Server 入门实践与安装部署

一,引言 最近一段时间,公司希望在自己的服务器上安装本地版的 Azure DevOps Service(Azure DevOps Server),用于项目内的测试,学习。本着学习的目的,我也就开始学习在测试服务器上安装 Azure DevOps Server 2022 Express 以及测试的 D

Azure DevOps Server 设置项目管理用户,用户组

一,引言 Azure DevOps Server 搭建完成后,关于如何进行项目管理,项目成员管理等,我们接着上一篇文章,继续讲解 Azure DevOps Server 的用户,用户组。首先,我们需要明白 Azure DevOps Server 有哪些登录方式 1)Azure DevOps Serv