Skip to content

Commit 25390da

Browse files
committed
WIP 2
1 parent 1d1e0c8 commit 25390da

File tree

1 file changed

+104
-72
lines changed

1 file changed

+104
-72
lines changed
Lines changed: 104 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Configure autoscale with PowerShell
2+
title: Configure autoscale using PowerShell
33
description: Configure autoscale for a Virtal Machine Scale Set using PowerShell
44
author: EdB-MSFT
55
ms.author: edbaynash
@@ -16,19 +16,19 @@ Benefits of using Poowershell to configurye autoscale
1616
Prereqs
1717
- give powershell to create VMSS ? ( how do they create load ?)
1818
- Windows or linux ? ( linux)
19-
- provide a vm image ? (git , galery ? which user owns it ?)
19+
- provide a vm image ? (git, galery ? which user owns it ?)
2020
- assume VMSS exists ?
2121
- create an image - too complicated
2222

2323
Define the scenario
24-
scripts to create the objects.
24+
scripts to create the objects.
2525
For each Object define what is required with example
2626

2727

2828
---
2929

3030
# Configure autoscale with PowerShell
31-
31+
3232
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.
3333

3434
This article shows you haw to configure autoscale for a Virtual Machine Scale Set using Powershell
@@ -52,11 +52,13 @@ Create a scale set using the following commands
5252

5353
```azurepowershell
5454
55+
# Create login credtions for the VMSS.
5556
$vmPassword = ConvertTo-SecureString "ChangeThisPassword1" -AsPlainText -Force
5657
$vmCred = New-Object System.Management.Automation.PSCredential('azureuser', $vmPassword)
5758
59+
#set the VMSS name and resource group name
5860
$resourceGroupName="rg-powershell-autoscale"
59-
$VMSSName="vmss-001"
61+
$vmssName="vmss-001"
6062
6163
# create a new resource group
6264
@@ -65,18 +67,7 @@ New-AzResourceGroup -ResourceGroupName $resourceGroupName -Location "EastUS"
6567
New-AzVmss `
6668
-ResourceGroupName $resourceGroupName `
6769
-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 `
70+
-VMScaleSetName $vmssName `
8071
-Credential $vmCred `
8172
-VirtualNetworkName "myVnet" `
8273
-SubnetName "mySubnet" `
@@ -97,19 +88,19 @@ To create autoscale setting using Powershell, follow the sequence below:
9788
Create scale in and scale out rules then associated them with a profile.
9889
Rules are created using the [`New-AzAutoscaleScaleRuleObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalescaleruleobject).
9990

100-
The wolloing Powershell script created two rules.
91+
The following Powershell script created two rules.
10192

10293
+ Scale out when Percentage CPU exceeds 70%
10394
+ Scale in when Percentage CPU is less than 30%
10495

10596
```azurepowershell
10697
10798
$resourceGroupName="rg-powershell-autoscale"
108-
$VMSSName="vmss-001"
99+
$vmssName="vmss-001"
109100
110101
$rule1=New-AzAutoscaleScaleRuleObject `
111102
-MetricTriggerMetricName "Percentage CPU" `
112-
-MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$VMSSName" `
103+
-MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName" `
113104
-MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
114105
-MetricTriggerStatistic "Average" `
115106
-MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
@@ -124,53 +115,42 @@ $rule1=New-AzAutoscaleScaleRuleObject `
124115
125116
126117
$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))
118+
-MetricTriggerMetricName "Percentage CPU" `
119+
-MetricTriggerMetricResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName" `
120+
-MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
121+
-MetricTriggerStatistic "Average" `
122+
-MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
123+
-MetricTriggerTimeAggregation "Average" `
124+
-MetricTriggerOperator "LessThan" `
125+
-MetricTriggerThreshold 30 `
126+
-MetricTriggerDividePerInstance $false `
127+
-ScaleActionDirection "Decrease" `
128+
-ScaleActionType "ChangeCount" `
129+
-ScaleActionValue 1 `
130+
-ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
140131
```
141132

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.
133+
|Parameter| Description|
134+
|---|---|
135+
|`MetricTriggerMetricName` |Sets the autoscale trigger metric
136+
|`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`.
137+
|`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`.
138+
|`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.
139+
|`MetricTriggerStatistic` |The aggregation method used to aggregate the sampled metrics. For example, TimeAggregation = "Average" will aggregate the sampled metrics by taking the average.
140+
|`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.
141+
|`MetricTriggerThreshold`|Defines the value of the metric that triggers a scale event.
142+
|`MetricTriggerOperator` |Specifies the logical comparative operating to use when evaluating the metric value.
143+
|`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.
144+
| `ScaleActionDirection`| Specify scaling in or out. Valid values are `Increase` and `Decrease`.
145+
|`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`.
146+
|`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.
167147

168148

169149
### Create an autoscale profile and associate the rules
170150

171151
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.
172152

173-
```azurecli
153+
```azurepowershell
174154
$profile1=New-AzAutoscaleProfileObject `
175155
-Name "profile1" `
176156
-CapacityDefault 1 `
@@ -184,16 +164,68 @@ $profile1=New-AzAutoscaleProfileObject `
184164
-Rule $rule1, $rule2
185165
```
186166
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.
167+
|Parameter| Description|
168+
|---|---|
169+
|`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.
170+
| `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.
171+
| `CapacityMinimum` |The minimum number of instances for the resource.
172+
|`FixedDateEnd`| The end time for the profile in ISO 8601 format for.
173+
|`FixedDateStart` |The start time for the profile in ISO 8601 format.
174+
| `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.
175+
|`RecurrenceFrequency` | How often the scheduled profile takes effect. This value must be `week`.
176+
|`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)
177+
|`ScheduleHour`| A collection of hours that the profile takes effect on. Values supported are 0 to 23.
178+
|`ScheduleMinute`| A collection of minutes at which the profile takes effect at.
179+
|`ScheduleTimeZone` |The timezone for the hours of the profile.
180+
181+
### Apply the autoscle settings
182+
183+
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)
184+
185+
```azurepowershell
186+
New-AzAutoscaleSetting `
187+
-Name vmss-autoscalesetting1 `
188+
-ResourceGroupName $resGroup `
189+
-Location eastus `
190+
-Profile $profile1 `
191+
-Enabled `
192+
-PropertiesName "test-autoscalesetting" `
193+
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
194+
```
195+
196+
197+
### Add notifications to your autoscale settings
198+
199+
Add notifications to your sale setting to trigger a webhook or send email notifications when a scale event occurs.
200+
For more information on webhook notifications, see [`New-AzAutoscaleWebhookNotificationObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalewebhooknotificationobject)
201+
202+
To set a web hook;
203+
```azurepowershell
204+
205+
$webhook1=New-AzAutoscaleWebhookNotificationObject -Property @{} -ServiceUri "http://contoso.com/wbhook1"
206+
```
207+
208+
Configure the notification using the webhook and set up email notification using the [`New-AzAutoscaleNotificationObject`](https://learn.microsoft.com/powershell/module/az.monitor/new-azautoscalenotificationobject) command:
209+
210+
```azurepowershell
211+
212+
$notification1=New-AzAutoscaleNotificationObject `
213+
-EmailCustomEmail "[email protected]" `
214+
-EmailSendToSubscriptionAdministrator $true `
215+
-EmailSendToSubscriptionCoAdministrator $true `
216+
-Webhook $webhook1
217+
```
218+
219+
Update your autoscale settings to apply the notification
220+
221+
```azurepowershell
222+
223+
Update-AzAutoscaleSetting `
224+
-Name vmss-autoscalesetting1 `
225+
-ResourceGroupName $resourceGroupName `
226+
-Profile $profile1 `
227+
-Notification $notification1 `
228+
-TargetResourceUri "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/$vmssName"
229+
230+
```
231+

0 commit comments

Comments
 (0)