Share via


搭配適用於 Python 的 Azure 連結庫使用 Azure 受控磁碟

Azure 受控磁碟是高效能、持久性區塊儲存體,其設計用於與 Azure 虛擬機器和 Azure VMware 解決方案搭配使用。 Azure 受控磁碟 提供簡化的磁碟管理、增強的延展性、改善的安全性,以及更好的調整,而不需要直接使用記憶體帳戶。 如需詳細資訊,請參閱 Azure 受控磁碟

您可以使用連結azure-mgmt-compute庫來管理現有虛擬機 受控磁碟。

如需如何使用連結庫建立虛擬機 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 記憶體建立受控磁碟

受控磁碟是從儲存為 Blob 的虛擬硬碟 (VHD) 建立。

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 記憶體建立受控磁碟映像

受控磁碟映像是從儲存為 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 時,會隱含建立受控磁碟。 在 參數中 storage_profileos_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 管理連結庫建立虛擬機的完整範例, 請參閱範例 - 建立虛擬機。 在建立範例中,您會使用 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()

使用 受控磁碟 虛擬機器擴展集

在 受控磁碟 之前,您必須手動為擴展集內想要的所有 VM 建立記憶體帳戶,然後使用 list 參數vhd_containers,將所有記憶體帳戶名稱提供給擴展集 RestAPI。

因為您不需要使用 Azure 受控磁碟 管理記憶體帳戶,因此storage_profile您的 for 虛擬機器擴展集 現在與 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 記憶體建立映像

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

另請參閱