|
| 1 | +--- |
| 2 | +title: Create a Virtual Machine Scale Sets for Windows in Azure | Microsoft Docs |
| 3 | +description: Create and deploy a highly available application on Windows VMs using a virtual machine scale set |
| 4 | +services: virtual-machine-scale-sets |
| 5 | +documentationcenter: '' |
| 6 | +author: iainfoulds |
| 7 | +manager: timlt |
| 8 | +editor: '' |
| 9 | +tags: '' |
| 10 | + |
| 11 | +ms.assetid: '' |
| 12 | +ms.service: virtual-machine-scale-sets |
| 13 | +ms.workload: infrastructure-services |
| 14 | +ms.tgt_pltfrm: na |
| 15 | +ms.devlang: |
| 16 | +ms.topic: article |
| 17 | +ms.date: 05/01/2017 |
| 18 | +ms.author: iainfou |
| 19 | +--- |
| 20 | + |
| 21 | +# Create a Virtual Machine Scale Set and deploy a highly available app on Windows |
| 22 | +In this tutorial, you learn how virtual machine scale sets in Azure allow you to quickly scale the number of virtual machines (VMs) running your app. A virtual machine scale set allows you to deploy and manage a set of identical, auto-scaling virtual machines. You can scale the number of VMs in the scale set manually, or define rules to autoscale based on CPU usage, memory demand, or network traffic. To see a virtual machine scale set in action, you build a basic IIS website that runs across multiple Windows VMs. |
| 23 | + |
| 24 | +The steps in this tutorial can be completed using the latest [Azure PowerShell](/powershell/azureps-cmdlets-docs/) module. |
| 25 | + |
| 26 | + |
| 27 | +## Scale Set overview |
| 28 | +Scale sets use similar concepts as you learned about in the previous tutorial to [Create highly available VMs](tutorial-availability-sets.md). VMs in a scale set are distributed across fault and update domains just like VMs in an availability set. |
| 29 | + |
| 30 | +VMs are created as needed in a scale set. You define autoscale rules to control how and when VMs are added or removed from the scale set. These rules can trigger based on metrics such as CPU load, memory usage, or network traffic. |
| 31 | + |
| 32 | +Scale sets support up to 1,000 VMs when you use an Azure platform image. For workloads with significant installation or VM customization requirements, you may wish to [Create a custom VM image](tutorial-custom-images.md). You can create up to 100 VMs in a scale set when using a custom image. |
| 33 | + |
| 34 | + |
| 35 | +## Create an app to scale |
| 36 | +Before you can create a scale set, create a resource group with [New-AzureRmResourceGroup](/powershell/module/azurerm.resources/new-azurermresourcegroup). The following example creates a resource group named `myResourceGroupAutomate` in the `westus` location: |
| 37 | + |
| 38 | +```powershell |
| 39 | +New-AzureRmResourceGroup -ResourceGroupName myResourceGroupScaleSet -Location westus |
| 40 | +``` |
| 41 | + |
| 42 | +In an earlier tutorial, you learned how to [Automate VM configuration](tutorial-automate-vm-deployment.md) using the Custom Script Extension. Create a scale set configuration then apply a Custom Script Extension to install and configure IIS: |
| 43 | + |
| 44 | +```powershell |
| 45 | +# Create a config object |
| 46 | +$vmssConfig = New-AzureRmVmssConfig -Location WestUS -SkuCapacity 2 -SkuName Standard_DS2 -UpgradePolicyMode Automatic |
| 47 | +
|
| 48 | +# Define the script for your Custom Script Extension to run |
| 49 | +$publicSettings = @{ |
| 50 | + "fileUris" = (,"https://raw.githubusercontent.com/iainfoulds/azure-samples/master/automate-iis.ps1"); |
| 51 | + "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1" |
| 52 | +} |
| 53 | +
|
| 54 | +# Use Custom Script Extension to install IIS and configure basic website |
| 55 | +Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmssConfig ` |
| 56 | + -Name "customScript" ` |
| 57 | + -Publisher "Microsoft.Compute" ` |
| 58 | + -Type "CustomScriptExtension" ` |
| 59 | + -TypeHandlerVersion 1.8 ` |
| 60 | + -Setting $publicSettings |
| 61 | +``` |
| 62 | + |
| 63 | +## Create scale load balancer |
| 64 | +An Azure load balancer is a Layer-4 (TCP, UDP) load balancer that provides high availability by distributing incoming traffic among healthy VMs. A load balancer health probe monitors a given port on each VM and only distributes traffic to an operational VM. For more information, see the next tutorial on [How to load balance Windows virtual machines](tutorial-load-balancer.md). |
| 65 | + |
| 66 | +Create a load balancer that has a public IP address and distributes web traffic on port 80: |
| 67 | + |
| 68 | +```powershell |
| 69 | +# Create a public IP address |
| 70 | +$publicIP = New-AzureRmPublicIpAddress ` |
| 71 | + -ResourceGroupName myResourceGroupScaleSet ` |
| 72 | + -Location westus ` |
| 73 | + -AllocationMethod Static ` |
| 74 | + -Name myPublicIP |
| 75 | +
|
| 76 | +# Create a frontend and backend IP pool |
| 77 | +$frontendIP = New-AzureRmLoadBalancerFrontendIpConfig ` |
| 78 | + -Name myFrontEndPool ` |
| 79 | + -PublicIpAddress $publicIP |
| 80 | +$backendPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name myBackEndPool |
| 81 | +
|
| 82 | +# Create the load balancer |
| 83 | +$lb = New-AzureRmLoadBalancer ` |
| 84 | + -ResourceGroupName myResourceGroupScaleSet ` |
| 85 | + -Name myLoadBalancer ` |
| 86 | + -Location westus ` |
| 87 | + -FrontendIpConfiguration $frontendIP ` |
| 88 | + -BackendAddressPool $backendPool |
| 89 | +
|
| 90 | +# Create a load balancer health probe on port 80 |
| 91 | +Add-AzureRmLoadBalancerProbeConfig -Name myHealthProbe ` |
| 92 | + -LoadBalancer $lb ` |
| 93 | + -Protocol tcp ` |
| 94 | + -Port 80 ` |
| 95 | + -IntervalInSeconds 15 ` |
| 96 | + -ProbeCount 2 |
| 97 | +
|
| 98 | +# Create a load balancer rule to distribute traffic on port 80 |
| 99 | +Add-AzureRmLoadBalancerRuleConfig ` |
| 100 | + -Name myLoadBalancerRule ` |
| 101 | + -LoadBalancer $lb ` |
| 102 | + -FrontendIpConfiguration $lb.FrontendIpConfigurations[0] ` |
| 103 | + -BackendAddressPool $lb.BackendAddressPools[0] ` |
| 104 | + -Protocol Tcp ` |
| 105 | + -FrontendPort 80 ` |
| 106 | + -BackendPort 80 |
| 107 | +
|
| 108 | +# Update the load balancer configuration |
| 109 | +Set-AzureRmLoadBalancer -LoadBalancer $lb |
| 110 | +``` |
| 111 | + |
| 112 | +## Create a scale set |
| 113 | +Now create a virtual machine scale set with [New-AzureRmVmss](/powershell/module/azurerm.compute/new-azurermvm). The following example creates a scale set named `myScaleSet`: |
| 114 | + |
| 115 | +```powershell |
| 116 | +# Reference a virtual machine image from the gallery |
| 117 | +Set-AzureRmVmssStorageProfile $vmssConfig ` |
| 118 | + -ImageReferencePublisher MicrosoftWindowsServer ` |
| 119 | + -ImageReferenceOffer WindowsServer ` |
| 120 | + -ImageReferenceSku 2016-Datacenter ` |
| 121 | + -ImageReferenceVersion latest |
| 122 | +
|
| 123 | +# Set up information for authenticating with the virtual machine |
| 124 | +Set-AzureRmVmssOsProfile $vmssConfig ` |
| 125 | + -AdminUsername azureuser ` |
| 126 | + -AdminPassword P@ssword! ` |
| 127 | + -ComputerNamePrefix myVM |
| 128 | +
|
| 129 | +# Create the virtual network resources |
| 130 | +$subnet = New-AzureRmVirtualNetworkSubnetConfig ` |
| 131 | + -Name "mySubnet" ` |
| 132 | + -AddressPrefix 10.0.0.0/24 |
| 133 | +$vnet = New-AzureRmVirtualNetwork ` |
| 134 | + -ResourceGroupName "myResourceGroupScaleSet" ` |
| 135 | + -Name "myVnet" ` |
| 136 | + -Location "westus" ` |
| 137 | + -AddressPrefix 10.0.0.0/16 ` |
| 138 | + -Subnet $subnet |
| 139 | +$ipConfig = New-AzureRmVmssIpConfig ` |
| 140 | + -Name "myIPConfig" ` |
| 141 | + -LoadBalancerBackendAddressPoolsId $lb.BackendAddressPools[0].Id ` |
| 142 | + -SubnetId $vnet.Subnets[0].Id |
| 143 | +
|
| 144 | +# Attach the virtual network to the config object |
| 145 | +Add-AzureRmVmssNetworkInterfaceConfiguration ` |
| 146 | + -VirtualMachineScaleSet $vmssConfig ` |
| 147 | + -Name "network-config" ` |
| 148 | + -Primary $true ` |
| 149 | + -IPConfiguration $ipConfig |
| 150 | +
|
| 151 | +# Create the scale set with the config object (this step might take a few minutes) |
| 152 | +New-AzureRmVmss ` |
| 153 | + -ResourceGroupName myResourceGroupScaleSet ` |
| 154 | + -Name myScaleSet ` |
| 155 | + -VirtualMachineScaleSet $vmssConfig |
| 156 | +``` |
| 157 | + |
| 158 | +It takes a few minutes to create and configure all the scale set resources and VMs. |
| 159 | + |
| 160 | + |
| 161 | +## Test your app |
| 162 | +To see your IIS website in action, obtain the public IP address of your load balancer with [Get-AzureRmPublicIPAddress](/powershell/module/azurerm.network/get-azurermpublicipaddress). The following example obtains the IP address for `myPublicIP` created as part of the scale set: |
| 163 | + |
| 164 | +```powershell |
| 165 | +Get-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupScaleSet -Name myPublicIP | select IpAddress |
| 166 | +``` |
| 167 | + |
| 168 | +Enter the public IP address in to a web browser. The app is displayed, including the hostname of the VM that the load balancer distributed traffic to: |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +To see the scale set in action, you can force-refresh your web browser to see the load balancer distribute traffic across all the VMs running your app. |
| 173 | + |
| 174 | + |
| 175 | +## Management tasks |
| 176 | +Throughout the lifecycle of the scale set, you may need to run one or more management tasks. Additionally, you may want to create scripts that automate various lifecycle-tasks. Azure PowerShell provides a quick way to do those tasks. Here are a few common tasks. |
| 177 | + |
| 178 | +### View VMs in a scale set |
| 179 | +To view a list of VMs running in your scale set, use [Get-AzureRmVmssVM](/powershell/module/azurerm.compute/get-azurermvmssvm) as follows: |
| 180 | + |
| 181 | +```powershell |
| 182 | +# Get current scale set |
| 183 | +$scaleset = Get-AzureRmVmss ` |
| 184 | + -ResourceGroupName myResourceGroupScaleSet ` |
| 185 | + -VMScaleSetName myScaleSet |
| 186 | +
|
| 187 | +# Loop through the instanaces in your scale set |
| 188 | +for ($i=0; $i -le ($set.Sku.Capacity - 1); $i++) { |
| 189 | + Get-AzureRmVmssVM -ResourceGroupName myResourceGroupScaleSet ` |
| 190 | + -VMScaleSetName myScaleSet ` |
| 191 | + -InstanceId $i |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | + |
| 196 | +### Increase or decrease VM instances |
| 197 | +To see the number of instances you currently have in a scale set, use [Get-AzureRmVmss](/powershell/module/azurerm.compute/get-azurermvmss) and query on `sku.capacity`: |
| 198 | + |
| 199 | +```powershell |
| 200 | +Get-AzureRmVmss -ResourceGroupName myResourceGroupScaleSet ` |
| 201 | + -VMScaleSetName myScaleSet | ` |
| 202 | + Select -ExpandProperty Sku |
| 203 | +``` |
| 204 | + |
| 205 | +You can then manually increase or decrease the number of virtual machines in the scale set with [Update-AzureRmVmss](/powershell/module/azurerm.compute/update-azurermvmss). The following example sets the number of VMs in your scale set to `5`: |
| 206 | + |
| 207 | +```powershell |
| 208 | +# Get current scale set |
| 209 | +$scaleset = Get-AzureRmVmss ` |
| 210 | + -ResourceGroupName myResourceGroupScaleSet ` |
| 211 | + -VMScaleSetName myScaleSet |
| 212 | +
|
| 213 | +# Set and update the capacity of your scale set |
| 214 | +$scaleset.sku.capacity = 5 |
| 215 | +Update-AzureRmVmss -ResourceGroupName myResourceGroupScaleSet ` |
| 216 | + -Name myScaleSet ` |
| 217 | + -VirtualMachineScaleSet $scaleset |
| 218 | +``` |
| 219 | + |
| 220 | +If takes a few minutes to update the specified number of instances in your scale set. |
| 221 | + |
| 222 | + |
| 223 | +### Configure autoscale rules |
| 224 | +Rather than manually scaling the number of instances in your scale set, you define autoscale rules. These rules monitor the instances in your scale set and respond accordingly based on metrics and thresholds you define. The following example scales out the number of instances by one when the average CPU load is greater than 60% over a 5 minute period. If the average CPU load then drops below 30% over a 5 minute period, the instances are scaled in by one instance: |
| 225 | + |
| 226 | +```powershell |
| 227 | +# Define your scale set information |
| 228 | +$mySubscriptionId = (Get-AzureRmSubscription).SubscriptionId |
| 229 | +$myResourceGroup = "myResourceGroupScaleSet" |
| 230 | +$myScaleSet = "myScaleSet" |
| 231 | +$myLocation = "West US" |
| 232 | +
|
| 233 | +# Create a scale up rule to increase the number instances after 60% average CPU usage exceeded for a 5 minute period |
| 234 | +$myRuleScaleUp = New-AzureRmAutoscaleRule ` |
| 235 | + -MetricName "Percentage CPU" ` |
| 236 | + -MetricResourceId /subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet ` |
| 237 | + -Operator GreaterThan ` |
| 238 | + -MetricStatistic Average ` |
| 239 | + -Threshold 60 ` |
| 240 | + -TimeGrain 00:01:00 ` |
| 241 | + -TimeWindow 00:05:00 ` |
| 242 | + -ScaleActionCooldown 00:05:00 ` |
| 243 | + -ScaleActionDirection Increase ` |
| 244 | + -ScaleActionValue 1 |
| 245 | +
|
| 246 | +# Create a scale down rule to decrease the number of instances after 30% average CPU usage over a 5 minute period |
| 247 | +$myRuleScaleDown = New-AzureRmAutoscaleRule ` |
| 248 | + -MetricName "Percentage CPU" ` |
| 249 | + -MetricResourceId /subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet ` |
| 250 | + -Operator LessThan ` |
| 251 | + -MetricStatistic Average ` |
| 252 | + -Threshold 30 ` |
| 253 | + -TimeGrain 00:01:00 ` |
| 254 | + -TimeWindow 00:05:00 ` |
| 255 | + -ScaleActionCooldown 00:05:00 ` |
| 256 | + -ScaleActionDirection Decrease ` |
| 257 | + -ScaleActionValue 1 |
| 258 | +
|
| 259 | +# Create a scale profile with your scale up and scale down rules |
| 260 | +$myScaleProfile = New-AzureRmAutoscaleProfile ` |
| 261 | + -DefaultCapacity 2 ` |
| 262 | + -MaximumCapacity 10 ` |
| 263 | + -MinimumCapacity 2 ` |
| 264 | + -Rules $myRuleScaleUp,$myRuleScaleDown ` |
| 265 | + -Name "autoprofile" |
| 266 | +
|
| 267 | +# Apply the autoscale rules |
| 268 | +Add-AzureRmAutoscaleSetting ` |
| 269 | + -Location $myLocation ` |
| 270 | + -Name "autosetting" ` |
| 271 | + -ResourceGroup $myResourceGroup ` |
| 272 | + -TargetResourceId /subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet ` |
| 273 | + -AutoscaleProfiles $myScaleProfile |
| 274 | +``` |
| 275 | + |
| 276 | + |
| 277 | +## Next steps |
| 278 | +In this tutorial, you learned how to create a virtual machine scale set. Advance to the next tutorial to learn more about load balancing concepts for virtual machines. |
| 279 | + |
| 280 | +[Load balance virtual machines](tutorial-load-balancer.md) |
0 commit comments