次の方法で共有


Python 用 Azure ライブラリ (SDK) で Azure Managed Disks を使用する

Azure Managed Disks は、Azure Virtual Machines および Azure VMware Solution で使用できるように設計された、高パフォーマンスで耐久性のあるブロック ストレージです。 これにより、ディスク管理が簡素化され、スケーラビリティが向上し、セキュリティが強化され、ストレージ アカウントを直接管理する必要がなくなります。 詳細については、「 Azure Managed Disks」を参照してください。

既存の VM に関連付けられている Managed Disks に対する操作については、 azure-mgmt-compute ライブラリを使用します。

この記事のコード例では、 azure-mgmt-compute ライブラリを使用した Managed Disks に対する一般的な操作を示します。 これらの例は、スタンドアロン スクリプトとして実行するのではなく、独自のコードに統合するためのものです。 スクリプト内のComputeManagementClientからazure.mgmt.compute インスタンスを作成する方法については、「例 - 仮想マシンを作成する」を参照してください。

azure-mgmt-compute ライブラリの使用方法の詳細な例については、GitHub のコンピューティング用の Azure SDK for Python サンプルを参照してください。

スタンドアロン マネージド ディスク

次の例では、スタンドアロン Managed Disks をプロビジョニングするさまざまな方法を示します。

空のマネージド ディスクを作成する

この例では、新しい空のマネージド ディスクを作成する方法を示します。 仮想マシンに接続するための空のディスクとして、またはスナップショットまたはイメージを作成するための開始点として使用できます。

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 Storage に格納されている 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 Storage に格納されている 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()

Managed Disks を使用する仮想マシン

特定のディスク イメージに基づいて暗黙的に作成されたマネージド ディスクを使用して仮想マシンを作成できるため、すべてのディスクの詳細を手動で定義する必要がなくなります。

マネージド ディスクは、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 Managed Disks の前に、仮想マシン スケール セット内の各 VM のストレージ アカウントを手動で作成し、 vhd_containers パラメーターを使用してスケール セット REST API でこれらのストレージ アカウントを指定する必要がありました。

Azure Managed Disks では、ストレージ アカウントの管理は不要になりました。 その結果、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 Disks を使用したその他の操作

マネージド ディスクのサイズ変更

この例では、既存のマネージド ディスクを大きくする方法を示します。 これは、データまたはアプリケーションの領域を増やす必要がある場合に便利です。

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

Managed Disks のストレージ アカウントの種類を更新する

この例では、マネージド ディスクのストレージの種類を変更し、サイズを大きくする方法を示します。 これは、データまたはアプリケーションの領域を増やしたり、パフォーマンスを向上させたりする必要がある場合に役立ちます。

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 Storage に格納されている 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()

こちらも参照ください