Overview and What You Will Learn
A Virtual Machine Scale Set (VMSS) runs a group of identical VMs as one unit, and an autoscale setting adds or removes instances automatically based on a metric such as CPU. In this lab you will create a scale set, add scale-out and scale-in rules, load the instances with a CPU stress test, and watch the instance count rise and then fall back.
- Creating a scale set with a minimum, maximum and default instance count
- Writing scale-out and scale-in rules with different thresholds
- Generating CPU load on every instance
- Watching capacity change as the average CPU moves
- Choosing cooldowns and limits that prevent flapping
Why This Matters in Production
Autoscaling lets capacity follow demand, so you neither run out of servers in a spike nor pay for peak capacity all year. Imagine a food-delivery team at a company like Swiggy facing a sudden ten-fold traffic jump during a flash sale. Nobody should be launching VMs by hand at 2 AM, and nobody wants that peak capacity idle for the other 364 days. A properly configured scale set handles both sides automatically.
Core Principles
An autoscale setting watches a metric and changes the scale set's instance count within a minimum and a maximum that you define. It never goes below the minimum or above the maximum, however extreme the metric becomes, and it falls back to a default count if it cannot read the metric.
across instances]):::src --> Q1{Above 70%
for 5 minutes?}:::q Q1 -->|yes| OUT[Scale out +1
up to max]:::ok Q1 -->|no| Q2{Below 30%
for 5 minutes?}:::q Q2 -->|yes| IN[Scale in -1
down to min]:::warn Q2 -->|no| STAY[No change]:::soft classDef src fill:#DBEAFE,color:#1E293B classDef q fill:#FFEDD5,color:#1E293B classDef ok fill:#10B981,color:#FFFFFF classDef warn fill:#F97316,color:#FFFFFF classDef soft fill:#F1F5F9,color:#1E293B
The rule works on the average across all instances, so adding an instance lowers the average even if the load stays the same. This is intentional, and it is why a scale set settles instead of growing without end.
RememberScale-out and scale-in rules are separate, and they should use thresholds with a wide gap, such as 70% out and 30% in. Autoscale also estimates what the metric would be after a scale-in and skips it if that would immediately trigger a scale-out again.
Detailed Step-by-Step Practical Lab
- Create a resource group:
az group create --name rg-vmss-lab-central --location centralindia- Create a Uniform-mode scale set with 2 instances:
az vmss create \ --resource-group rg-vmss-lab-central \ --name vmss-web-lab \ --orchestration-mode Uniform \ --image Ubuntu2204 \ --instance-count 2 \ --admin-username azureadmin \ --generate-ssh-keys \ --vm-sku Standard_B1s- Confirm both instances are running:
az vmss list-instances \ --resource-group rg-vmss-lab-central \ --name vmss-web-lab \ --output table- Create the autoscale setting with a minimum of 2, a maximum of 6 and a default of 2 instances:
az monitor autoscale create \ --resource-group rg-vmss-lab-central \ --resource vmss-web-lab \ --resource-type Microsoft.Compute/virtualMachineScaleSets \ --name autoscale-web-lab \ --min-count 2 \ --max-count 6 \ --count 2- Add a scale-out rule that adds one instance when average CPU is above 70% for 5 minutes, with a 5-minute cooldown before the next scaling action:
az monitor autoscale rule create \ --resource-group rg-vmss-lab-central \ --autoscale-name autoscale-web-lab \ --condition "Percentage CPU > 70 avg 5m" \ --scale out 1 \ --cooldown 5- Add a scale-in rule that removes one instance when average CPU is below 30% for 5 minutes:
az monitor autoscale rule create \ --resource-group rg-vmss-lab-central \ --autoscale-name autoscale-web-lab \ --condition "Percentage CPU < 30 avg 5m" \ --scale in 1 \ --cooldown 5 az monitor autoscale rule list \ --resource-group rg-vmss-lab-central \ --autoscale-name autoscale-web-lab \ --output table- Start a 20-minute CPU stress test on every current instance. The
systemd-runcommand keeps it running after the command returns, and the test ends by itself so you can watch the scale-in:
for id in $(az vmss list-instances \ --resource-group rg-vmss-lab-central \ --name vmss-web-lab \ --query "[].instanceId" --output tsv); do az vmss run-command invoke \ --resource-group rg-vmss-lab-central \ --name vmss-web-lab \ --instance-id "$id" \ --command-id RunShellScript \ --scripts "sudo apt-get update && sudo apt-get install -y stress-ng && sudo systemd-run --unit=cpu-stress stress-ng --cpu 0 --timeout 1200s"done- Watch the instance count once a minute. Expect a scale-out after roughly 6 to 10 minutes of high CPU. New instances are not stressed, so the average falls as they join, and the set may settle at 3 instead of climbing to 6:
for i in $(seq 1 30); do echo "$(date +%H:%M) instances: $(az vmss show \ --resource-group rg-vmss-lab-central \ --name vmss-web-lab \ --query sku.capacity --output tsv)" sleep 60doneKeep watching after the stress test ends at about the 20-minute mark. CPU drops, and the scale-in rule removes the extra instance after its 5-minute window and cooldown, back to the minimum of 2.
Clean up:
az group delete --name rg-vmss-lab-central --yes --no-waitProduction Best Practices & Common Pitfalls
Common MistakeSetting scale-out and scale-in thresholds too close together, such as 60% out and 55% in. When load hovers in that range the scale set keeps adding and removing instances, wasting money and time on churn instead of settling.
TipUse a cooldown on every rule so the set waits after a scaling action before deciding again. New instances need time to boot and take traffic, and without a cooldown a brief spike can trigger a second, unnecessary scale-out.
RememberThe maximum count is your cost ceiling and also your capacity ceiling. A maximum set too low can silently cap your ability to handle a real spike, so base it on load testing and quota, not a round number.
- Avoid burstable VM sizes for real autoscaling. B-series VMs run on CPU credits, so their CPU percentage can look low or spike unpredictably. This lab uses them for cost, but production scale sets should use a non-burstable family.
- Pre-scale for known events. CPU is a lagging signal, so add a scheduled autoscale profile that raises the minimum before a flash sale instead of waiting for load to arrive.
- Scale on what users feel. For web tiers, request count per instance or queue length often predicts trouble sooner than CPU does.
- Roll out updates by image. Build changes into a new image version and use an automatic or rolling upgrade policy with health checks, rather than patching running instances by hand.
- Spread across zones. Placing the scale set in multiple availability zones keeps capacity through a single zone failure, and autoscale balances new instances across them.
- Check quota before the event. Autoscale cannot create instances beyond the subscription's vCPU quota for that VM family and region.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az vmss create |
Create a new Virtual Machine Scale Set |
az vmss list-instances |
List current instances in a scale set |
az vmss show --query sku.capacity |
Check the current instance count |
az monitor autoscale create |
Create an autoscale setting with limits |
az monitor autoscale rule create |
Add a scale-out or scale-in rule |
az monitor autoscale rule list |
Review the rules on an autoscale setting |