|
| 1 | +--- |
| 2 | +title: Configure autoscale with PowerShell |
| 3 | +description: Configure autoscale for a Virtal Machine Scale Set using PowerShell |
| 4 | +author: EdB-MSFT |
| 5 | +ms.author: edbaynash |
| 6 | +ms.topic: how-to |
| 7 | +ms.date: 01/05/2023 |
| 8 | +ms.subservice: autoscale |
| 9 | +ms.reviewer: akkumari |
| 10 | + |
| 11 | +# Customer intent: As a user or dev ops administrator, I want to use powershell to set up autoscale so I can scale my VMSS. |
| 12 | + |
| 13 | + |
| 14 | +1-sentence Intro to autoscale |
| 15 | +Benefits of using Poowershell to configurye autoscale |
| 16 | +Prereqs |
| 17 | + - give powershell to create VMSS ? ( how do they create load ?) |
| 18 | + - Windows or linux ? ( linux) |
| 19 | + - provide a vm image ? (git , galery ? which user owns it ?) |
| 20 | + - assume VMSS exists ? |
| 21 | + - create an image - too complicated |
| 22 | + |
| 23 | +Define the scenario |
| 24 | +scripts to create the objects. |
| 25 | +For each Object define what is required with example |
| 26 | + |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +# Configure autoscale with PowerShell |
| 31 | + |
| 32 | +Autoscale settings help ensure that you have the right amount of resources running to handle the fluctuating load of your application.Cyou can configure autoscale using the Azure portal, Azure CLI, PowerShell or ARM or Bicep templates. |
| 33 | + |
| 34 | +This article shows you haw to configure autoscale for a Virtual Machine Scale Set using Powershell |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +To configure autoscale using PowerShell, you need an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free). |
| 39 | + |
| 40 | +## Overview |
| 41 | + |
| 42 | ++ Start by creating a scale set that you can autoscle |
| 43 | ++ Create rules to scale in and scale out |
| 44 | ++ Create a profile that uses your rules |
| 45 | ++ Apply the autoscale settings |
| 46 | ++ Make some changes |
| 47 | ++ Update your autoscale settings |
| 48 | + |
| 49 | +## Create a Virtual Machine Scale Set |
| 50 | + |
| 51 | +Create a scale set using the following commands |
| 52 | + |
| 53 | +```azurepowershell |
| 54 | +
|
| 55 | +$vmPassword = ConvertTo-SecureString "ChangeThisPassword1" -AsPlainText -Force |
| 56 | +$vmCred = New-Object System.Management.Automation.PSCredential('azureuser', $vmPassword) |
| 57 | +
|
| 58 | +$resourceGroupName="rg-powershell-autoscale" |
| 59 | +$VMSSName="vmss-001" |
| 60 | +
|
| 61 | +# create a new resource group |
| 62 | +
|
| 63 | +New-AzResourceGroup -ResourceGroupName $resourceGroupName -Location "EastUS" |
| 64 | +
|
| 65 | +New-AzVmss ` |
| 66 | + -ResourceGroupName $resourceGroupName ` |
| 67 | + -Location "EastUS" ` |
| 68 | + -VMScaleSetName $VMSSName ` |
| 69 | + -VirtualNetworkName "myVnet" ` |
| 70 | + -SubnetName "mySubnet" ` |
| 71 | + -PublicIpAddressName "myPublicIPAddress" ` |
| 72 | + -LoadBalancerName "myLoadBalancer" ` |
| 73 | + -UpgradePolicyMode "Automatic" |
| 74 | +
|
| 75 | +
|
| 76 | +New-AzVmss ` |
| 77 | + -ResourceGroupName $resourceGroupName ` |
| 78 | + -Location "EastUS" ` |
| 79 | + -VMScaleSetName $VMSSName ` |
| 80 | + -Credential $vmCred ` |
| 81 | + -VirtualNetworkName "myVnet" ` |
| 82 | + -SubnetName "mySubnet" ` |
| 83 | + -PublicIpAddressName "myPublicIPAddress" ` |
| 84 | + -LoadBalancerName "myLoadBalancer" ` |
| 85 | + -UpgradePolicyMode "Automatic" ` |
| 86 | +``` |
| 87 | + |
| 88 | +## Create autoscale settings. |
| 89 | + |
| 90 | +To create autoscale setting using Powershell, follow the sequence below: |
| 91 | +1. Create rules using `New-AzAutoscaleScaleRuleObject` |
| 92 | +1. Create a profile using `New-AzAutoscaleProfileObject` |
| 93 | +1. Create or update the autoscale settings using `Update-AzAutoscaleSetting` |
| 94 | + |
| 95 | +### Create rules |
| 96 | + |
| 97 | +Create scale in and scale out rules then associated them with a profile. |
| 98 | +Rules are created using the [`New-AzAutoscaleScaleRuleObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalescaleruleobject). |
| 99 | + |
| 100 | +The wolloing Powershell script created two rules. |
| 101 | + |
| 102 | ++ Scale out when Percentage CPU exceeds 70% |
| 103 | ++ Scale in when Percentage CPU is less than 30% |
| 104 | + |
| 105 | +```azurepowershell |
| 106 | +
|
| 107 | +$resourceGroupName="rg-powershell-autoscale" |
| 108 | +$VMSSName="vmss-001" |
| 109 | +
|
| 110 | +$rule1=New-AzAutoscaleScaleRuleObject ` |
| 111 | + -MetricTriggerMetricName "Percentage CPU" ` |
| 112 | + -MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$VMSSName" ` |
| 113 | + -MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) ` |
| 114 | + -MetricTriggerStatistic "Average" ` |
| 115 | + -MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) ` |
| 116 | + -MetricTriggerTimeAggregation "Average" ` |
| 117 | + -MetricTriggerOperator "GreaterThan" ` |
| 118 | + -MetricTriggerThreshold 70 ` |
| 119 | + -MetricTriggerDividePerInstance $false ` |
| 120 | + -ScaleActionDirection "Increase" |
| 121 | + -ScaleActionType "ChangeCount" ` |
| 122 | + -ScaleActionValue 1 ` |
| 123 | + -ScaleActionCooldown ([System.TimeSpan]::New(0,5,0)) |
| 124 | +
|
| 125 | +
|
| 126 | +$rule2=New-AzAutoscaleScaleRuleObject ` |
| 127 | +-MetricTriggerMetricName "Percentage CPU" ` |
| 128 | +-MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$VMSSName" ` |
| 129 | +-MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) ` |
| 130 | +-MetricTriggerStatistic "Average" ` |
| 131 | +-MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) ` |
| 132 | +-MetricTriggerTimeAggregation "Average" ` |
| 133 | +-MetricTriggerOperator "LessThan" ` |
| 134 | +-MetricTriggerThreshold 30 ` |
| 135 | +-MetricTriggerDividePerInstance $false ` |
| 136 | +-ScaleActionDirection "Decrease" ` |
| 137 | +-ScaleActionType "ChangeCount" ` |
| 138 | +-ScaleActionValue 1 ` |
| 139 | +-ScaleActionCooldown ([System.TimeSpan]::New(0,5,0)) |
| 140 | +``` |
| 141 | + |
| 142 | +### Common parameters |
| 143 | + |
| 144 | +Set the autoscale trigger metric using `MetricTriggerMetricName` and `MetricTriggerMetricResourceUri`. The `MetricTriggerMetricResourceUri` can be any resource and not just the resource that is being scaled. For example, you can scale your VMSS based on metrics created by a load balancer, database, or the VMSS itself. The `MetricTriggerMetricName` must exist for the specified `MetricTriggerMetricResourceUri`. |
| 145 | + |
| 146 | +`MetricTriggerTimeGrain` is the sampling frequency of the metric that the rule monitors. `MetricTriggerTimeGrain` must be one of the predefined values for the specified metric and must be between 12 hours and 1 minute. For example, `MetricTriggerTimeGrain` = *PT1M*"* means that the metrics are sampled every 1 minute and aggregated using the aggregation method specified in `MetricTriggerStatistic`. |
| 147 | + |
| 148 | +`MetricTriggerTimeAggregation` is the aggregation method within the timeGrain period. For example, statistic = "Average" and timeGrain = "PT1M" means that the metrics will be aggregated every 1 minute by taking the average. |
| 149 | + |
| 150 | +`MetricTriggerStatistic` is the aggregation method used to aggregate the sampled metrics. For example, TimeAggregation = "Average" will aggregate the sampled metrics by taking the average. |
| 151 | + |
| 152 | +`MetricTriggerTimeWindow` is amount of time that the autoscale engine looks back to aggregate the metric. This value must be greater than the delay in metric collection, which varies by resource. It must be between 5 minutes and 12 hours. For example, 10 minutes means that every time autoscale runs, it queries metrics for the past 10 minutes. This allows your metrics to stabilize and avoids reacting to transient spikes. |
| 153 | + |
| 154 | +`MetricTriggerThreshold` defines the value of the metric that triggers a scale event. |
| 155 | + |
| 156 | +`MetricTriggerOperator` specifies the logical comparative operating to use when evaluating the metric value. |
| 157 | + |
| 158 | +`MetricTriggerDividePerInstance`, when st to `true` divides the trigger metric by the total number of instances. Gor example, If message count is 300 and there are 5 instances running, the calculated metric value is 60 messages per instance. This property isn't applicable for all metrics. |
| 159 | + |
| 160 | +Specify scaling in or out using `ScaleActionDirection`. Valid values are *Increase* and *Decrease* |
| 161 | + |
| 162 | +Use `ScaleActionType` to scale by a number of instance, to a specific instance count, or by percentage of the current instance count. Valid value include `ChangeCount`, `ExactCount`, and `PercentChangeCount`. |
| 163 | + |
| 164 | +`ScaleActionValue` spefices the value of the `ScaleActionType` to scale the reource by, for example, When `ScaleActionType` is *PercentChangeCount* setting `ScaleActionValue` to *50* scales the resource by 50% of the current instance count. |
| 165 | + |
| 166 | +`ScaleActionCooldown` is the minimum amount of time to wait between scale operations. This is to allow the metrics to stabilize and avoiuds [flapping](./autoscale-flapping.md). For example, if `ScaleActionCooldown` is 10 minutes and a scale operation just occurred, Autoscale won't attempt to scale again for 10 minutes. |
| 167 | + |
| 168 | + |
| 169 | +### Create an autoscale profile and associate the rules |
| 170 | + |
| 171 | +After defining the scale rules, create a profile. The profile specifies the default, upper, and lower instance count limits, and the times that the associated rules can be applied. Use thew [`New-AzAutoscaleProfileObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscaleprofileobject) command to create a new autoscale profile. |
| 172 | + |
| 173 | +```azurecli |
| 174 | +$profile1=New-AzAutoscaleProfileObject ` |
| 175 | + -Name "profile1" ` |
| 176 | + -CapacityDefault 1 ` |
| 177 | + -CapacityMaximum 10 ` |
| 178 | + -CapacityMinimum 1 ` |
| 179 | + -RecurrenceFrequency week ` |
| 180 | + -ScheduleDay "Wednesday","Friday" ` |
| 181 | + -ScheduleHour 7 ` |
| 182 | + -ScheduleMinute 00 ` |
| 183 | + -ScheduleTimeZone "Pacific Standard Time" ` |
| 184 | + -Rule $rule1, $rule2 |
| 185 | +``` |
| 186 | +Parameters |
| 187 | +-FixedDateStart |
| 188 | +the start time for the profile in ISO 8601 format. |
| 189 | +- `CapacityDefault` the number of instances that will be set if metrics are not available for evaluation. The default is only used if the current instance count is lower than the default. |
| 190 | +- `CapacityMaximum` the maximum number of instances for the resource. The maximum number of instances is further limited by the number of cores that are available in the subscription. |
| 191 | +- `CapacityMinimum` the minimum number of instances for the resource. |
| 192 | +-`FixedDateEnd` the end time for the profile in ISO 8601 format. |
| 193 | +-`FixedDateStart` the start time for the profile in ISO 8601 format. |
| 194 | +- `Rule` a collection of rules that provide the triggers and parameters for the scaling action when this profile is active. A maximum of 10, comma separated rules can be specified. |
| 195 | +-`RecurrenceFrequency` How often the scheduled profile takes effect. This value must be *week*. |
| 196 | +-`ScheduleDay` A collection of days that the profile takes effect on when specifying a recurring schedule. Possible values are Sunday through Saturday. For more information on recurring schedules see [Add a recurring profile using CLI](./autoscale-multiprofile.md?tabs=powershell#add-a-recurring-profile-using-powershell) |
| 197 | +- `ScheduleHour` A collection of hours that the profile takes effect on. Values supported are 0 to 23. |
| 198 | +- `ScheduleMinute` A collection of minutes at which the profile takes effect at. |
| 199 | +- `ScheduleTimeZone` The timezone for the hours of the profile. |
0 commit comments