Microsoft Azure: Azure Managed Disk

Microsoft has rolled out Azure Managed Disks this month and its available for all to simplify the management, scaling, sizing of disk, etc for VMs.

Since i am still pretty much a .Net guy so download the Azure Management Libraries for .Net to manage Managed Disks.

GitHub Link: https://github.com/Azure/azure-sdk-for-net/tree/Fluent

Time to play 🙂

Virtual Machine with Managed Disk
​
​var linuxVM1 = azure.VirtualMachines
  .Define(linuxVM1Name)
  .WithRegion(Region.USEast)
  .WithNewResourceGroup(rgName)
  .WithNewPrimaryNetwork ("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithNewPrimaryPublicIpAddress(linuxVM1Pip)
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername(“tirekicker”)
  .WithSsh(sshkey)
  .WithNewDataDisk(100)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

Things to note: Its quite simple, easy and quick to create managed disks without specifying all the disk details and you don’t need to worry about Managing Storage Accounts anymore. Use define()…create() method chain to create a VM with Managed Disk for OS and explicit Managed Disks for data.

VM Scale Set with Managed Disks

​var vmScaleSet = azure.VirtualMachineScaleSets
  .Define(vmScaleSetName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithSku(VirtualMachineScaleSetSkuTypes.StandardD5v2)
  .WithExistingPrimaryNetworkSubnet(network, "subnet1")
  .WithExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer)
  .WithoutPrimaryInternalLoadBalancer()
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername("tirekicker")
  .WithSsh(sshkey)
  .WithNewDataDisk(100)
  .WithNewDataDisk(100, 1, CachingTypes.ReadWrite)
  .WithNewDataDisk(100, 2, CachingTypes.ReadOnly)
  .WithCapacity(3)
  .Create();

Attach Empty Managed Disk to VM
Like mentioned above, by using define().. create() you can accomplish this.

​var dataDisk = azure.Disks.Define(diskName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithData()
  .WithSizeInGB(50)
  .Create();

Again define()… create() method, this time to attach the empty Managed Disk to VM

​var linuxVM2 = azure.VirtualMachines.Define(linuxVM2Name)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork ("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithNewPrimaryPublicIpAddress(linuxVM2Pip)
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername("tirekicker")
  .WithSsh(sshkey)
  .WithNewDataDisk(100)
  .WithNewDataDisk(100, 1, CachingTypes.ReadWrite)
  .WithExistingDataDisk(dataDisk)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

To detach Managed Disks and attach new Managed Disks, you have to use an update() … apply() method chain

​linuxVM2.Update()
  .WithoutDataDisk(2)
  .WithNewDataDisk(200)
  .Apply();

VM from a Specialized VHD

​var linuxVM4 = azure.VirtualMachines.Define(linuxVmName3)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithoutPrimaryPublicIpAddress()
  .WithSpecializedOsUnmanagedDisk(specializedVhd, OperatingSystemTypes.Linux)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

VM using a Custom Image

​var virtualMachineCustomImage = azure.VirtualMachineCustomImages
  .Define(customImageName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .FromVirtualMachine(linuxVM) // from a de-allocated and generalized Virtual Machine
  .Create();

Custom Image with define()…create() method

​var linuxVM4 = azure.VirtualMachines.Define(linuxVM4Name)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithoutPrimaryPublicIpAddress()
  .WithLinuxCustomImage(virtualMachineCustomImage.Id)
  .WithRootUsername(userName)
  .WithSsh(sshkey)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

VM Using Specialized Disks from Snapshot

// Create a Snapshot for an operating system disk
var osDisk = azure.Disks.GetById(linuxVM.OsDiskId);
var osSnapshot = azure.Snapshots.Define(managedOSSnapshotName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithLinuxFromDisk(osDisk)
  .Create();

// Create a Managed Disk from the Snapshot for the operating system disk
var newOSDisk = azure.Disks.Define(managedNewOSDiskName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithLinuxFromSnapshot(osSnapshot)
  .WithSizeInGB(100)
  .Create();

// Create a Snapshot for a data disk
var dataSnapshot = azure.Snapshots.Define(managedDataDiskSnapshotName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithDataFromDisk(dataDisk)
  .WithSku(DiskSkuTypes.StandardLRS)
  .Create();

// Create a Managed Disk from the Snapshot for the data disk
var newDataDisk = azure.Disks.Define(managedNewDataDiskName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithData()
  .FromSnapshot(dataSnapshot)
  .Create();

var linuxVM5 = azure.VirtualMachines.Define(linuxVm5Name)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithoutPrimaryPublicIpAddress()
  .WithSpecializedOsDisk(newOSDisk, OperatingSystemTypes.Linux)
  .WithExistingDataDisk(newDataDisk)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

Convert VM to Use Managed Disk with a Single Rebot

​var linuxVM6 = azure.VirtualMachines.Define(linuxVM6Name)
  .WithRegion(Region.USEast)
  .WithNewResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithNewPrimaryPublicIpAddress(linuxVM6Pip)
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername("tirekicker")
  .WithSsh(sshkey)
  .WithUnmanagedDisks() // uses Storage Account
  .WithNewUnmanagedDataDisk(100) // uses Storage Account
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

linuxVM7.Deallocate();
linuxVM7.ConvertToManaged();

Enjoy guys and plenty more is coming 🙂

Leave a Reply