使用 python SDK 在 Azure 中创建池
Creating a pool in Azure with python SDK
我正在尝试创建一个基于标准市场 ubuntu 图像的池。我正在使用 Azure 4.0.0, image refernce, vm 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 编辑器)获取的一些图像值:
>
"imageReference": {
"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 which is using this helper select_latest_verified_vm_image_with_node_agent_sku 来获取图像参考。同样的错误 The value provided for one of the properties in the request body is invalid.
我用你的代码做了测试,得到了同样的错误。然后我研究并更改代码中的一些内容。以及两件事引起的问题。
第一个:
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',
您应该使用真实的 VM 大小设置 vmSize
。例如,Standard_A1。参见 Choose a VM size for compute nodes in an Azure Batch pool。
希望这对您有所帮助。如果您需要更多帮助,请给我留言。
我认为网上有很多令人困惑的示例,或者它们只是匹配旧版本的 SDK。
深入研究我发现的文档 this。
cloud_service_configuration CloudServiceConfiguration The cloud
service configuration for the pool. This property and
virtualMachineConfiguration are mutually exclusive and one of the
properties must be specified. This property cannot be specified if the
Batch account was created with its poolAllocationMode property set to
'UserSubscription'.
在我的例子中,我只能使用
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
)
我正在尝试创建一个基于标准市场 ubuntu 图像的池。我正在使用 Azure 4.0.0, image refernce, vm 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 编辑器)获取的一些图像值:
>
"imageReference": {
"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 which is using this helper select_latest_verified_vm_image_with_node_agent_sku 来获取图像参考。同样的错误 The value provided for one of the properties in the request body is invalid.
我用你的代码做了测试,得到了同样的错误。然后我研究并更改代码中的一些内容。以及两件事引起的问题。
第一个:
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',
您应该使用真实的 VM 大小设置 vmSize
。例如,Standard_A1。参见 Choose a VM size for compute nodes in an Azure Batch pool。
希望这对您有所帮助。如果您需要更多帮助,请给我留言。
我认为网上有很多令人困惑的示例,或者它们只是匹配旧版本的 SDK。
深入研究我发现的文档 this。
cloud_service_configuration CloudServiceConfiguration The cloud service configuration for the pool. This property and virtualMachineConfiguration are mutually exclusive and one of the properties must be specified. This property cannot be specified if the Batch account was created with its poolAllocationMode property set to 'UserSubscription'.
在我的例子中,我只能使用
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
)