python - Azure 批处理池 : How do I use a custom VM Image via Python?

标签 python azure azure-batch

我想使用 Python 创建我的池。当使用市场上的镜像 (Ubuntu Server 16.04) 时,我可以执行此操作,但我想使用自定义镜像(但也包括 Ubuntu Server 16.04)——我已使用所需的库和设置准备了该镜像。

这就是我创建池的方式:

new_pool = batch.models.PoolAddParameter(
      id=pool_id,
      virtual_machine_configuration=batchmodels.VirtualMachineConfiguration(
          image_reference=image_ref_to_use, # ??
          node_agent_sku_id=sku_to_use),
      vm_size=_POOL_VM_SIZE,
      target_dedicated_nodes=_POOL_NODE_COUNT,
      start_task=start_task,
      max_tasks_per_node=_CORES_PER_NODE
)

我想我需要使用 batch.models.ImageReference()创建我的图像引用...但我不知道如何使用它。

是的,我检查了documentation ,其内容如下:

A reference to an Azure Virtual Machines Marketplace image or a custom Azure Virtual Machine image.

它列出的参数为:

  • 出版商(str)
  • 报价(str)
  • sku(字符串)
  • 版本(str)
  • virtual_machine_image_id (str)

但是参数virtual_machine_image_id不存在...换句话说,batch.models.ImageReference(virtual_machine_image_id)是不允许的。

如何为我的池使用自定义图像?

更新

所以我想出了如何使用自定义图像...事实证明,无论我卸载 azure python 库并重新安装多少次,virtual_machine_image_id永远不可用。

然后我去了here下载了 zip 文件。打开它,检查了ImageReference类(Class)和低调,virtual_machine_image_id可以在 __init__ 中找到ImageReference的功能类(class)。然后我下载了 pythonwheel 并使用 pip 来安装它。成功了。

或者我是这么想的。

然后我不得不努力弄清楚node_agent_sku_id是什么。是...仅通过手动创建池并查看 Batch Node Agent SKU ID我设法找到它了吗?

现在我正在努力解决身份验证问题...

我收到的错误是:

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

AuthenticationErrorDetail: The specified type of authentication SharedKey is not allowed when external resources of type Compute are linked.

azure.batch.models.batch_error.BatchErrorException: {'lang': 'en-US', 'value': 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:f8c1a3b3-65c4-4efd-9c4f-75c5c253f992\nTime:2017-10-15T20:36:06.7898187Z'}

从错误中,我了解到我不允许使用 SharedKeyCredentials :

credentials = batchauth.SharedKeyCredentials(_BATCH_ACCOUNT_NAME,
                                             _BATCH_ACCOUNT_KEY)

batch_client = batch.BatchServiceClient(
    credentials,
    base_url=_BATCH_ACCOUNT_URL)

我必须做什么?

更新2

好的。用户fpark已通知我需要使用:

from azure.batch import BatchServiceClient
from azure.common.credentials import ServicePrincipalCredentials

credentials = ServicePrincipalCredentials(
    client_id=CLIENT_ID,
    secret=SECRET,
    tenant=TENANT_ID,
    resource="https://batch.core.windows.net/"
)
    batch_client = BatchServiceClient(
    credentials,
    base_url=BATCH_ACCOUNT_URL
)

进行身份验证。不幸的是,上面的代码被描述为 here并且没有提及什么CLIENT_ID等都是。

然后我设法找到了另一份文档,它似乎是同一件事:https://azure-sdk-for-python.readthedocs.io/en/v2.0.0rc3/resourcemanagementauthentication.html

该页面将我指向另一个网页:https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal

我按照该教程进行操作并最终成功验证了我的应用程序...

注意

创建应用程序时,教程将告诉您:

Provide a name and URL for the application. Select either Web app / API or Native for the type of application you want to create. After setting the values, select Create.

请勿选择 Native因为您无法选择获取应用程序 key ...

最佳答案

所需的最低 Azure Batch SDK

azure-batch需要 Python SDK v4.0.0 或更高版本。通常,使用 pip install --upgrade azure-batch 您应该获得最新版本。如果这不起作用,您可以向 pip 添加 --force-reinstall 选项来强制它(使用 --upgrade)。

节点代理 Sku Id

关于node_agent_sku_id的正确值,您需要使用list_node_agent_skus操作以查看操作系统和支持的节点代理 sku 之间的映射。

需要 Azure Active Directory 身份验证

关于auth问题,必须使用Azure Active Directory authentication使用此功能。它不适用于共享 key 身份验证。

文档

更多信息可以在this guide中找到,包括启用自定义图像所需的所有先决条件。

关于python - Azure 批处理池 : How do I use a custom VM Image via Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46756780/

相关文章:

python - 特定的 DAG 在 Airflow 中停止运行,即使调度程序运行良好

asp.net-mvc-3 - Azure ACS - 最佳实践实现

Azure批处理作业如何将大文件拆分为较小的文件

python - 自定义 numpy 类型导致 numpy.mean 崩溃

python - 索引值和列相乘

python - 如何传播python数组

Azure 虚拟机磁盘空间不足

Azure 开发运营 : Why is my subscription not shown when creating a new service connection?

powershell - 如何使用帐户 key 从 PowerShell 列出 Azure Batch 池?

rest - 通过 RESTful API 将新的/更新的应用程序版本部署到 Azure Batch 服务?