Introduction
In this article, we are going through some advanced functionalities in order to take maximum advantage of your Azure Virtual Machines. Each advanced functionality comes with a practical sample and we are going through it,
- Configuring the Availability Set of your Virtual Machine, in order to raise the availability up to 99.99% uptime guaranteed by SLA;
- Executing PowerShell script into your Virtual Machine, through the Run Command;
- How to migrate existing VHDs, to run on Azure Virtual Machines;
In order to understand better this article, please check my previous article explaining the basics of Azure Virtual Machines,
Manage the availability of your Azure VMs
To take advantage of Azure 99,99% uptime guaranteed SLA you must take some steps as follows,
Availability sets
Availability sets can protect you from:
- System Reboots, to apply updates;
- Hardware failures;
- Network problems;
- Lack of energy.
But... how?
One availability set contains a set of update domains and fault domains that run in the same datacenter.
Update domains are where redundancy is applied to avoid a system reboot to impact all your Virtual Machines, in the same update domain group, due to updates in the Azure platform.
Fault domains are where redundancy is applied to avoid faults(hardware, energy or network problems) on hardware or software to impact all your Virtual Machines running in the same fault domain group.
When deploying Virtual Machines into a single Availability Set, the Availability Set is going to distribute your VMs automatically among the Fault & Update Domains as follows:
Availability Zones
Availability Zones are Availability Sets running in different datacenter locations in the same Azure Region. It means that they are physically separated within each other, preventing data center failures and raising the guaranteed uptime SLA to 99.99%.`
Azure managed disks
Azure Managed Disks are physical disks but virtualized. It brings lots of benefits to your Virtual Machine as follows:
- Highly available, is designed to have 99,999% uptime having 3 replicas of your data;
- Highly durable, having a 0% failure rate;
- Integrated with Availability Sets, to avoid single point failure;
- Scalability, being able to deploy up to 1.000 VMs per scale set;
- Azure Backup, managing and scheduling your backups;
- Direct upload, being easier to handle your disk data.
Manage OS Upgrades with scheduled events
With Scheduled Events, you may plan for Virtual Machine maintenance in order to avoid any impact on your clients and services as far as your Virtual Machine will need to go down for a while.
Metadata Service API
- If you have VNet in your Virtual Machine enabled, this is the endpoint at today's date,
http://169.254.169.254/metadata/scheduledevents?api-version=2019-01-01
- For Virtual Machines without VNet enabled, you must follow this tutorial to discover your endpoint,
https://github.com/azure-samples/virtual-machines-python-scheduled-events-discover-endpoint-for-non-vnet-vm
Samples for Querying the API
You must make those calls from your Virtual Machine PowerShell
Request for Events:
- $metadataEndpoint = 'http://169.254.169.254/metadata/scheduledevents?api-version=2019-01-01'
-
- Invoke-RestMethod -Headers @{"Metadata"="true"} -URI $metadataEndpoint -Method get
Starting an Event:
- $metadataEndpoint = 'http://169.254.169.254/metadata/scheduledevents?api-version=2019-01-01'
-
- Invoke-RestMethod -Uri $metadataEndpoint -Headers @{"Metadata"="true"} -Method POST -Body '{"StartRequests": [{"EventId": "<your event id goes here>"}]}'
Enabling and disabling Scheduled Events
- Enabled automatically after making a request to the Metadata Service API;
- Disabled automatically after not making any request to the Metadata Service API for 24 hours.
P.S: Scheduled events are not available in every Azure Region, check region availability:
Using a Load Balancer to distribute incoming calls
The Load Balancer is a great tool to distribute your incoming request through your Virtual Machines in order to do not overload any Virtual Machine, distributing requests among your available Virtual Machines according to your needs.
Health Probe
Health probes are going to constantly run a health check, based on configurations set by you, against your VMs in order to set your VMs to be available or unavailable to receive requests.
We create rules in the Load Balancer to define how the incoming traffic flow is going to be distributed among the available Virtual Machines. You may also configure if you want a Health Probe running with your rule in order to do not distribute incoming traffic to your unhealthy VMs.
Application Tiers
Applications that make usage of their software architecture being grouped in tiers may take advantage of Application Tiers, in order to group your tiers in a different group of Virtual Machines. So you're able to separate your application tiers in different availability zones or availability sets.
A good practice would be to have each tier of your application in a different Application Tier and having each Application Tier in a different Availability Set/Zone. Remembering that an Availability Set/Zone works better with multiple Virtual Machines.
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/manage-availability
Running PowerShell scripts in your Azure Windows VM
You may use the Run Command feature to run PowerShell commands remotely by your VM Agent in your Virtual Machine. It offers a fast and safe way to run commands in your Virtual Machine in order to diagnose, troubleshoot and remediate issues that are affecting your VM.
Available commands to run through the run command:
- RunPowerShellScript, where you may run your custom script;
- EnableRemotePS, enabling your VM to accept remote PowerShell commands;
- EnableAdminAccount, enabling admin account;
- IPConfig, working as normal IPConfig command;
- RDPSettings, showing registry and domain policy settings;
- ResetRDPCert, removing SSL certificate and resetting your RDP listener;
- SetRDPPort, setting the port number for Remote Desktop Access.
Running scripts remotely has some limitations, as Microsoft describes:
- Script Output is limited to the last 4,096 bytes.
- The minimum time to run a script is about 20 seconds.
- Scripts run as System on Windows.
- One script at a time can run.
- Scripts that prompt for information (interactive mode) are not supported.
- You can't cancel a running script.
- The maximum time a script can run is 90 minutes. After that, it will time out.
- Outbound connectivity from the VM is required to return the results of the script.
Migrating existing VHDs to Azure VM
If you have an existing VHD and wants to migrate to Azure Virtual Machine you can migrate it without any problem using PowerShell, as follows:
Variables used around code,
- $diskName= "sampleDisk"
- $diskLocalPath="C:\myFolder\disk.vhd"
- $imageName="sampleImage"
- $location= "yourLocation"
- $resourceGroup= "sampleResourceGroup"
- $virtualMachineName="sampleVm"
Upload your VHD,
- #grants a temporary write access
- $diskTempAccess = Grant-AzDiskAccess -ResourceGroupName $resourceGroup -DiskName $diskName -DurationInSecond 86400 -Access 'Write'
-
- #copy your disk
- AzCopy.exe copy $diskLocalPath $diskTempAccess.AccessSAS --blob-type PageBlob
Create an Image from your VHD,
- $disk = Get-AzDisk -ResourceGroupName $resourceGroup -DiskName $diskName
-
-
- $imageConfig = New-AzImageConfig
- -Location $location
- $imageConfig = Set-AzImageOsDisk
- -Image $imageConfig
- -OsState Generalized
- -OsType Windows
- -ManagedDiskId $disk.Id
-
- $image = New-AzImage
- -ImageName $imageName
- -ResourceGroupName $resourceGroup
- -Image $imageConfig
Create a new Virtual Machine using the copy of your VHD,
- New-AzVm `
- -ResourceGroupName $resourceGroup
- -Name $virtualMachineName
- -Image $image.Id
- -Location $location
- -VirtualNetworkName "your Vnet"
- -SubnetName "your subnet"
- -SecurityGroupName "your security group"
- -PublicIpAddressName "your ip"
- -OpenPorts 3389
Complete code
- $diskName= "sampleDisk"
- $diskLocalPath="C:\myFolder\disk.vhd"
- $imageName="sampleImage"
- $location= "yourLocation"
- $resourceGroup= "sampleResourceGroup"
- $virtualMachineName="sampleVm"
-
-
- #upload ------------------------------------------------------
- #grants a temporary write access
- $diskTempAccess = Grant-AzDiskAccess -ResourceGroupName $resourceGroup -DiskName $diskName -DurationInSecond 86400 -Access 'Write'
-
- #copy your disk
- AzCopy.exe copy $diskLocalPath $diskTempAccess.AccessSAS --blob-type PageBlob
-
-
- #create custom image -----------------------------------------
- $disk = Get-AzDisk -ResourceGroupName $resourceGroup -DiskName $diskName
-
-
- $imageConfig = New-AzImageConfig
- -Location $location
- $imageConfig = Set-AzImageOsDisk
- -Image $imageConfig
- -OsState Generalized
- -OsType Windows
- -ManagedDiskId $disk.Id
-
- $image = New-AzImage
- -ImageName $imageName
- -ResourceGroupName $resourceGroup
- -Image $imageConfig
-
-
- #create VM
- New-AzVm `
- -ResourceGroupName $resourceGroup
- -Name $virtualMachineName
- -Image $image.Id
- -Location $location
- -VirtualNetworkName "your Vnet"
- -SubnetName "your subnet"
- -SecurityGroupName "your security group"
- -PublicIpAddressName "your ip"
- -OpenPorts 3389
External References