Home Microsoft AzureAzure VM Scale Sets: Autoscale Linux VMs the Easy Way

Azure VM Scale Sets: Autoscale Linux VMs the Easy Way

by Knowledge Base Linux
Azure VM scale sets multiplying virtual machines automatically behind a load balancer

Traffic doubles during a sale, and your single VM falls over. Adding VMs by hand works once, but it does not scale — and it does not scale back down when traffic drops, so you keep paying for idle capacity. Azure VM scale sets automate both directions: a group of identical, load-balanced VMs that grows when demand rises and shrinks when it falls, all from one configuration.

Azure VM scale sets adding and removing virtual machines based on a CPU usage gauge

This guide creates a Linux scale set with the Azure CLI, configures autoscaling on CPU, and covers the operational details that matter: health monitoring, upgrades, and safe scale-in. For backing up the VMs you run, see our Azure VM backup guide, and for deploying apps on Azure’s container platform, our Azure App Service guide.

Prerequisites

  • An Azure subscription and the Azure CLI (az) installed and logged in (az login).
  • A resource group to hold the scale set (we use web-rg in the examples).
  • Basic familiarity with Linux VMs on Azure; no prior scale set experience needed.

Creating an Azure VM Scale Set

A scale set starts from a VM image and a size, just like a single VM, but it creates a group of instances behind a load balancer. The simplest creation uses the Azure CLI with an Ubuntu image:

az group create --name web-rg --location eastus

az vmss create \
  --resource-group web-rg \
  --name web-vmss \
  --image Ubuntu2204 \
  --vm-sku Standard_B2s \
  --instance-count 2 \
  --admin-username azureuser \
  --generate-ssh-keys \
  --load-balancer web-lb \
  --vnet-name web-vnet \
  --subnet web-subnet

This creates two identical Ubuntu VMs behind a load balancer, each reachable through the load balancer rather than directly. Check the instances and their state:

az vmss list-instances --resource-group web-rg --name web-vmss --output table
az vmss show --resource-group web-rg --name web-vmss --query provisioningState

By default, scale sets use the uniform orchestration mode, which is the modern mode and the right default for new deployments. Instances are managed as a group: updating the scale set model (for example, a new image version) can roll out to instances according to the upgrade policy you choose.

Configuring Autoscale for Azure VM Scale Sets

A fixed instance count defeats the purpose, so the next step is an autoscale rule. The classic rule scales out when average CPU exceeds a threshold and scales in when it drops below one:

az monitor autoscale create \
  --resource-group web-rg \
  --resource web-vmss \
  --resource-type Microsoft.Compute/virtualMachineScaleSets \
  --name web-autoscale \
  --min-count 2 --max-count 6 --count 2

az monitor autoscale rule create \
  --resource-group web-rg \
  --autoscale-name web-autoscale \
  --condition "Percentage CPU > 70 avg 5m" \
  --scale out 1

az monitor autoscale rule create \
  --resource-group web-rg \
  --autoscale-name web-autoscale \
  --condition "Percentage CPU < 30 avg 10m" \
  --scale in 1

Three settings deserve attention. The minimum count is your availability floor — never set it to zero for a user-facing service unless cold starts are acceptable. The cooldown (default 5 minutes for scale-out, longer for scale-in in many configurations) prevents flapping, where the set scales out and back in repeatedly. And the metric matters: CPU works for compute-bound services, but request queue length or custom metrics are better for I/O-bound or queue-driven workloads.

For scheduled patterns — more capacity during business hours, less overnight — add a scheduled autoscale profile instead of relying only on metrics. Predictable traffic deserves a predictable schedule; metrics handle the surprises on top of it.

Health Monitoring, Upgrades, and Scale-In Safety

Autoscaling is only safe if unhealthy instances are replaced. Enable automatic repairs in the scale set’s settings so Azure replaces instances that fail health checks, and configure an application health probe — an HTTP endpoint your app serves — rather than relying on VM-level signals alone. Both settings are configured in the Azure portal under your scale set’s Health and Automatic repairs sections, or declaratively in an ARM or Bicep template for your app’s health endpoint.

For upgrades, choose the rolling upgrade policy for production scale sets: it updates instances in batches, respecting a healthy-instance minimum, instead of replacing everything at once. Manual upgrades are fine for development, but an automatic or rolling policy is what keeps a deployment boring during image updates.

Scale-in safety matters as much as scale-out. By default, the oldest instances are removed first, which is usually fine for stateless web servers. For stateful workloads — or any workload where draining matters — enable instance protection on specific instances during maintenance, and make sure your application handles termination gracefully: stop accepting new requests, finish in-flight work, then shut down. A scale set that kills instances mid-request creates errors that look like application bugs.

Managing Instances Day to Day

Day-to-day operations use the same CLI. Scale manually when you need to override autoscale temporarily:

az vmss scale --resource-group web-rg --name web-vmss --new-capacity 4

Reimage an instance that has drifted from the model image, restart one that is misbehaving, and delete a specific instance when needed:

az vmss reimage --resource-group web-rg --name web-vmss --instance-id 0
az vmss restart --resource-group web-rg --name web-vmss --instance-ids 1
az vmss delete-instances --resource-group web-rg --name web-vmss --instance-ids 2

For debugging, SSH to an individual instance through the load balancer’s NAT rules or via Azure Bastion, rather than exposing SSH publicly on every instance. Bastion gives you browser-based SSH without a public IP on the VMs, which is the safer pattern for production scale sets.

Troubleshooting Azure VM Scale Sets

Instances fail to provision. Check quota for the VM size in the region (az vm list-usage --location eastus) and the image name. Quota exhaustion is the most common cause of scale-out failures, and it only shows up when you actually need the capacity — request a quota increase before a traffic event, not during one.

Autoscale never triggers. Verify the autoscale settings exist (az monitor autoscale show), that the metric name matches, and that the instances are actually reporting CPU data. A scale set with no metrics cannot scale on metrics.

New instances are unhealthy. The application is not starting, or the health probe points at the wrong endpoint. SSH into one instance (via Bastion) and check the application logs before blaming the scale set.

Scale-in removes the wrong instance. Review the scale-in policy (default: oldest first) and use instance protection for instances that must survive a scale-in event, such as one holding a long-running job.

Conclusion

Azure VM scale sets turn capacity management from a manual chore into a policy: define the image and size once, set a floor and a ceiling, and let autoscale handle the rest — with health-based repairs and rolling upgrades keeping the fleet healthy as it changes size. Start with CPU-based rules, add scheduled profiles for predictable patterns, and protect stateful instances during scale-in. The full reference is in the Azure Virtual Machine Scale Sets documentation. With those pieces in place, your next traffic spike becomes a line on a chart, not an incident.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.