Azure 托管磁盘是用于 Azure 虚拟机和 Azure VMware 解决方案的高性能持久块存储。 它们简化了磁盘管理,提供更大的可伸缩性,增强安全性,并无需直接管理存储帐户。 有关详细信息,请参阅 Azure 托管磁盘。
对于与现有 VM 关联的托管磁盘上的操作,请使用 azure-mgmt-compute
库。
本文中的代码示例演示了使用 azure-mgmt-compute
库进行托管磁盘的常见操作。 这些示例不应作为独立脚本运行,而是要集成到自己的代码中。 若要了解如何从azure.mgmt.compute
脚本中创建ComputeManagementClient
实例,请参阅示例 - 创建虚拟机。
有关如何使用 azure-mgmt-compute
库的更完整示例,请参阅 GitHub 中 用于计算的适用于 Python 的 Azure SDK 示例 。
独立托管磁盘
以下示例演示了预配独立托管磁盘的不同方法。
创建空托管磁盘
此示例演示如何创建新的空托管磁盘。 您可以将其用作空盘,附加到虚拟机,或用作创建快照或映像的起点。
from azure.mgmt.compute.models import DiskCreateOption
poller = compute_client.disks.begin_create_or_update(
'my_resource_group',
'my_disk_name',
{
'___location': 'eastus',
'disk_size_gb': 20,
'creation_data': {
'create_option': DiskCreateOption.empty
}
}
)
disk_resource = poller.result()
从 Blob 存储中创建托管磁盘
此示例演示如何从 Azure Blob 存储中存储的 VHD 文件创建托管磁盘。 如果要重复使用或将现有虚拟硬盘移动到 Azure,这非常有用。
from azure.mgmt.compute.models import DiskCreateOption
poller = compute_client.disks.begin_create_or_update(
'my_resource_group',
'my_disk_name',
{
'___location': 'eastus',
'creation_data': {
'create_option': DiskCreateOption.IMPORT,
'storage_account_id': '/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>',
'source_uri': 'https://<storage-account-name>.blob.core.windows.net/vm-images/test.vhd'
}
}
)
disk_resource = poller.result()
从 Blob 存储创建托管磁盘映像
此示例演示如何从 Azure Blob 存储中存储的 VHD 文件创建托管磁盘映像。 如果要创建可用于创建新虚拟机的可重用映像,这非常有用。
from azure.mgmt.compute.models import OperatingSystemStateTypes, HyperVGeneration
poller = compute_client.images.begin_create_or_update(
'my_resource_group',
'my_image_name',
{
'___location': 'eastus',
'storage_profile': {
'os_disk': {
'os_type': 'Linux',
'os_state': OperatingSystemStateTypes.GENERALIZED,
'blob_uri': 'https://<storage-account-name>.blob.core.windows.net/vm-images/test.vhd',
'caching': "ReadWrite",
},
},
'hyper_v_generation': HyperVGeneration.V2,
}
)
image_resource = poller.result()
从自己的映像创建托管磁盘
此示例演示如何通过复制现有托管磁盘来创建新的托管磁盘。 如果要进行备份或使用另一个虚拟机上的相同磁盘设置,这非常有用。
from azure.mgmt.compute.models import DiskCreateOption
# If you don't know the id, do a 'get' like this to obtain it
managed_disk = compute_client.disks.get(self.group_name, 'myImageDisk')
poller = compute_client.disks.begin_create_or_update(
'my_resource_group',
'my_disk_name',
{
'___location': 'eastus',
'creation_data': {
'create_option': DiskCreateOption.COPY,
'source_resource_id': managed_disk.id
}
}
)
disk_resource = poller.result()
包含托管磁盘的虚拟机
可以使用基于特定磁盘映像隐式创建的托管磁盘创建虚拟机,而无需手动定义所有磁盘详细信息。
从 Azure 中的 OS 映像创建 VM 时,隐式创建托管磁盘。 Azure 会自动处理存储帐户,因此无需手动指定 storage_profile.os_disk
或创建存储帐户。
storage_profile = azure.mgmt.compute.models.StorageProfile(
image_reference = azure.mgmt.compute.models.ImageReference(
publisher='Canonical',
offer='UbuntuServer',
sku='16.04-LTS',
version='latest'
)
)
有关演示如何使用适用于 Python 的 Azure 管理库创建虚拟机的完整示例,请参阅 示例 - 创建虚拟机。 此示例演示如何使用 storage_profile
参数。
还可以从自己的图片创建一个 storage_profile
。
# If you don't know the id, do a 'get' like this to obtain it
image = compute_client.images.get(self.group_name, 'myImageDisk')
storage_profile = azure.mgmt.compute.models.StorageProfile(
image_reference = azure.mgmt.compute.models.ImageReference(
id = image.id
)
)
可以轻松附加以前预配的托管磁盘:
vm = compute_client.virtual_machines.get(
'my_resource_group',
'my_vm'
)
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
vm.storage_profile.data_disks.append({
'lun': 12, # You choose the value, depending of what is available for you
'name': managed_disk.name,
'create_option': DiskCreateOptionTypes.attach,
'managed_disk': {
'id': managed_disk.id
}
})
async_update = compute_client.virtual_machines.begin_create_or_update(
'my_resource_group',
vm.name,
vm,
)
async_update.wait()
具有托管磁盘的虚拟机规模集
在 Azure 托管磁盘之前,必须手动为虚拟机规模集中的每个 VM 创建存储帐户,并使用 vhd_containers
参数在规模集 REST API 中指定这些存储帐户。
使用 Azure 托管磁盘时,不再需要存储帐户管理。 因此,用于虚拟机规模集的storage_profile
虚拟机规模集现在可以匹配用于单个 VM 创建的虚拟机规模集:
'storage_profile': {
'image_reference': {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "16.04-LTS",
"version": "latest"
}
},
完整示例如下所示:
naming_infix = "PyTestInfix"
vmss_parameters = {
'___location': self.region,
"overprovision": True,
"upgrade_policy": {
"mode": "Manual"
},
'sku': {
'name': 'Standard_A1',
'tier': 'Standard',
'capacity': 5
},
'virtual_machine_profile': {
'storage_profile': {
'image_reference': {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "16.04-LTS",
"version": "latest"
}
},
'os_profile': {
'computer_name_prefix': naming_infix,
'admin_username': 'Foo12',
'admin_password': 'BaR@123!!!!',
},
'network_profile': {
'network_interface_configurations' : [{
'name': naming_infix + 'nic',
"primary": True,
'ip_configurations': [{
'name': naming_infix + 'ipconfig',
'subnet': {
'id': subnet.id
}
}]
}]
}
}
}
# Create VMSS test
result_create = compute_client.virtual_machine_scale_sets.begin_create_or_update(
'my_resource_group',
'my_scale_set',
vmss_parameters,
)
vmss_result = result_create.result()
使用托管磁盘的其他操作
调整托管磁盘的大小
此示例演示如何使现有托管磁盘变大。 当你需要更多空间用于数据或应用程序时,这非常有用。
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
managed_disk.disk_size_gb = 25
async_update = self.compute_client.disks.begin_create_or_update(
'my_resource_group',
'myDisk',
managed_disk
)
async_update.wait()
更新托管磁盘的存储帐户类型
此示例演示如何更改托管磁盘的存储类型并使其变大。 当你需要更多空间或更好的数据或应用程序的性能时,这非常有用。
from azure.mgmt.compute.models import StorageAccountTypes
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
managed_disk.account_type = StorageAccountTypes.STANDARD_LRS
async_update = self.compute_client.disks.begin_create_or_update(
'my_resource_group',
'myDisk',
managed_disk
)
async_update.wait()
从 Blob 存储中创建图像
此示例演示如何从 Azure Blob 存储中存储的 VHD 文件创建托管磁盘映像。 如果要创建可用于创建新虚拟机的可重用映像,这非常有用。
async_create_image = compute_client.images.create_or_update(
'my_resource_group',
'myImage',
{
'___location': 'eastus',
'storage_profile': {
'os_disk': {
'os_type': 'Linux',
'os_state': "Generalized",
'blob_uri': 'https://<storage-account-name>.blob.core.windows.net/vm-images/test.vhd',
'caching': "ReadWrite",
}
}
}
)
image = async_create_image.result()
创建当前附加到虚拟机的托管磁盘的快照
此示例演示如何拍摄附加到虚拟机的托管磁盘的快照。 如果需要,可以使用快照备份磁盘或还原磁盘。
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
async_snapshot_creation = self.compute_client.snapshots.begin_create_or_update(
'my_resource_group',
'mySnapshot',
{
'___location': 'eastus',
'creation_data': {
'create_option': 'Copy',
'source_uri': managed_disk.id
}
}
)
snapshot = async_snapshot_creation.result()