Java: Manage Azure Managed Disks

Publicado el 8 febrero, 2017

Principal Program Manager, Azure Developer Experience

We are announcing beta 5 of the Azure Management Libraries for Java. Beta 5 adds support for Azure Managed Disks.

Today, Microsoft announced the general availability of Azure Managed Disks – it simplifies the management and scaling of Virtual Machines. Specify the size and disk you want to use for Virtual Machines. You do not have to worry about creating and managing Storage Accounts.

You can use the Azure Management Libraries for Java to manage Managed Disks.

https://github.com/azure/azure-sdk-for-java

Add the following to your Maven POM file to use beta 5:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.0.0-beta5</version>
</dependency>

Create a Virtual Machine with Managed Disks

You can create a Virtual Machine with an implicit Managed Disk for the operating system and explicit Managed Disks for data using a define() … create() method chain. Creation is simplified with implicit creation of managed disks without specifying all the disk details. You do not have to worry about creating and managing Storage Accounts.

VirtualMachine linuxVM1 = azure.virtualMachines()
  .define(linuxVM1Name)
  .withRegion(Region.US_EAST)
  .withNewResourceGroup(rgName)
  .withNewPrimaryNetwork("10.0.0.0/28")
  .withPrimaryPrivateIpAddressDynamic()
  .withNewPrimaryPublicIpAddress(linuxVM1Pip)
  .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  .withRootUsername(“tirekicker”)
  .withSsh(sshkey)
  .withNewDataDisk(100)
  .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  .create();

You can download the full, ready-to-run sample code.

Create a Virtual Machine Scale Set with Managed Disks

You can create a Virtual Machine Scale Set with implicit Managed Disks for operating systems and explicit Managed Disks for data using a define() … create() method chain.

VirtualMachineScaleSet vmScaleSet = azure.virtualMachineScaleSets()
  .define(vmssName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D5_V2)
  .withExistingPrimaryNetworkSubnet(network, "subnet1")
  .withExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer)
  .withoutPrimaryInternalLoadBalancer()
  .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  .withRootUsername("tirekicker")
  .withSsh(sshkey)
  .withNewDataDisk(100)
  .withNewDataDisk(100, 1, CachingTypes.READ_WRITE)
  .withNewDataDisk(100, 2, CachingTypes.READ_ONLY)
  .withCapacity(10)
  .create();

You can download the full, ready-to-run sample code.

Create an Empty Managed Disk and Attach It to a Virtual Machine

You can create an empty Managed Disk using a define() … create() method chain.

Disk dataDisk = azure.disks().define(dataDiskName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withData()
  .withSizeInGB(50)
  .create();

You can attach the empty Managed Disk to a Virtual Machine using another define() ... create() method chain.

VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVmName2)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withNewPrimaryNetwork("10.0.0.0/28")
  .withPrimaryPrivateIpAddressDynamic()
  .withNewPrimaryPublicIpAddress(publicIpDnsLabel2)
  .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  .withRootUsername("tirekicker")
  .withSsh(sshkey)
  .withNewDataDisk(100)
  .withNewDataDisk(100, 1, CachingTypes.READ_WRITE)
  .withExistingDataDisk(dataDisk)
  .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  .create();

You can download the full, ready-to-run sample code.

Update a Virtual Machine

You can detach Managed Disks and attach new Managed Disks using an update() … apply() method chain.

linuxVM2.update()
  .withoutDataDisk(2)
  .withNewDataDisk(200)
  .apply();

You can download the full, ready-to-run sample code.

Create a Virtual Machine From a Specialized VHD

You can create a Virtual Machine from a Specialized VHD using a define() … create() method chain.

VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVmName3)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withNewPrimaryNetwork("10.0.0.0/28")
  .withPrimaryPrivateIpAddressDynamic()
  .withoutPrimaryPublicIpAddress()
  .withSpecializedOsUnmanagedDisk(specializedVhd, OperatingSystemTypes.LINUX) 
  .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  .create();

You can download the full, ready-to-run sample code.

Create a Virtual Machine Using a Custom Image

You can create a custom image from a de-allocated and generalized Virtual Machine using a define() … create() method chain.

VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages()
  .define(customImageName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .fromVirtualMachine(linuxVM) // from a de-allocated and generalized Virtual Machine
  .create();

You can create a Virtual Machine from the custom image using another define() … create() method chain.

VirtualMachine linuxVM4 = azure.virtualMachines().define(linuxVmName4)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withNewPrimaryNetwork("10.0.0.0/28")
  .withPrimaryPrivateIpAddressDynamic()
  .withoutPrimaryPublicIpAddress()
  .withLinuxCustomImage(virtualMachineCustomImage.id())
  .withRootUsername("tirekicker")
  .withSsh(sshKey)
  .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  .create();

You can download the full, ready-to-run sample code.

Create a Virtual Machine Using Specialized Disks From Snapshots

You can create a Managed Disk Snapshot for an operating system disk.

 

// Create a Snapshot for an operating system disk
Disk osDisk = azure.disks().getById(linuxVM.osDiskId());
Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withLinuxFromDisk(osDisk)
  .create();
// Create a Managed Disk from the Snapshot for the operating system disk
Disk newOSDisk = azure.disks().define(managedNewOSDiskName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withLinuxFromSnapshot(osSnapshot)
  .withSizeInGB(100)
  .create();

You can create a Managed Disk Snapshot for a data disk.

// Create a Snapshot for a data disk
Snapshot dataSnapshot = azure.snapshots().define(managedDataDiskSnapshotName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withDataFromDisk(dataDisk)
  .withSku(DiskSkuTypes.STANDARD_LRS)
  .create();
// Create a Managed Disk from the Snapshot for the data disk
Disk newDataDisk = azure.disks().define(managedNewDataDiskName)
  .withRegion(Region.US_EAST)
  .withExistingResourceGroup(rgName)
  .withData()
  .fromSnapshot(dataSnapshot)
  .create();

You can create a Virtual Machine from these specialized disks using a define() … create() method chain.

VirtualMachine linuxVM5 = azure.virtualMachines().define(linuxVmName5)
  .withRegion(region)
  .withExistingResourceGroup(rgName)
  .withNewPrimaryNetwork("10.0.0.0/28")
  .withPrimaryPrivateIpAddressDynamic()
  .withoutPrimaryPublicIpAddress()
  .withSpecializedOsDisk(newOSDisk, OperatingSystemTypes.LINUX)
  .withExistingDataDisk(newDataDisk)
  .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  .create();

You can download the full, ready-to-run sample code.

Convert a Virtual Machine to Use Managed Disks With a Single Reboot

You can convert a Virtual Machine with unmanaged disks (Storage Account based) to Managed Disks with a single reboot.

VirtualMachine linuxVM6 = azure.virtualMachines().define(linuxVmName6)
  .withRegion(Region.US_EAST)
  .withNewResourceGroup(rgName)
  .withNewPrimaryNetwork("10.0.0.0/28")
  .withPrimaryPrivateIpAddressDynamic()
  .withNewPrimaryPublicIpAddress(publicIpDnsLabel6)
  .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  .withRootUsername("tirekicker")
  .withSsh(sshKey)
  .withUnmanagedDisks() // uses storage accounts
  .withNewUnmanagedDataDisk(100)
  .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  .create();

linuxVM6.deallocate();
linuxVM6.convertToManaged();

You can download the full, ready-to-run sample code.

Try It

You can run the samples above or go straight to our GitHub repo. Give it a try and let us know what do you think (via e-mail or comments below). Over the next few weeks, we will be adding support for more Azure services and applying finishing touches to the API.

You can find plenty of additional info about Java on Azure at http://azure.com/java.