Azure Virtual Machine Scale Sets

VM Scale Sets (VMSS) are an ARM based Azure Compute resource you can use to deploy and manage a collection of virtual machines as a set. Scale sets are well suited for building large-scale services targeting big compute, big data, and containerized workloads – all of which are increasing in significance as cloud computing continues to evolve. Scale set VMs are configured identically, you just choose how many you need, which enables them to scale out and in rapidly and automatically.

Architecture

Infrastructure

VM Scale Sets are created using the following services:

  • Virtual Network
  • Storage Account
  • Public IP
  • Load Balancer (including dynamic NAT rules for access to the autoscaled systems)
  • VM Scale Set (created by one or more VM image)

A good place to view the scale set resources you create is in the Azure Resource Explorer, which allows you to see how every resource in your subscription is defined.

Administration

Starting and Stopping

Currently starting and stopping can only be done via PowerShell – the Portal UI is due to be enhanced to support VMSS features in the near future.

# Connect to Azure
Login-AzureRmAccount
# Note: to use Resource Manager services make sure you set your subscription using RM:
#Instead of Get-AzureSubscription, you are using Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionName "Development (no VPN)"
# Start all instances together
Start-AzureRmVmss -ResourceGroupName "rg_name" -VMScaleSetName "fireAnt02"
# to start specific instances only
Start-AzureRmVmss -ResourceGroupName "rg_name" -VMScaleSetName "fireAnt02" -InstanceId "0""1"
                               
# stop the scale set (all instances)
Stop-AzureRmVmss -ResourceGroupName "rg_name" -VMScaleSetName "fireAnt02"
# restart an instance
Restart-AzureRmVmss -ResourceGroupName "rg_name" -VMScaleSetName "fireAnt02" -InstanceId "0"

Scale

There are two ways to change Scale Sets, upload a new template or use the API (using PowerShell, for example). To use the API, set an object equal to all properties, make a change, and apply the change.

Manually Scale VMSS

Change the capacity of a scale set, get the set object using Get-AzureRmVmss, change the capacity property to whatever you want it to be, such as $vmss.sku.capacity = 12, and then run Update-AzureRmVmss with the changed object. For example:

$vmss = Get-AzureRmVmss -ResourceGroupName "rg_name" -VMScaleSetName "fireAnt02"
$vmss.Sku.Capacity = 12
Update-AzureRmVmss -ResourceGroupName "rg_name" -Name "fireAnt02" -VirtualMachineScaleSet $vmss

Note: If you reduce the Scale Sets capacity you may have to deal with service dependencies on the changed VMs. For Service Fabric that might mean removing/reducing the partition and removing the node state from the cluster.

More details from Service Fabric: The Remove-ServiceFabricNodeState cmdlet notifies Service Fabric that the state on a node which is down has been removed, for instance, if a hard disk crashes. This cmdlet informs Service Fabric that the replicas on that node no longer exist, and that Service Fabric should stop waiting for those replicas to come back up. Do not run this cmdlet if the state on the node has not been removed and the node subsequently comes back up with its state intact.

Remove-ServiceFabricNodeState -NodeName "_fireAnt02_10" -Force

Automatically Scale VMSS

A resource, such as a Web app, VM, Cloud Service or VM Scale Set can have only one autoscale setting configured for it. However, each autoscale setting can have multiple profiles. For example, one for a performance-based scale profile and a second one for a schedule based profile. Each profile can have multiple rules configured on it. For more information about Autoscale, see How to Autoscale an Application.

Here are the steps we will use:

    1. Create rule(s).
    2. Create profile(s) mapping the rules that you created previously to the profiles.
    3. Optional: Create notifications for autoscale by configuring webhook and email properties.
    4. Create an autoscale setting with a name on the target resource by mapping the profiles and notifications that you created in the previous steps.

The following examples show you how you can create an Autoscale setting for a VM scale set for a Windows operating system based by using the CPU utilization metric.

First, create a rule to scale-out, with an instance count increase .

$rule1 = New-AzureRmAutoscaleRule -MetricName "\Processor(_Total)\% Processor Time" -MetricResourceId /subscriptions/s1/resourceGroups/big2/providers/Microsoft.Compute/virtualMachineScaleSets/big2 -Operator GreaterThan -MetricStatistic Average -Threshold 0.01 -TimeGrain 00:01:00 -TimeWindow 00:10:00 -ScaleActionCooldown 00:10:00 -ScaleActionDirection Increase -ScaleActionScaleType ChangeCount -ScaleActionValue 1

Next, create a rule to scale-in, with an instance count decrease.

$rule2 = New-AzureRmAutoscaleRule -MetricName "\Processor(_Total)\% Processor Time" -MetricResourceId /subscriptions/s1/resourceGroups/big2/providers/Microsoft.Compute/virtualMachineScaleSets/big2 -Operator GreaterThan -MetricStatistic Average -Threshold -TimeGrain 00:01:00 -TimeWindow 00:10:00 -ScaleActionCooldown 00:10:00 -ScaleActionDirection Decrease -ScaleActionScaleType ChangeCount -ScaleActionValue 1

Then, create a profile for the rules.

$profile1 = New-AzureRmAutoscaleProfile -DefaultCapacity -MaximumCapacity 10 -MinimumCapacity -Rules $rule1,$rule2 -Name "My_Profile"

Create a webhook property.

$webhook_scale = New-AzureRmAutoscaleWebhook -ServiceUri "https://example.com?mytoken=mytokenvalue"

Create the notification property for the autoscale setting, including email and the webhook that you created previously.

$notification1= New-AzureRmAutoscaleNotification -CustomEmails ashwink@microsoft.com -SendEmailToSubscriptionAdministrators SendEmailToSubscriptionCoAdministrators -Webhooks $webhook_scale

Finally, create the autoscale setting to add the profile that you created above.

Add-AzureRmAutoscaleSetting -Location "East US" -Name "MyScaleVMSSSetting" -ResourceGroup big2 -TargetResourceId /subscriptions/s1/resourceGroups/big2/providers/Microsoft.Compute/virtualMachineScaleSets/big2 -AutoscaleProfiles $profile1 -Notifications $notification1

For more information about managing Autoscale settings, see Get-AutoscaleSetting.

Autoscale history

The following example shows you how you can view recent autoscale and alert events. Use the activity log search to view the autoscale history.

Get-AzureRmLog -Caller "Microsoft.Insights/autoscaleSettings" -DetailedOutput -StartTime 2015-03-01

You can use the Get-AzureRmAutoScaleHistory cmdlet to retrieve AutoScale history.

Get-AzureRmAutoScaleHistory -ResourceId /subscriptions/s1/resourceGroups/myrg1/providers/microsoft.insights/autoscalesettings/myScaleSetting -StartTime 2016-03-15 -DetailedOutput

For more information, see Get-AutoscaleHistory.

View details for an autoscale setting

You can use the Get-Autoscalesetting cmdlet to retrieve more information about the autoscale setting.

The following example shows details about all autoscale settings in the resource group ‘myrg1’.

Get-AzureRmAutoscalesetting -ResourceGroup myrg1 -DetailedOutput

The following example shows details about all autoscale settings in the resource group ‘myrg1’ and specifically the autoscale setting named ‘MyScaleVMSSSetting’.

Get-AzureRmAutoscalesetting -ResourceGroup myrg1 -Name MyScaleVMSSSetting -DetailedOutput

Remove an autoscale setting

You can use the Remove-Autoscalesetting cmdlet to delete an autoscale setting.

Remove-AzureRmAutoscalesetting -ResourceGroup myrg1 -Name MyScaleVMSSSetting

Reference: https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/insights-powershell-samples#create-and-manage-autoscale-settings

Configure Load Balancer

Configure the Load Balancer to pass a port among the cluster nodes.

  1. Open portal.azure.com, browse to the VM Scale Set.
  2. Click on the resource for the VMSS, this will open a long list of resources.
  3. Find the load balancer, which is named LB-servicename
  4. Click on Load Balancing Rules
  5. Click Add
  6. Fill out the form to add the new port. Keep in mind the following:
    1. Give the rule an appropriate name, for example: LBMonitoringHttpRule
    2. The Frontend IP address will be the public IP offered by the LB, found under the Inbound NAT rules.
    3. TCP, enter a port number.
    4. Backend Pool, use the LoadBalancerBEAddressPool, which will offer a list of all internal addresses used by nodes (10 network).
    5. Probe: FabricGatewayProbe (TCP:19000) will understand which nodes are running the web service and need the address pointed to the node.
    6. Session preference (your preference)
    7. Idle timeout (minutes), I like to use 5 minutes
    8. Floating IP will be disabled in this implementation