Azure 受控磁碟是高效能、持久的區塊記憶體,專為與 Azure 虛擬機和 Azure VMware 解決方案搭配使用而設計。 它們可簡化磁碟管理、提供更高的延展性、增強安全性,並不需要直接管理記憶體帳戶。 如需詳細資訊,請參閱 Azure 受控磁碟。
針對與現有 VM 相關聯的受管理的磁碟作業,請使用 azure-mgmt-compute 庫。
本文中的程式代碼範例示範使用 azure-mgmt-compute 庫搭配受控磁碟的一般作業。 這些範例並非要以獨立腳本的形式執行,而是要整合到您自己的程式代碼中。 若要瞭解如何在ComputeManagementClient腳本中建立azure.mgmt.compute實例,請參閱範例 - 建立虛擬機。
如需更完整的 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()