Gunakan Azure Managed Disks dengan pustaka Azure (SDK) untuk Python

Disk Terkelola Azure adalah penyimpanan blok berkinerja tinggi dan tahan lama yang dirancang untuk digunakan dengan Azure Virtual Machines dan Azure VMware Solution. Azure Managed Disks menyediakan manajemen disk yang disederhanakan, skalabilitas yang ditingkatkan, keamanan yang ditingkatkan, dan penskalaan yang lebih baik tanpa harus bekerja langsung dengan akun penyimpanan. Untuk informasi selengkapnya, lihat Disk Terkelola Azure.

Anda menggunakan azure-mgmt-compute pustaka untuk mengelola Disk Terkelola untuk komputer virtual yang ada.

Untuk contoh cara membuat komputer virtual dengan azure-mgmt-compute pustaka, lihat Contoh - Membuat komputer virtual.

Contoh kode dalam artikel ini menunjukkan cara melakukan beberapa tugas umum dengan disk terkelola menggunakan azure-mgmt-compute pustaka. Mereka tidak dapat dijalankan apa adanya, tetapi dirancang untuk Anda masukkan ke dalam kode Anda sendiri. Anda dapat berkonsultasi Contoh - Buat komputer virtual untuk mempelajari cara membuat instans azure.mgmt.compute ComputeManagementClient dalam kode Anda untuk menjalankan contoh.

Untuk contoh lengkap selengkapnya tentang cara menggunakan azure-mgmt-compute pustaka, lihat Azure SDK untuk sampel Python untuk komputasi di GitHub.

Managed Disk Mandiri

Anda dapat membuat Disk Terkelola mandiri dalam banyak cara seperti yang diilustrasikan di bagian berikut.

Buat Managed Disk kosong

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

Buat Managed Disk dari penyimpanan blob

Disk terkelola dibuat dari hard disk virtual (VHD) yang disimpan sebagai blob.

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

Buat citra Managed Disk dari penyimpanan blob

Gambar disk terkelola dibuat dari hard disk virtual (VHD) yang disimpan sebagai blob.

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

Buat Managed Disk dari citra Anda sendiri

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

Mesin virtual dengan Managed Disk

Anda dapat membuat Virtual Machine dengan Managed Disk implisit untuk citra disk tertentu, yang membebaskan Anda dari menentukan semua detail.

Disk Terkelola dibuat secara implisit saat membuat VM dari gambar OS di Azure. Dalam parameter storage_profile, os_disk bersifat opsional dan Anda tidak perlu membuat akun penyimpanan sesuai prasyarat yang diperlukan untuk membuat Virtual Machine.

storage_profile = azure.mgmt.compute.models.StorageProfile(
    image_reference = azure.mgmt.compute.models.ImageReference(
        publisher='Canonical',
        offer='UbuntuServer',
        sku='16.04-LTS',
        version='latest'
    )
)

Untuk contoh lengkap tentang cara membuat komputer virtual menggunakan pustaka manajemen Azure, untuk Python, lihat Contoh - Membuat komputer virtual. Dalam contoh buat, Anda menggunakan storage_profile parameter .

Anda juga dapat membuat storage_profile dari citra Anda sendiri:

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

Anda dapat dengan mudah melampirkan Disk Terkelola yang disediakan sebelumnya:

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

Virtual Machine Scale Sets dengan Disk Terkelola

Sebelum Managed Disk, Anda perlu membuat akun penyimpanan secara manual untuk semua VM yang Anda inginkan di dalam Scale Set Anda, lalu gunakan parameter daftar vhd_containers untuk menyediakan semua nama akun penyimpanan ke Scale Set RestAPI.

Karena Anda tidak perlu mengelola akun penyimpanan dengan Azure Managed Disks, Set Skala Komputer Virtual Anda storage_profile sekarang bisa sama persis dengan yang digunakan dalam pembuatan VM:

'storage_profile': {
    'image_reference': {
        "publisher": "Canonical",
        "offer": "UbuntuServer",
        "sku": "16.04-LTS",
        "version": "latest"
    }
},

Sampel lengkapnya adalah sebagai berikut:

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

Operasi lain dengan Managed Disk

Mengubah Ukuran Managed Disk

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

Perbarui jenis akun penyimpanan Managed Disk

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

Buat citra dari penyimpanan 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()

Buat snapshot Managed Disk yang saat ini dilampirkan ke mesin virtual

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

Lihat juga