Rugalmas készlet monitorozása és méretezése a PowerShell használatával az Azure SQL Database-ben

A következőre vonatkozik: Azure SQL Database

Ez a PowerShell-példaszkript egy rugalmas készlet teljesítmény-mérőszámait monitorozza, egy magasabb számítási mérethez skálázza a készletet, és létrehoz egy riasztási szabályt az egyik teljesítmény-mérőszámon.

Ha nem rendelkezik Azure-előfizetéssel, első lépésként hozzon létre egy ingyenes Azure-fiókot.

Megjegyzés:

Ez a cikk az Azure Az PowerShell-modult használja, amely az Azure-ral való interakcióhoz ajánlott PowerShell-modul. Az Az PowerShell-modul használatának megkezdéséhez lásd az Azure PowerShell telepítését ismertető szakaszt. Az Az PowerShell-modulra történő migrálás részleteiről lásd: Az Azure PowerShell migrálása az AzureRM modulból az Az modulba.

Az Azure Cloud Shell használata

Az Azure által üzemeltetett Azure Cloud Shell egy interaktív felület, amelyet a böngészőből használhat. A Bash vagy a PowerShell segítségével is használhatja a Cloud Shellt az Azure-szolgáltatásokhoz. A Cloud Shell előre telepített parancsaival futtathatja a jelen cikkben szereplő kódot anélkül, hogy bármit telepítenie kellene a helyi környezetben.

Az Azure Cloud Shell indítása:

Beállítás Példa/hivatkozás
Kattintson a Kipróbálás elemre egy kódblokk jobb felső sarkában. A Kipróbálás lehetőségre kattintással nem másolja a kódot automatikusan a Cloud Shellbe. Screenshot that shows an example of Try It for Azure Cloud Shell.
Látogasson el a https://shell.azure.com webhelyre, vagy kattintson a Cloud Shell indítása gombra a böngészőben. Screenshot that shows how to launch Cloud Shell in a new window.
Az Azure Portal jobb felső sarkában található menüben kattintson a Cloud Shell gombra. Screenshot that shows the Cloud Shell button in the Azure portal

Az ebben a cikkben szereplő kód Azure Cloud Shellben való futtatásához:

  1. Indítsa el a Cloud Shellt.

  2. A kód másolásához válassza az adott kódblokkhoz tartozó Másolás gombot.

  3. Illessze be a kódot a Cloud Shell-munkamenetbe a Windowson és Linuxon a Ctrl Shift+V billentyűkombinációval+, vagy macOS rendszeren a Cmd+Shift+V billentyűkombinációval.

  4. A kód futtatásához nyomja le az Enter billentyűt.

Ha a PowerShell helyi telepítése és használata mellett dönt, ehhez az oktatóanyaghoz az Az PowerShell 1.4.0-s vagy újabb verziójára van szükség. Ha frissíteni szeretne, olvassa el az Azure PowerShell-modul telepítését ismertető cikket. Ha helyileg futtatja a PowerShellt, akkor emellett a Connect-AzAccount futtatásával kapcsolatot kell teremtenie az Azure-ral.

Példaszkript

# 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 = ''
# 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 = "SqlAdmin"
$password = (New-Guid).Guid # Generates a randomized GUID 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

# Create a new server with a system wide unique server name
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -Location $location `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))

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

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

# Create two blank database in the pool
$firstDatabase = New-AzSqlDatabase  -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $firstDatabaseName `
    -ElasticPoolName $poolName
$secondDatabase = New-AzSqlDatabase  -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $secondDatabaseName `
    -ElasticPoolName $poolName

# 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
$elasticPool = Set-AzSqlElasticPool -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -ElasticPoolName $poolName `
    -Edition "Standard" `
    -Dtu 100 `
    -DatabaseDtuMin 20 `
    -DatabaseDtuMax 100

# 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 an 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.
$actionGroup = Set-AzActionGroup `
    -Name "mysample-email-the-azure-admins" `
    -ShortName "AzAdminsGrp" `
    -ResourceGroupName $resourceGroupName `
    -Receiver $receiver

# 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.
$criteria = New-AzMetricAlertRuleV2Criteria `
    -MetricName "dtu_consumption_percent" `
    -TimeAggregation Average `
    -Operator GreaterThan `
    -Threshold 90

# Create the Alert rule.
# Add-AzMetricAlertRuleV2 adds or updates a V2 (non-classic) metric-based alert rule.
Add-AzMetricAlertRuleV2 -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

<#
# 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.
Add-AzMetricAlertRule -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)
#>

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

Az üzemelő példány eltávolítása

Az alábbi paranccsal távolítsa el az erőforráscsoportot és a hozzá társított összes erőforrást.

Remove-AzResourceGroup -ResourceGroupName $resourcegroupname

Szkript ismertetése

A szkript a következő parancsokat használja. A táblázatban lévő összes parancs a hozzá tartozó dokumentációra hivatkozik.

Parancs Megjegyzések
New-AzResourceGroup Létrehoz egy erőforráscsoportot, amely az összes erőforrást tárolja.
New-AzSqlServer Adatbázisokat vagy rugalmas készleteket üzemeltető kiszolgálót hoz létre.
New-AzSqlElasticPool Rugalmas készletet hoz létre.
New-AzSqlDatabase Adatbázist hoz létre egy kiszolgálón.
Get-AzMetric Megjeleníti az adatbázis méretkihasználtsági adatait.
Set-AzSqlElasticPool Frissíti a rugalmas készlet tulajdonságait.
Add-AzMetricAlertRule (Elavult) Riasztási szabályt ad hozzá vagy frissít a metrikák jövőbeni automatikus figyeléséhez. Csak a klasszikus metrikaalapú riasztási szabályokra vonatkozik.
Add-AzMetricAlertRuleV2 Hozzáad vagy frissít egy riasztási szabályt a metrikák jövőbeni automatikus figyeléséhez. Csak a nem klasszikus metrikaalapú riasztási szabályokra vonatkozik.
Remove-AzResourceGroup Töröl egy erőforráscsoportot az összes beágyazott erőforrással együtt.

További lépések

Az Azure PowerShellről további információt az Azure PowerShell dokumentációjában talál.

További PowerShell-példaszkriptminták találhatók az Azure PowerShell-szkriptekben.