PowerShell gebruiken om een elastische pool in Azure SQL Database te bewaken en te schalen

van toepassing op:Azure SQL Database-

In dit PowerShell-voorbeeldscript worden de metrische prestatiegegevens van een elastische pool bewaakt, geschaald naar een hogere rekenkracht en wordt een waarschuwingsregel gemaakt voor een van de metrische prestatiegegevens.

Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

Notitie

In dit artikel wordt de Azure Az PowerShell-module gebruikt. Dit is de aanbevolen PowerShell-module voor interactie met Azure. Zie Azure PowerShell-installeren om aan de slag te gaan met de Az PowerShell-module. Raadpleeg Azure PowerShell migreren van AzureRM naar Az om te leren hoe u naar de Azure PowerShell-module migreert.

Azure Cloud Shell gebruiken

Azure host Azure Cloud Shell, een interactieve shell-omgeving die u via uw browser kunt gebruiken. U kunt Bash of PowerShell met Cloud Shell gebruiken om te werken met Azure-services. U kunt de vooraf geïnstalleerde Opdrachten van Cloud Shell gebruiken om de code in dit artikel uit te voeren, zonder dat u iets hoeft te installeren in uw lokale omgeving.

Azure Cloud Shell starten:

Optie Voorbeeld/koppeling
Selecteer Nu proberen in de rechterbovenhoek van een codeblok. Als u Try It selecteert, wordt de code niet automatisch gekopieerd naar Cloud Shell. Schermopname met een voorbeeld van Try It for Azure Cloud Shell.
Ga naar https://shell.azure.comof selecteer de knop Cloud Shell starten om Cloud Shell in uw browser te openen. Schermopname van het starten van Cloud Shell in een nieuw venster.
Selecteer de knop Cloud Shell in de menubalk rechtsboven in de Azure Portal. Schermopname van de knop Cloud Shell in Azure Portal

De code uitvoeren in dit artikel in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Selecteer de knop Kopiëren op een codeblok om de code te kopiëren.

  3. Plak de code in de Cloud Shell-sessie door Ctrl+Shift+V- in Windows en Linux te selecteren of door Cmd+Shift+V- in macOS te selecteren.

  4. Selecteer Voer in om de code uit te voeren.

Als u PowerShell lokaal wilt installeren en gebruiken, is voor deze zelfstudie Az PowerShell 1.4.0 of hoger vereist. Zie Azure PowerShell-module installerenals u een upgrade wilt uitvoeren. Als u PowerShell lokaal uitvoert, moet u ook Connect-AzAccount uitvoeren om een verbinding met Azure te maken.

Voorbeeldscript

# This script requires the following
# - Az.Resources
# - Az.Accounts
# - Az.Monitor
# - Az.Sql

# First, run Connect-AzAccount

# Set the subscription in which to create these objects. This is displayed on objects in the Azure portal.
$subscriptionId = "<Subscription-ID>"
# Set the resource group name and location for your server
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "westus2"
# Set elastic pool name
$poolName = "MySamplePool"
# Set an admin login and password for your database
$adminSqlLogin = "<admin>"
$password = "<password>"
# Set server name - the logical server name has to be unique in the system
$serverName = "server-$(Get-Random)"
# The sample database names
$firstDatabaseName = "myFirstSampleDatabase"
$secondDatabaseName = "mySecondSampleDatabase"
# The IP address range that you want to allow to access your server via the firewall rule
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"

# Set subscription
Set-AzContext -SubscriptionId $subscriptionId

# Create a new resource group
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location

$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force)

# Create a new server with a system-wide unique server name
$serverParams = @{
    ResourceGroupName           = $resourceGroupName
    ServerName                  = $serverName
    Location                    = $location
    SqlAdministratorCredentials = $adminCredential
}
$server = New-AzSqlServer @serverParams

# Create elastic database pool
$elasticPoolParams = @{
    ResourceGroupName = $resourceGroupName
    ServerName        = $serverName
    ElasticPoolName   = $poolName
    Edition           = "Standard"
    Dtu               = 50
    DatabaseDtuMin    = 10
    DatabaseDtuMax    = 50
}
$elasticPool = New-AzSqlElasticPool @elasticPoolParams

# Create a server firewall rule that allows access from the specified IP range
$firewallParams = @{
    ResourceGroupName = $resourceGroupName
    ServerName        = $serverName
    FirewallRuleName  = "AllowedIPs"
    StartIpAddress    = $startIp
    EndIpAddress      = $endIp
}
$serverFirewallRule = New-AzSqlServerFirewallRule @firewallParams

# Create two blank databases in the pool
$firstDatabaseParams = @{
    ResourceGroupName = $resourceGroupName
    ServerName        = $serverName
    DatabaseName      = $firstDatabaseName
    ElasticPoolName   = $poolName
}
$firstDatabase = New-AzSqlDatabase @firstDatabaseParams
$secondDatabaseParams = @{
    ResourceGroupName = $resourceGroupName
    ServerName        = $serverName
    DatabaseName      = $secondDatabaseName
    ElasticPoolName   = $poolName
}
$secondDatabase = New-AzSqlDatabase @secondDatabaseParams

# Monitor the DTU consumption of the pool in 5-minute intervals
$monitorParameters = @{
    ResourceId  = "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/elasticPools/$poolName"
    TimeGrain   = [TimeSpan]::Parse("00:05:00")
    MetricNames = "dtu_consumption_percent"
}
$metric = Get-AzMetric @monitorParameters
$metric.Data

# Scale the pool
$scaleParams = @{
    ResourceGroupName = $resourceGroupName
    ServerName        = $serverName
    ElasticPoolName   = $poolName
    Edition           = "Standard"
    Dtu               = 100
    DatabaseDtuMin    = 20
    DatabaseDtuMax    = 100
}
$elasticPool = Set-AzSqlElasticPool @scaleParams

# Set up an Alert rule using Azure Monitor for the database
# Add an Alert that fires when the pool utilization reaches 90%
# Objects needed: an Action Group Receiver, an Action Group, Alert Criteria, and finally an Alert Rule.

# Creates a new action group receiver object with a target email address.
$receiver = New-AzActionGroupReceiver -Name "my Sample Azure Admins" -EmailAddress "azure-admins-group@contoso.com"

# Creates a new or updates an existing action group.
$actionGroupParams = @{
    Name              = "mysample-email-the-azure-admins"
    ShortName         = "AzAdminsGrp"
    ResourceGroupName = $resourceGroupName
    Receiver          = $receiver
}
$actionGroup = Set-AzActionGroup @actionGroupParams

# Fetch the created AzActionGroup into an object of type Microsoft.Azure.Management.Monitor.Models.ActivityLogAlertActionGroup
$actionGroupObject = New-AzActionGroup -ActionGroupId $actionGroup.Id

# Create a criteria for the Alert to monitor.
$criteriaParams = @{
    MetricName      = "dtu_consumption_percent"
    TimeAggregation = "Average"
    Operator        = "GreaterThan"
    Threshold       = 90
}
$criteria = New-AzMetricAlertRuleV2Criteria @criteriaParams

# Create the Alert rule.
# Add-AzMetricAlertRuleV2 adds or updates a V2 (non-classic) metric-based alert rule.
$alertRuleParams = @{
    Name              = "mySample_Alert_DTU_consumption_pct"
    ResourceGroupName = $resourceGroupName
    WindowSize        = (New-TimeSpan -Minutes 1)
    Frequency         = (New-TimeSpan -Minutes 1)
    TargetResourceId  = "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/elasticPools/$poolName"
    Condition         = $criteria
    ActionGroup       = $actionGroupObject
    Severity          = 3 #Informational
}
Add-AzMetricAlertRuleV2 @alertRuleParams

<#
# Set up an alert rule using Azure Monitor for the database
# Add a classic alert that fires when the pool utilization reaches 90%
# Note that Add-AzMetricAlertRule is deprecated. Use Add-AzMetricAlertRuleV2 instead.
$deprecatedAlertParams = @{
    ResourceGroup           = $resourceGroupName
    Name                    = "mySampleAlertRule"
    Location                = $location
    TargetResourceId        = "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/elasticPools/$poolName"
    MetricName              = "dtu_consumption_percent"
    Operator                = "GreaterThan"
    Threshold               = 90
    WindowSize              = $([TimeSpan]::Parse("00:05:00"))
    TimeAggregationOperator = "Average"
    Action                  = $(New-AzAlertRuleEmail -SendToServiceOwner)
}
Add-AzMetricAlertRule @deprecatedAlertParams
#>

# Clean up deployment
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

Uitrol opschonen

Gebruik de volgende opdracht om de resourcegroep en alle bijbehorende resources te verwijderen.

Remove-AzResourceGroup -ResourceGroupName $resourcegroupname

Uitleg van script

In dit script worden de volgende opdrachten gebruikt. Elke opdracht in de tabel is een koppeling naar opdracht-specifieke documentatie.

Opdracht Opmerkingen
New-AzResourceGroup Hiermee maakt u een resourcegroep waarin alle resources worden opgeslagen.
New-AzSqlServer Hiermee maakt u een server die als host fungeert voor databases of elastische pools.
New-AzSqlElasticPool Maakt een elastische pool.
New-AzSqlDatabase Hiermee maakt u een database op een server.
Get-AzMetric Geeft de grootte van de gebruiksgegevens voor de database weer.
Set-AzSqlElasticPool Hiermee worden eigenschappen van elastische pools bijgewerkt.
Add-AzMetricAlertRule (afgeschaft) Hiermee voegt u een waarschuwingsregel toe of werkt u deze bij om in de toekomst automatisch metrische gegevens te bewaken. Alleen van toepassing op klassieke waarschuwingsregels op basis van metrische gegevens.
Add-AzMetricAlertRuleV2 Hiermee voegt u een waarschuwingsregel toe of werkt u deze bij om in de toekomst automatisch metrische gegevens te bewaken. Alleen van toepassing op niet-klassieke waarschuwingsregels op basis van metrische gegevens.
Remove-AzResourceGroup Hiermee verwijdert u een resourcegroep, inclusief alle geneste resources.