|
| 1 | +--- |
| 2 | +title: Tutorial - Autoscale a scale set with the Azure CLI |
| 3 | +description: Learn how to use the Azure CLI to automatically scale a Virtual Machine Scale Set as CPU demands increases and decreases |
| 4 | +author: ju-shim |
| 5 | +ms.author: jushiman |
| 6 | +ms.topic: tutorial |
| 7 | +ms.service: azure-virtual-machine-scale-sets |
| 8 | +ms.subservice: autoscale |
| 9 | +ms.date: 06/14/2024 |
| 10 | +ms.reviewer: mimckitt |
| 11 | +ms.custom: avverma, devx-track-azurecli, linux-related-content, innovation-engine |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Automatically scale a Virtual Machine Scale Set with the Azure CLI |
| 15 | + |
| 16 | +When you create a scale set, you define the number of VM instances that you wish to run. As your application demand changes, you can automatically increase or decrease the number of VM instances. The ability to autoscale lets you keep up with customer demand or respond to application performance changes throughout the lifecycle of your app. In this tutorial you learn how to: |
| 17 | + |
| 18 | +> [!div class="checklist"] |
| 19 | +> * Use autoscale with a scale set |
| 20 | +> * Create and use autoscale rules |
| 21 | +> * Simulate CPU load to trigger autoscale rules |
| 22 | +> * Monitor autoscale actions as demand changes |
| 23 | +
|
| 24 | +[!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)] |
| 25 | + |
| 26 | +[!INCLUDE [azure-cli-prepare-your-environment.md](~/reusable-content/azure-cli/azure-cli-prepare-your-environment.md)] |
| 27 | + |
| 28 | +- This tutorial requires version 2.0.32 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed. |
| 29 | + |
| 30 | +## Create a scale set |
| 31 | +Create a resource group with [az group create](/cli/azure/group). |
| 32 | + |
| 33 | +```azurecli-interactive |
| 34 | +export RANDOM_SUFFIX=$(openssl rand -hex 3) |
| 35 | +export REGION="WestUS2" |
| 36 | +export MY_RESOURCE_GROUP_NAME="myResourceGroup$RANDOM_SUFFIX" |
| 37 | +az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION |
| 38 | +``` |
| 39 | + |
| 40 | +Now create a Virtual Machine Scale Set with [az vmss create](/cli/azure/vmss). The following example creates a scale set with an instance count of 2, generates SSH keys if they don't exist, and uses a valid image "Ubuntu2204". |
| 41 | + |
| 42 | +```azurecli-interactive |
| 43 | +export MY_SCALE_SET_NAME="myScaleSet$RANDOM_SUFFIX" |
| 44 | +az vmss create \ |
| 45 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 46 | + --name $MY_SCALE_SET_NAME \ |
| 47 | + --image Ubuntu2204 \ |
| 48 | + --orchestration-mode Flexible \ |
| 49 | + --instance-count 2 \ |
| 50 | + --admin-username azureuser \ |
| 51 | + --generate-ssh-keys |
| 52 | +``` |
| 53 | + |
| 54 | +## Define an autoscale profile |
| 55 | +To enable autoscale on a scale set, you first define an autoscale profile. This profile defines the default, minimum, and maximum scale set capacity. These limits let you control cost by not continually creating VM instances, and balance acceptable performance with a minimum number of instances that remain in a scale-in event. Create an autoscale profile with [az monitor autoscale create](/cli/azure/monitor/autoscale#az-monitor-autoscale-create). The following example sets the default and minimum capacity of 2 VM instances, and a maximum of 10: |
| 56 | + |
| 57 | +```azurecli-interactive |
| 58 | +az monitor autoscale create \ |
| 59 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 60 | + --resource $MY_SCALE_SET_NAME \ |
| 61 | + --resource-type Microsoft.Compute/virtualMachineScaleSets \ |
| 62 | + --name autoscale \ |
| 63 | + --min-count 2 \ |
| 64 | + --max-count 10 \ |
| 65 | + --count 2 |
| 66 | +``` |
| 67 | + |
| 68 | +## Create a rule to autoscale out |
| 69 | +If your application demand increases, the load on the VM instances in your scale set increases. If this increased load is consistent, rather than just a brief demand, you can configure autoscale rules to increase the number of VM instances. When these instances are created and your application is deployed, the scale set starts to distribute traffic to them through the load balancer. You control which metrics to monitor, how long the load must meet a given threshold, and how many VM instances to add. |
| 70 | + |
| 71 | +Create a rule with [az monitor autoscale rule create](/cli/azure/monitor/autoscale/rule#az-monitor-autoscale-rule-create) that increases the number of VM instances when the average CPU load is greater than 70% over a 5-minute period. When the rule triggers, the number of VM instances is increased by three. |
| 72 | + |
| 73 | +```azurecli-interactive |
| 74 | +az monitor autoscale rule create \ |
| 75 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 76 | + --autoscale-name autoscale \ |
| 77 | + --condition "Percentage CPU > 70 avg 5m" \ |
| 78 | + --scale out 3 |
| 79 | +``` |
| 80 | + |
| 81 | +## Create a rule to autoscale in |
| 82 | +When application demand decreases, the load on the VM instances drops. If this decreased load persists over a period of time, you can configure autoscale rules to decrease the number of VM instances in the scale set. This scale-in action helps reduce costs by running only the necessary number of instances required to meet current demand. |
| 83 | + |
| 84 | +Create another rule with [az monitor autoscale rule create](/cli/azure/monitor/autoscale/rule#az-monitor-autoscale-rule-create) that decreases the number of VM instances when the average CPU load drops below 30% over a 5-minute period. The following example scales in the number of VM instances by one. |
| 85 | + |
| 86 | +```azurecli-interactive |
| 87 | +az monitor autoscale rule create \ |
| 88 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 89 | + --autoscale-name autoscale \ |
| 90 | + --condition "Percentage CPU < 30 avg 5m" \ |
| 91 | + --scale in 1 |
| 92 | +``` |
| 93 | + |
| 94 | +## Simulate CPU load on scale set |
| 95 | +To test the autoscale rules, you need to simulate sustained CPU load on the VM instances in the scale set. In this minimalist approach, we avoid installing additional packages by using the built-in "yes" command to generate CPU load. The following command starts 3 background processes that continuously output data to /dev/null for 60 seconds and then terminates them. |
| 96 | + |
| 97 | +```bash |
| 98 | +for i in {1..3}; do |
| 99 | + yes > /dev/null & |
| 100 | +done |
| 101 | +sleep 60 |
| 102 | +pkill yes |
| 103 | +``` |
| 104 | + |
| 105 | +This command simulates CPU load without introducing package installation errors. |
| 106 | + |
| 107 | +## Monitor the active autoscale rules |
| 108 | +To monitor the number of VM instances in your scale set, use the watch command. It may take up to 5 minutes for the autoscale rules to begin the scale-out process in response to the CPU load. However, once it happens, you can exit watch with Ctrl-c. |
| 109 | + |
| 110 | +By then, the scale set will automatically increase the number of VM instances to meet the demand. The following command shows the list of VM instances in the scale set: |
| 111 | + |
| 112 | +```azurecli-interactive |
| 113 | +az vmss list-instances \ |
| 114 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 115 | + --name $MY_SCALE_SET_NAME \ |
| 116 | + --output table |
| 117 | +``` |
| 118 | + |
| 119 | +Once the CPU threshold has been met, the autoscale rules increase the number of VM instances in the scale set. The output will show the list of VM instances as new ones are created. |
| 120 | + |
| 121 | +```output |
| 122 | + InstanceId LatestModelApplied Location Name ProvisioningState ResourceGroup VmId |
| 123 | +------------ -------------------- ---------- --------------- ------------------- -------------------- ------------------------------------ |
| 124 | + 1 True WestUS2 myScaleSet_1 Succeeded myResourceGroupxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 125 | + 2 True WestUS2 myScaleSet_2 Succeeded myResourceGroupxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 126 | + 4 True WestUS2 myScaleSet_4 Creating myResourceGroupxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 127 | + 5 True WestUS2 myScaleSet_5 Creating myResourceGroupxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 128 | + 6 True WestUS2 myScaleSet_6 Creating myResourceGroupxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 129 | +``` |
| 130 | + |
| 131 | +Once the CPU load subsides, the average CPU load returns to normal. After another 5 minutes, the autoscale rules then scale in the number of VM instances. Scale-in actions remove VM instances with the highest IDs first. When a scale set uses Availability Sets or Availability Zones, scale-in actions are evenly distributed across the VM instances. The following sample output shows one VM instance being deleted as the scale set autoscales in: |
| 132 | + |
| 133 | +```output |
| 134 | +6 True WestUS2 myScaleSet_6 Deleting myResourceGroupxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 135 | +``` |
| 136 | + |
| 137 | +## Clean up resources |
| 138 | +To remove your scale set and associated resources, please manually delete the resource group using your preferred method. |
| 139 | + |
| 140 | +## Next steps |
| 141 | +In this tutorial, you learned how to automatically scale in or out a scale set with the Azure CLI: |
| 142 | + |
| 143 | +> [!div class="checklist"] |
| 144 | +> * Use autoscale with a scale set |
| 145 | +> * Create and use autoscale rules |
| 146 | +> * Simulate CPU load to trigger autoscale rules |
| 147 | +> * Monitor autoscale actions as demand changes |
0 commit comments