Once we've created an Azure VM without an availability set, later if we want to add it to an availability set there is no obvious option on the Azure portal. We can add an availability set only while creating the VM.
The problem comes when we try to add always-on on IaaS SQL instance. One of the pre-requisites is IaaS VMs should be on an availability set. So to enable always-on on the existing IaaS VM either we have to re-create the VM or need to create a new one & install the required s/w from scratch.
To resolve this restriction and add an availability group to the existing Azure VM, we can follow the below steps sequentially,
- Get the list of attached OS + Data disk of existing VM
- Get the Network interface details.
- Remove the Azure Disk Encryption (ADE) if it's enabled.
- Remove the Azure VM (delete it)
- Now create the new VM with the same name & with Availability set.
- After re-creating the VM change the OS disks & Data disk [as noted in step-1].
- Start the VM and try to access it. [this VM start will take some time]
- Once you're able to access it & verify it's working, you can start enabling ADE (if required)
- The above-mentioned process is all manual and it will take some time. There is a well known PowerShell script to automate this. [except disabling & removing ADE]
Here is the PowerShell script to add/attach availability set to existing VM.
- # Set variables
- $resourceGroup = "<<your-resource-group>>"
- $vmName = "<<your-vm-name>>"
- $newAvailSetName = "<<your-availability-set-name>>"
-
- # Get the details of the VM to be moved to the Availability Set
- $originalVM = Get-AzVM `
- -ResourceGroupName $resourceGroup `
- -Name $vmName
-
- # Create new availability set if it does not exist
- $availSet = Get-AzAvailabilitySet `
- -ResourceGroupName $resourceGroup `
- -Name $newAvailSetName `
- -ErrorAction Ignore
- if (-Not $availSet) {
- $availSet = New-AzAvailabilitySet `
- -Location $originalVM.Location `
- -Name $newAvailSetName `
- -ResourceGroupName $resourceGroup `
- -PlatformFaultDomainCount 2 `
- -PlatformUpdateDomainCount 2 `
- -Sku Aligned
- }
-
- # Remove the original VM
- Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName
-
- # Create the basic configuration for the replacement VM.
- $newVM = New-AzVMConfig `
- -VMName $originalVM.Name `
- -VMSize $originalVM.HardwareProfile.VmSize `
- -AvailabilitySetId $availSet.Id
-
- # For a Linux VM, change the last parameter from -Windows to -Linux
- Set-AzVMOSDisk `
- -VM $newVM -CreateOption Attach `
- -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
- -Name $originalVM.StorageProfile.OsDisk.Name `
- -Windows
-
- # Add Data Disks
- foreach ($disk in $originalVM.StorageProfile.DataDisks) {
- Add-AzVMDataDisk -VM $newVM `
- -Name $disk.Name `
- -ManagedDiskId $disk.ManagedDisk.Id `
- -Caching $disk.Caching `
- -Lun $disk.Lun `
- -DiskSizeInGB $disk.DiskSizeGB `
- -CreateOption Attach
- }
-
- # Add NIC(s) and keep the same NIC as primary
- foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {
- if ($nic.Primary -eq "True")
- {
- Add-AzVMNetworkInterface `
- -VM $newVM `
- -Id $nic.Id -Primary
- }
- else
- {
- Add-AzVMNetworkInterface `
- -VM $newVM `
- -Id $nic.Id
- }
- }
-
- # Recreate the VM
- New-AzVM `
- -ResourceGroupName $resourceGroup `
- -Location $originalVM.Location `
- -VM $newVM `
- -DisableBginfoExtension
With this, we're good to add the availability set to the existing Azure VM.
The above steps & PowerShell script was tested on Jun/July 2020.
Reference
- https://docs.microsoft.com/en-us/azure/virtual-machines/windows/change-availability-set