使用適用於 Python 的 Azure 函式庫與開發套件 (SDK) 來管理 Azure 受控磁碟

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 的作業系統映像檔建立虛擬機時,隱含地建立了一個管理磁碟。 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'
    )
)

若想了解如何利用Azure管理函式Python庫建立虛擬機的完整範例,請參見範例 - 建立虛擬機。 此範例示範如何使用 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 現在可以與建立個別虛擬機器時所使用的相同:

'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()

另請參閱