Azure Autoscaling with VMSS and Managed Disks, Part 3: Vertical Autoscaling

save money azure heroimg

In part two of this series, I showed you how to autoscale virtual machines (VMs) using Virtual Machine Scale Sets (VMSS) and Managed Disks behind a load balancer. In part three, I’ll look at how to configure vertical scaling (up and down) as an alternative to the default horizontal scaling (in and out). For more information on VMSS, Managed Disks, and vertical versus horizontal scaling, read part one and two of the series.

 

 

Azure Autoscale uses horizontal scaling by default. This is the best solution to avoid interruptions in service because it does not require rebooting VMs. But vertical scaling can be achieved with the help of metrics, VMSS alerts, and Azure Automation runbooks. As you have probably guessed, it is more complicated to set up, test, and maintain. Nevertheless, I will show you how to configure a runbook to scale down VMs in the VMSS if CPU utilization drops below five percent. The same method can be used to scale up VMs.

Create an Azure Automation Run as Account

Before you can use a runbook for vertical scaling, you will need an Azure Automation account with a Run as Account. This allows the runbook to authenticate with Azure Resource Manager (ARM) resources.

  • Log in to the Azure management portal here.
  • Scroll to the bottom of the list of services on the far left and click More services.
  • In the Filter field, type automation and click Automation Accounts in the results.
  • At the top of the Automation Accounts pane, click + Add.
  • In the Add Automation Account pane, give the new account a name in the Name field.
  • Select the Azure subscription in which you want to create the new account using the Subscription drop-down menu.
  • Check Use existing above the Resource group field.
  • Using the Resource group drop-down menu, select the resource group created in part 2 of this series.
  • Change the location using the Location drop-down menu if required.
  • Make sure that Yes is selected under Create Azure Run As Account.
  • Click Create at the bottom of the pane.
Add an Azure Automation account (Image Credit: Russell Smith)
Add an Azure Automation Account (Image Credit: Russell Smith)

The new automation account will appear in the Automation Accounts pane when deployment has completed. You might need to click Refresh to see if the pane is still open.

Import and Publish a PowerShell Runbook

Microsoft has created four runbooks for vertical scaling VMs in a VMSS. You can add them to your Azure Automation account by downloading them from the gallery.

Import PowerShell runbooks (Image Credit: Russell Smith)
Import PowerShell Runbooks (Image Credit: Russell Smith)
  • On the Automation Accounts pane, click the new automation account to open it.
  • On the Automation Account pane, click the Runbooks tile on the right.
  • At the top of the Runbooks pane, click Browse gallery.
  • In the Browse gallery pane, type scale in the Filter box. Press ENTER.
  • Click on each of the following runbooks and then click Import in the runbook’s pane. To confirm the operation, click OK in the Import pane each time.
    • Scale Down Virtual Machine Scale Set Instances
    • Scale Up Virtual Machine Scale Set Instances
    • Scale Down and Reprovision Virtual Machine Scale Set Instances
    • Scale Up and Reprovision Virtual Machine Scale Set Instances
  • Close the Browse gallery pane, and you will see the four new runbooks on the Runbooks pane.
Add a webhook (Image Credit: Russell Smith)
Add a Webhook (Image Credit: Russell Smith)

Before you can use an imported runbook, you need to publish it. First, decide which runbook you want to use. There are two runbooks that scale down VMs in a VMSS. You can choose to scale down, or scale down and reprovision. The latter choice removes existing VMs and replaces them with new ones. In this article, I will use a runbook that does not reprovision the VMs.

  • On the Runbooks pane, open the runbook you want to publish by clicking it in the list.
  • On the pane for the open runbook, click Edit in the list of options on the top right.
  • On the Edit PowerShell Runbook pane, click Publish.
  • Click Yes to confirm the operation.

Create a Webhook

A webhook is a URL that will allow a VMSS alert to send information to the runbook. You want to do this so that an action can be automatically triggered.

  • Back on the Runbook pane, click Webhook at the top of the pane.
  • On the Add Webhook pane, click Create new webhook.
  • Give the webhook a name on the Create a new webhook pane in the Name field.
  • Modify the expiration date of the webhook if required.
  • Before clicking OK, click the Clipboard icon to the right of the URL field. You will need the URL in the next step. Make sure you copy it somewhere safe.
  • Click OK.
  • Now click Create parameters and run settings on the left of the Add Webhook pane.
  • On the Parameters pane, leave the default settings. Click OK.
  • On the Add Webhook pane, click Create to add the webhook to the runbook.
Add a webhook (Image Credit: Russell Smith)
Add a Webhook (Image Credit: Russell Smith)

Add an Alert to the VMSS

The final step is to add an alert to the VMSS. This will raise the alarm when CPU usage rises above five percent. You will need the URL of the webhook that was created above.

  • Scroll to the bottom of the list of services on the far left, and click More services.
  • In the Filter field, type scale and click Virtual machine scale sets in the results.
  • On the Virtual machine scale sets pane, click your VMSS in the list to open it.
  • Under MONITORING on the VMSS pane, click Alert rules.
  • Click Add metric alert at the top of the Alert rules window.
  • On the Add rule pane, give the new rule a name in the Name field. Add a description if required.
  • Scroll down to the Condition drop-down menu and select Less than.
  • In the Threshold field, type the percentage threshold on which you would like to generate an alert for CPU usage. I’m going to use 5.
  • Microsoft recommends setting a Period of at least 20-30 minutes to avoid triggering vertical scaling too often. I am going to select a period of Over the last 30 minutes.
  • In the Notify via section, copy the URL of the webhook you created in the previous steps into the Webhook field.
  • Click OK to add the rule.
  • The new rule will now appear in the Alert rules pane.
Add a VMSS alert (Image Credit: Russell Smith)
Add a VMSS Alert (Image Credit: Russell Smith)

Now all you need to do is wait for an alert to be generated. This alert will trigger the runbook via the webhook. Note that you cannot trigger the runbook manually for testing. It is designed to work only when triggered by an alert. It is also worth noting that not all VMs sizes can be scaled down. For more information on which VMs sizes can be scaled, open the runbook code by clicking View at the top of the runbook’s pane.

View the runbook code (Image Credit: Russell Smith)
View the Runbook Code (Image Credit: Russell Smith)

In this article, I showed you how to configure vertical scaling for VMSS using Azure Automation and a PowerShell runbook.