How to Use Azure Managed Disks
Azure Managed Disks are a powerful and flexible way to manage your virtual machine storage. This guide covers common operations and best practices.
What are Managed Disks?
Managed Disks are a resource manager-enabled storage solution that simplifies disk management for Azure Virtual Machines. They handle the underlying storage account creation, management, and replication.
Creating a Managed Disk
You can create managed disks using the Azure portal, Azure CLI, or PowerShell.
Using Azure CLI
To create a new managed disk, use the az disk create command:
az disk create \
--resource-group MyResourceGroup \
--name MyManagedDisk \
--sku Standard_LRS \
--size-gb 128 \
--location eastus
This command creates a 128 GB Standard HDD managed disk in the 'eastus' region within the 'MyResourceGroup' resource group.
Using Azure Portal
- Navigate to the Azure portal.
- Search for "Disks" and select it.
- Click "+ Create".
- Fill in the required details: Subscription, Resource group, Disk name, Region, and choose a Size and Performance tier.
- Click "Review + create".
Attaching a Managed Disk to a VM
Once created, you can attach a managed disk to an Azure Virtual Machine.
Using Azure CLI
az vm disk attach \
--resource-group MyResourceGroup \
--vm-name MyVM \
--name MyManagedDisk
This attaches the disk named 'MyManagedDisk' to the VM 'MyVM'.
Using Azure Portal
- Navigate to the Virtual Machine you want to attach the disk to.
- In the left-hand menu, select "Disks".
- Click "+ Add data disk".
- Select "Existing disks" and choose your managed disk from the dropdown.
- Click "Save".
Detaching a Managed Disk from a VM
To detach a disk, ensure the VM is stopped (deallocated) first.
Using Azure CLI
az vm disk detach \
--resource-group MyResourceGroup \
--vm-name MyVM \
--name MyManagedDisk
Using Azure Portal
- Navigate to the Virtual Machine.
- In the left-hand menu, select "Disks".
- Click on the data disk you wish to detach.
- Click "Detach disk".
- Click "Save" to apply the changes.
Resizing a Managed Disk
You can resize a managed disk without detaching it from the VM in most cases (though OS-level actions might be required).
Using Azure CLI
az disk update \
--resource-group MyResourceGroup \
--name MyManagedDisk \
--size-gb 256
This resizes the disk to 256 GB.
Common Scenarios and Best Practices
- Performance Tiers: Choose between Standard HDD, Standard SSD, Premium SSD, and Ultra Disk based on your application's I/O and latency requirements.
- Data Redundancy: Managed disks offer LRS (Locally Redundant Storage), ZRS (Zone Redundant Storage), and GRS (Geo-Redundant Storage) options. Select the appropriate redundancy for your disaster recovery needs.
- Snapshots: Take snapshots of your managed disks to create backups or to provision new disks.
- Encryption: Managed disks are encrypted by default at rest using platform-managed keys. You can also use customer-managed keys for enhanced control.