Adding an Azure Virtual Machine that uses managed disk into an availability set–manual process

Hi Everyone,

UPDATE (08/19/2017) : The PowerShell Module described below now supports both Managed and  Unmanaged Disks, so there is no need anymore to use this manual process unless you want to understand what happens behind the scenes of the module.

This blog post walks you through the manual process of adding an Azure virtual machine that uses managed disk to an availability set using a manual process purely inside the Azure portal (https://portal.azure.com). Note that if you have virtual machines using unmanaged disks that needs to be included in an availability set, you can use a PowerShell module posted at the TechNet Gallery using this link.

This blog assumes you have a great understanding of what is an availability set, if you don’t know what this is, please refer to Azure availability sets guidelines for Windows VMs and Manage the availability of Windows virtual machines in Azure. It also assume that you have the concepts of managed disks in Azure, if not, please refer to Azure Managed Disks Overview.

Finally we need the following items already deployed in Azure:

  • Availability set aligned to be used with managed disks
  • The virtual machine using managed disks that is not yet associated with an availability set

 

This example also assumes that you don’t have any data disk attached to your virtual machine, if that’s the case you need to change the dataDisks in a similar way I do with the osDisk property.

 

Steps

  1. Sign in at https://portal.azure.com
  2. Export the current template definition of you live resource (virtual machine in this case)
    1. At the top navigation bar of Azure Portal, click in the search field and type “resource explorer”

    2. Click on “resource explorer”

      image

    3. Easiest way to navigate over your resources is expanding Subscriptions->Subscription Name->Providers->Microsoft.Compute->virtualMachines->VM Name

      image

    4. On the details pane, where the template shows up, copy all its template contents and save it locally with notepad or any tool you may find appropriate.

    5. Close “Resource Explorer”

  3. Delete your virtual machine (yes, as of the day this article was written you can delete the virtual machine in Azure and the disks gets preserved)
  4. Create a new deployment
    1. Click on “+ New”, at the search field, type “template deployment” and press enter

      image

    2. Select “Template Deployment” from the “Everything” blade and click “Create”

      image

    3. At the “Custom deployment” blade, click “Edit template”. This will bring a bring a blank template structure.

      image

    4. At line 5, between the square brackets “[]”, paste the template you saved in the file.

      Blank template

      image

      Template after content was pasted

      image

    5. Delete the line that contains “vmId” property, in this guide this is line 7

    6. Replace the contents of imageReference:

      1. From

         "imageReference": {
             "publisher": "OpenLogic",
             "offer": "CentOS",
             "sku": "7.3",
             "version": "latest"
         },
        
      2. To

         "imageReference": null,
        
    7. Replace the contents of osDisk:

      1. From

         "osDisk": {
             "osType": "Linux",
             "name": "centos02",
             "createOption": "FromImage",
             "caching": "ReadWrite",
             "diskSizeGB": 31
         },
        
      2. To

         "osDisk": {
           "createOption": "Attach",
           "osType": "Linux",
           "managedDisk": {
             "id": "<Resource ID of your OS Disk>"
           }
         },
        

        Where <Resource ID of your OS Disk> needs to point to the resource ID of the currently existing OS Disk, which can be obtained by searching for the disk in the search field of the top navigation pane and copying the Resource ID property (note that you need to open a new tab on your internet browser)

        image

        image

    8. Replace the content of “osProfile”

      1. From

         "osProfile": {
             "computerName": "centos02",
             "adminUsername": "pmcadmin",
             "linuxConfiguration": {
                 "disablePasswordAuthentication": false
             },
             "secrets": []
         },
        
      2. To

         "osProfile": null,
        
    9. Insert the new element for the Availability Set. For that, go to the line where it starts with “provisioningState”, Insert a line before “provisioningState” and paste the following element (including the extra comma)

       "availabilitySet": {
         "id": "<Resource ID of the Availability Set>"
       },
      

      To obtain the resource Id:

      1. At the top navigation pane, click the search icon
      2. Type the availability set name at the search field
      3. Select the availability set
      4. Click “Properties”
      5. Copy the Resource Id by clicking at the copy icon (image)
    10. Notice that if your VM have any installed extension, you need to remove them from this template otherwise the deployment may fail, you can always add the extensions back later, dealing with extensions is out of scope of this blog post.

    11. Just after the very last property of your template, in my case “name”, which is the virtual machine name, add a comma at the end of the “name” line and add the following line (you must have a comma before this new “apiVersion”, otherwise validation will fail.

       "apiVersion": "2016-04-30-preview"
      
    12. Your template code should be similar to this

       {
         "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
         "contentVersion": "1.0.0.0",
         "parameters": {},
         "resources": [
           {
             "properties": {
               "hardwareProfile": {
                 "vmSize": "Standard_A1"
               },
               "storageProfile": {
                 "imageReference": null,
                 "osDisk": {
                   "createOption": "Attach",
                   "osType": "Linux",
                   "managedDisk": {
                     "id": "/subscriptions/<GUID>/resourceGroups/TEST03-RG/providers/Microsoft.Compute/disks/centos02"
                   }
                 },
                 "dataDisks": []
               },
               "osProfile": null,
               "networkProfile": {
                 "networkInterfaces": [
                   {
                     "id": "/subscriptions/<GUID>/resourceGroups/test03-rg/providers/Microsoft.Network/networkInterfaces/centos0287"
                   }
                 ]
               },
               "availabilitySet": {
                 "id": "<Resource ID of the Availability Set>"
               },
               "provisioningState": "Succeeded"
             },
             "type": "Microsoft.Compute/virtualMachines",
             "location": "westus",
             "id": "/subscriptions/<GUID>/resourceGroups/test03-rg/providers/Microsoft.Compute/virtualMachines/centos02",
             "name": "centos02",
             "apiVersion": "2016-04-30-preview"
           }
         ]
       }
      
    13. Click “Save”

    14. Select the same exact resource group where your virtual machine existed at the “Resource group” field and same location if available to choose, then click “I agree to the terms and conditions stated above” and click “Purchase”

      image

If any errors come up, you can always click in the notification icon at the top navigation pane and select the failed deployment:

 

image

 

This will bring the failed deployment blade

 

image

Then just click on “Redeploy” button so you can edit the template and fix any issues.

 

Again, if you have virtual machines using unmanaged disks that needs to be included in an availability set, you can use a PowerShell module that I developed and poste at the TechNet Gallery using this link.

That’s it for this blog and I hope this helps.