Skip to content

Commit a81dca6

Browse files
committed
review
1 parent 0901a2c commit a81dca6

File tree

1 file changed

+110
-45
lines changed

1 file changed

+110
-45
lines changed

articles/azure-monitor/autoscale/autoscale-using-powershell.md

Lines changed: 110 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Configure autoscale using PowerShell
3-
description: Configure autoscale for a Virtal Machine Scale Set using PowerShell
3+
description: Configure autoscale for a Virtual Machine Scale Set using PowerShell
44
author: EdB-MSFT
55
ms.author: edbaynash
66
ms.topic: how-to
@@ -14,27 +14,28 @@ ms.reviewer: akkumari
1414

1515
# Configure autoscale with PowerShell
1616

17-
Autoscale settings help ensure that you have the right amount of resources running to handle the fluctuating load of your application. You can configure autoscale using the Azure portal, Azure CLI, PowerShell or ARM or Bicep templates.
17+
Autoscale settings help ensure that you have the right amount of resources running to handle the fluctuating load of your application. You can configure autoscale using the Azure portal, Azure CLI, PowerShell or ARM or Bicep templates.
1818

19-
This article shows you how to configure autoscale for a Virtual Machine Scale Set using Powershell using the following steps:
19+
This article shows you how to configure autoscale for a Virtual Machine Scale Set with PowerShell, using the following steps:
2020
+ Create a scale set that you can autoscale
2121
+ Create rules to scale in and scale out
2222
+ Create a profile that uses your rules
2323
+ Apply the autoscale settings
2424
+ Update your autoscale settings with notifications
2525

26-
## Prerequisites
26+
## Prerequisites
2727

2828
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).
2929

3030

3131
## Create a Virtual Machine Scale Set
3232

33-
Create a scale set using the following cmdlets
33+
Create a scale set using the following cmdlets. Set the `$resourceGroupName` and `$vmssName` variables to suite your environment.
3434

3535
```azurepowershell
3636
#Set the VMSS name and resource group name
3737
38+
$subscriptionId = (Get-AzContext).Subscription.Id
3839
$resourceGroupName="rg-powershell-autoscale"
3940
$vmssName="vmss-001"
4041
@@ -55,12 +56,12 @@ New-AzVmss `
5556
-SubnetName "mySubnet" `
5657
-PublicIpAddressName "myPublicIPAddress" `
5758
-LoadBalancerName "myLoadBalancer" `
58-
-UpgradePolicyMode "Automatic" `
59+
-UpgradePolicyMode "Automatic"
5960
```
6061

6162
## Create autoscale settings
6263

63-
To create autoscale setting using Powershell, follow the sequence below:
64+
To create autoscale setting using PowerShell, follow the sequence below:
6465

6566
1. Create rules using `New-AzAutoscaleScaleRuleObject`
6667
1. Create a profile using `New-AzAutoscaleProfileObject`
@@ -71,7 +72,7 @@ To create autoscale setting using Powershell, follow the sequence below:
7172
Create scale in and scale out rules then associated them with a profile.
7273
Rules are created using the [`New-AzAutoscaleScaleRuleObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalescaleruleobject).
7374

74-
The following Powershell script created two rules.
75+
The following PowerShell script created two rules.
7576

7677
+ Scale out when Percentage CPU exceeds 70%
7778
+ Scale in when Percentage CPU is less than 30%
@@ -91,8 +92,8 @@ $rule1=New-AzAutoscaleScaleRuleObject `
9192
-MetricTriggerOperator "GreaterThan" `
9293
-MetricTriggerThreshold 70 `
9394
-MetricTriggerDividePerInstance $false `
94-
-ScaleActionDirection "Increase"
95-
-ScaleActionType "ChangeCount" `
95+
-ScaleActionDirection "Increase" `
96+
-ScaleActionType "ChangeCount" `
9697
-ScaleActionValue 1 `
9798
-ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
9899
@@ -105,7 +106,7 @@ $rule2=New-AzAutoscaleScaleRuleObject `
105106
-MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
106107
-MetricTriggerTimeAggregation "Average" `
107108
-MetricTriggerOperator "LessThan" `
108-
-MetricTriggerThreshold 30 `
109+
-MetricTriggerThreshold 30 `
109110
-MetricTriggerDividePerInstance $false `
110111
-ScaleActionDirection "Decrease" `
111112
-ScaleActionType "ChangeCount" `
@@ -116,75 +117,69 @@ $rule2=New-AzAutoscaleScaleRuleObject `
116117
|Parameter| Description|
117118
|---|---|
118119
|`MetricTriggerMetricName` |Sets the autoscale trigger metric
119-
|`MetricTriggerMetricResourceUri`| Specifies the resource that the `MetricTriggerMetricName` metric belongs to. `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`.
120+
|`MetricTriggerMetricResourceUri`| Specifies the resource that the `MetricTriggerMetricName` metric belongs to. `MetricTriggerMetricResourceUri` can be any resource and not just the resource that's 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`.
120121
|`MetricTriggerTimeGrain`|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`.
121-
|`MetricTriggerTimeAggregation` | 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.
122-
|`MetricTriggerStatistic` |The aggregation method used to aggregate the sampled metrics. For example, TimeAggregation = "Average" will aggregate the sampled metrics by taking the average.
123-
|`MetricTriggerTimeWindow` | The 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.
122+
|`MetricTriggerTimeAggregation` | The aggregation method within the timeGrain period. For example, statistic = "Average" and timeGrain = "PT1M" means that the metrics are aggregated every 1 minute by taking the average.
123+
|`MetricTriggerStatistic` |The aggregation method used to aggregate the sampled metrics. For example, TimeAggregation = "Average" aggregates the sampled metrics by taking the average.
124+
|`MetricTriggerTimeWindow` | The 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 feature allows your metrics to stabilize and avoids reacting to transient spikes.
124125
|`MetricTriggerThreshold`|Defines the value of the metric that triggers a scale event.
125126
|`MetricTriggerOperator` |Specifies the logical comparative operating to use when evaluating the metric value.
126-
|`MetricTriggerDividePerInstance`| When set 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.
127+
|`MetricTriggerDividePerInstance`| When set to `true` divides the trigger metric by the total number of instances. For 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.
127128
| `ScaleActionDirection`| Specify scaling in or out. Valid values are `Increase` and `Decrease`.
128-
|`ScaleActionType` |Scale by a number of instances, to a specific instance count, or by percentage of the current instance count. Valid value include `ChangeCount`, `ExactCount`, and `PercentChangeCount`.
129+
|`ScaleActionType` |Scale by a number of instances, to a specific instance count, or by percentage of the current instance count. Valid values include `ChangeCount`, `ExactCount`, and `PercentChangeCount`.
129130
|`ScaleActionCooldown`| The minimum amount of time to wait between scale operations. This is to allow the metrics to stabilize and avoids [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.
130131

131132

132-
### Create an autoscale profile and associate the rules
133+
### Create a default autoscale profile and associate the rules
133134

134-
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 the [`New-AzAutoscaleProfileObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscaleprofileobject) cmdlet to create a new autoscale profile.
135+
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 the [`New-AzAutoscaleProfileObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscaleprofileobject) cmdlet to create a new autoscale profile. As this is a default profile, it doesn't have any schedule parameters. The default profile is active at times that no other profiles are active
135136

136137
```azurepowershell
137-
$profile1=New-AzAutoscaleProfileObject `
138-
-Name "profile1" `
138+
$defaultProfile=New-AzAutoscaleProfileObject `
139+
-Name "default" `
139140
-CapacityDefault 1 `
140141
-CapacityMaximum 10 `
141142
-CapacityMinimum 1 `
142-
-RecurrenceFrequency week `
143-
-ScheduleDay "Wednesday","Friday" `
144-
-ScheduleHour 7 `
145-
-ScheduleMinute 00 `
146-
-ScheduleTimeZone "Pacific Standard Time" `
147143
-Rule $rule1, $rule2
148144
```
149145

150146
Parameters
151147

152148
|Parameter|Description|
153149
|---|---|
154-
|`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.
150+
|`CapacityDefault`| The number of instances that are if metrics aren't available for evaluation. The default is only used if the current instance count is lower than the default.
155151
| `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.
156152
| `CapacityMinimum` |The minimum number of instances for the resource.
157153
|`FixedDateEnd`| The end time for the profile in ISO 8601 format for.
158154
|`FixedDateStart` |The start time for the profile in ISO 8601 format.
159155
| `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.
160156
|`RecurrenceFrequency` | How often the scheduled profile takes effect. This value must be `week`.
161-
|`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)
157+
|`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)
162158
|`ScheduleHour`| A collection of hours that the profile takes effect on. Values supported are 0 to 23.
163-
|`ScheduleMinute`| A collection of minutes at which the profile takes effect at.
159+
|`ScheduleMinute`| A collection of minutes at which the profile takes effect.
164160
|`ScheduleTimeZone` |The timezone for the hours of the profile.
165161

166-
### Apply the autoscle settings
162+
### Apply the autoscale settings
167163

168-
After fining the rules and profile, apply the autoscale settings using [`New-AzAutoscaleSetting`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalesetting). To update existing autoscale setting use [`AzAutoscaleSetting`](https://learn.microsoft.com/powershell/module/az.monitor/add-azautoscalesetting)
164+
After fining the rules and profile, apply the autoscale settings using [`New-AzAutoscaleSetting`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalesetting). To update existing autoscale setting use [`Update-AzAutoscaleSetting`](https://learn.microsoft.com/powershell/module/az.monitor/add-azautoscalesetting)
169165

170166
```azurepowershell
171167
New-AzAutoscaleSetting `
172168
-Name vmss-autoscalesetting1 `
173-
-ResourceGroupName $resGroup `
169+
-ResourceGroupName $resourceGroupName `
174170
-Location eastus `
175-
-Profile $profile1 `
171+
-Profile $defaultProfile `
176172
-Enabled `
177-
-PropertiesName "test-autoscalesetting" `
178-
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
173+
-PropertiesName "vmss-autoscalesetting1" `
174+
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
179175
```
180176

181-
182-
### Add notifications to your autoscale settings
177+
### Add notifications to your autoscale settings
183178

184179
Add notifications to your sale setting to trigger a webhook or send email notifications when a scale event occurs.
185180
For more information on webhook notifications, see [`New-AzAutoscaleWebhookNotificationObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalewebhooknotificationobject)
186181

187-
To set a web hook;
182+
Set a webhook using the following cmdlet;
188183
```azurepowershell
189184
190185
$webhook1=New-AzAutoscaleWebhookNotificationObject -Property @{} -ServiceUri "http://contoso.com/wbhook1"
@@ -208,38 +203,108 @@ Update your autoscale settings to apply the notification
208203
Update-AzAutoscaleSetting `
209204
-Name vmss-autoscalesetting1 `
210205
-ResourceGroupName $resourceGroupName `
211-
-Profile $profile1 `
206+
-Profile $defaultProfile `
212207
-Notification $notification1 `
213208
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
214209
215210
```
216211

217212
## Review your autoscale settings
218213

219-
To review your autoscale settings load the settings into a variable using `Get-AzAutoscaleSetting` then output the variable as follows:
214+
To review your autoscale settings, load the settings into a variable using `Get-AzAutoscaleSetting` then output the variable as follows:
220215

221216
```azurepowershell
222-
$autoscaleSetting=Get-AzAutoscaleSetting -ResourceGroupName $resourceGroupName -Name vmss-autoscalesetting1 `
217+
$autoscaleSetting=Get-AzAutoscaleSetting -ResourceGroupName $resourceGroupName -Name vmss-autoscalesetting1
223218
$autoscaleSetting | Select-Object -Property *
224219
```
225220

226221
Get your autoscale history using `AzAutoscaleHistory`
227222
```azurepowershell
228-
Get-AzAutoscaleHistory -ResourceId /subscriptions/<subscriptionId/resourceGroups/<resourcegroup name>/providers/Microsoft.Compute/virtualMachineScaleSets/<scaleset name>
223+
Get-AzAutoscaleHistory -ResourceId /subscriptions/<subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName
229224
```
230225

231-
## Other autoscale commands
226+
## Scheduled and recurring profiles
227+
228+
### Add a scheduled profile for a special event
229+
230+
Set up autoscale profiles to scale differently for specific events. For example, for a day when demand will be higher than usual, create a profile with increased maximum and minimum instance limits.
231+
232+
The following example uses the same rules as the default profile defined above, but sets new instance limits for a specific date. You can also configure different rules to be used with the new profile.
233+
234+
```azurepowershell
235+
$highDemandDay=New-AzAutoscaleProfileObject `
236+
-Name "High-demand-day" `
237+
-CapacityDefault 7 `
238+
-CapacityMaximum 30 `
239+
-CapacityMinimum 5 `
240+
-FixedDateEnd ([System.DateTime]::Parse("2023-12-31T14:00:00Z")) `
241+
-FixedDateStart ([System.DateTime]::Parse("2023-12-31T13:00:00Z")) `
242+
-FixedDateTimeZone "UTC" `
243+
-Rule $rule1, $rule2
244+
245+
Update-AzAutoscaleSetting `
246+
-Name vmss-autoscalesetting1 `
247+
-ResourceGroupName $resourceGroupName `
248+
-Profile $defaultProfile, $highDemandDay `
249+
-Notification $notification1 `
250+
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
251+
252+
```
253+
254+
### Add a recurring scheduled profile
255+
256+
Recurring profiles let you schedule a scaling profile that repeats each week. For example, scale to a single instance on the weekend from Friday night to Monday morning.
257+
258+
While scheduled profiles have a start and end date, recurring profiles don't have an end time. The profile remains active until another profile becomes active. You must therefore create recurring default profile to start when you want the previous recurring profile to finish.
259+
For example, to configure a weekend profile that starts on Friday nights and ends on Monday mornings, create one profile that starts on Friday night, and a copy of the default profile that starts on Monday morning.
260+
261+
The following script creates a weekend profile and an addition default profile to end the weekend profile.
262+
```azurepowershell
263+
$fridayProfile=New-AzAutoscaleProfileObject `
264+
-Name "Weekend" `
265+
-CapacityDefault 1 `
266+
-CapacityMaximum 1 `
267+
-CapacityMinimum 1 `
268+
-RecurrenceFrequency week `
269+
-ScheduleDay "Friday" `
270+
-ScheduleHour 22 `
271+
-ScheduleMinute 00 `
272+
-ScheduleTimeZone "Pacific Standard Time" `
273+
-Rule $rule1, $rule2
232274
233-
For a complete list of PowerShell cmdlets for autoscale, see the [Poweshell Module Browser](https://learn.microsoft.com/powershell/module/?term=azautoscale)
234275
276+
$mondayProfile=New-AzAutoscaleProfileObject `
277+
-Name "default2" `
278+
-CapacityDefault 1 `
279+
-CapacityMaximum 10 `
280+
-CapacityMinimum 1 `
281+
-RecurrenceFrequency week `
282+
-ScheduleDay "Monday" `
283+
-ScheduleHour 00 `
284+
-ScheduleMinute 00 `
285+
-ScheduleTimeZone "Pacific Standard Time" `
286+
-Rule $rule1, $rule2
287+
288+
Update-AzAutoscaleSetting `
289+
-Name vmss-autoscalesetting1 `
290+
-ResourceGroupName $resourceGroupName `
291+
-Profile $defaultProfile, $highDemandDay, $fridayProfile, $mondayProfile `
292+
-Notification $notification1 `
293+
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
294+
295+
```
235296

297+
## Other autoscale commands
298+
299+
For a complete list of PowerShell cmdlets for autoscale, see the [PowerShell Module Browser](https://learn.microsoft.com/powershell/module/?term=azautoscale)
236300

237301
## Clean up resources
238302

239-
To cleanup the resources you created in this tutorial delete the resource group that you created.
303+
To clean up the resources you created in this tutorial, delete the resource group that you created.
240304
The following cmdlet deletes the resource group and all of its resources.
241305
```azurecli
242306
243307
Remove-AzResourceGroup -Name $resourceGroupName
244308
245309
```
310+

0 commit comments

Comments
 (0)