【问题标题】:Creating a pool in Azure with python SDK使用 python SDK 在 Azure 中创建池
【发布时间】:2018-12-06 16:38:45
【问题描述】:

我正在尝试基于标准市场 ubuntu 映像创建一个池。我正在使用Azure 4.0.0image referncevm config reference 和其他东西是基于 docs.microsoft.com 编写的

这是我的代码:

import azure.batch as batch
from azure.batch import BatchServiceClient
from azure.batch.batch_auth import SharedKeyCredentials
from azure.batch import models
import sys

account = 'mybatch'
key = 'Acj1hh7vMR6DSodYgYEghjce7mHmfgfdgodYgYEghjce7mHmfgodYgYEghjce7mHmfgCj/7f3Zs1rHdfgPsdlA=='
batch_url = 'https://mybatch.westeurope.batch.azure.com'

creds = SharedKeyCredentials(account, key)
batch_client = BatchServiceClient(creds, base_url = batch_url)


pool_id = 'mypool3'

if batch_client.pool.exists( pool_id ):
  print( 'pool exists' )
  sys.exit()

vmc = models.VirtualMachineConfiguration(
  image_reference = models.ImageReference(
    offer = 'UbuntuServer', 
    publisher = 'Canonical',
    sku = '16.04.0-LTS', 
    version = 'latest', 
    virtual_machine_image_id = None
  ) ,
  node_agent_sku_id = 'batch.node.ubuntu 16.04'
)

pool_config = models.CloudServiceConfiguration(os_family = '5')

new_pool = models.PoolAddParameter(
  id = pool_id, 
  vm_size = 'small', 
  cloud_service_configuration = pool_config, 
  target_dedicated_nodes = 1,
  virtual_machine_configuration = vmc
)

batch_client.pool.add(new_pool)

以下是我从 azure 门户(添加池 JSON 编辑器)获取的一些图像值:

>

“图像参考”:{

"publisher": "Canonical",

"offer": "UbuntuServer",

“sku”:“16.04.0-LTS”

},

但是当我运行代码时出现错误:

Traceback (most recent call last):
  File "a.py", line 80, in <module>
    batch_client.pool.add(new_pool)
  File "/root/miniconda/lib/python3.6/site-packages/azure/batch/operations/pool_operations.py", line 310, in add
    raise models.BatchErrorException(self._deserialize, response)
azure.batch.models.batch_error_py3.BatchErrorException: {'additional_properties': {}, 'lang': 'en-US', 'value': 'The value provided for one of the properties in the request body is invalid.\nRequestId:d8a1f7fa-6f40-4e4e-8f41-7958egas6efa\nTime:2018-12-05T16:18:44.5453610Z'}

哪些图像值是错误的?是否可以通过 RequestId 获取有关此错误的更多信息?


更新

我找到了一个newer example here,它正在使用这个助手select_latest_verified_vm_image_with_node_agent_sku 来获取图像参考。同样的错误The value provided for one of the properties in the request body is invalid.

【问题讨论】:

  • 你可以看看这个link
  • @CharlesXu 这个例子也给了我错误。它并没有说明它是为什么 SDK 版本编写的。我试图更新代码以修复错误,但下一行也给了我一个错误等等。最后一个是AttributeError: 'BatchServiceClientConfiguration' object has no attribute 'signed_session'。无论如何,创建图像参考的值是相同的。无论如何感谢您的链接。
  • 好的,我也做了测试。如果我解决了问题,我会给出答案。

标签: azure azure-sdk azure-sdk-python


【解决方案1】:

我用你的代码做了测试,得到了同样的错误。然后我研究并更改了代码中的一些内容。以及由两件事引起的问题。

第一

pool_config = models.CloudServiceConfiguration(os_family = '5')

你可以看看models.CloudServiceConfiguration的描述:

os_family: The Azure Guest OS family to be installed on the virtual
     machines in the pool. Possible values are: 2 - OS Family 2, equivalent to
     Windows Server 2008 R2 SP1. 3 - OS Family 3, equivalent to Windows Server
     2012. 4 - OS Family 4, equivalent to Windows Server 2012 R2. 5 - OS Family
     5, equivalent to Windows Server 2016. For more information, see Azure
     Guest OS Releases

也许这个属性是为 windows 设置的。你可以去掉这个配置。

第二

vm_size = 'small', 

您应该将vmSize 设置为真实的VM 大小。例如,Standard_A1。见Choose a VM size for compute nodes in an Azure Batch pool

希望这会对您有所帮助。如果您需要更多帮助,请给我留言。

【讨论】:

  • 谢谢,但是:-) : 首先 - 我已经尝试了所有的数字,这个参数是必需的。第二 - 我尝试了像 'Standard_A1_v2' 这样的值,但它们不起作用(参考 docs.microsoft.com/en-us/azure/cloud-services/… )。 '小'虽然有效。除了virtual_machine_configuration 部分之外,基本上我的所有代码都有效。如果您从我的示例中删除 virtual_machine_configuration = vmc,它将创建一个 Windows VM 池。
  • 您的回答促使我寻找解决方案。谢谢你:)
【解决方案2】:

我认为网上有很多令人困惑的例子,或者它们只是匹配旧版本的 SDK。

深入挖掘我找到的文档this

cloud_service_configuration CloudServiceConfiguration 云 池的服务配置。 此属性和 virtualMachineConfiguration 是互斥的,并且是其中之一 必须指定属性。如果 创建批处理帐户时将其 poolAllocationMode 属性设置为 '用户订阅'。

就我而言,我只能使用 cloud_service_configuration = pool_config virtual_machine_configuration = vmc,但不能同时使用。

这是工作代码:

new_pool = models.PoolAddParameter(
  id = pool_id, 
  vm_size = 'BASIC_A1', 
  target_dedicated_nodes = 1,
  virtual_machine_configuration = vmc
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2021-10-26
    • 2020-05-03
    相关资源
    最近更新 更多