使用 Azure CLI 監視和調整單一 PostgreSQL 伺服器Monitor and scale a single PostgreSQL server using Azure CLI

此範例 CLI 指令碼會在查詢計量之後,對單一「適用於 PostgreSQL 的 Azure 資料庫」伺服器調整計算和儲存。This sample CLI script scales compute and storage for a single Azure Database for PostgreSQL server after querying the metrics. 計算可以擴大或縮小。Compute can scale up or down. 儲存體只能擴大。Storage can only scale up.

重要

儲存體只能擴大,而不能縮小。Storage can only be scaled up, not down.

必要條件Prerequisites

  • Azure Cloud Shell中使用 Bash 環境。Use the Bash environment in Azure Cloud Shell.

    在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window

  • 如果想要,請安裝 Azure CLI 以執行 CLI 參考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。If you're using a local installation, sign in to the Azure CLI by using the az login command. 請遵循您終端機上顯示的步驟,完成驗證程序。To finish the authentication process, follow the steps displayed in your terminal. 如需其他登入選項,請參閱使用 Azure CLI 登入For additional sign-in options, see Sign in with the Azure CLI.

    • 出現提示時,請在第一次使用時安裝 Azure CLI 擴充功能。When you're prompted, install Azure CLI extensions on first use. 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能For more information about extensions, see Use extensions with the Azure CLI.

    • 執行 az version 以尋找已安裝的版本和相依程式庫。Run az version to find the version and dependent libraries that are installed. 若要升級至最新版本,請執行 az upgradeTo upgrade to the latest version, run az upgrade.

  • 本文需要 2.0 版或更新版本的 Azure CLI。This article requires version 2.0 or later of the Azure CLI. 如果您是使用 Azure Cloud Shell,就已安裝最新版本。If using Azure Cloud Shell, the latest version is already installed.

範例指令碼Sample script

使用您的訂用帳戶識別碼來更新指令碼。Update the script with your subscription ID.

#!/bin/bash

# Set up variables
RESOURCE_GROUP="myresourcegroup"
SERVER_NAME="mydemoserver-$RANDOM"
PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 12)
echo password $PASSWORD # returns the generated password
LOCATION="westus"
ADMIN_USER="myadmin"
SUBSCRIPTION_ID="" # enter your subscription ID

# Create a resource group
az group create \
    --name $RESOURCE_GROUP \
    --location $LOCATION

# Create a PostgreSQL server in the resource group
az postgres server create \
    --name $SERVER_NAME \
    --resource-group $RESOURCE_GROUP \
    --location $LOCATION \
    --admin-user $ADMIN_USER \
    --admin-password $PASSWORD \
    --sku-name GP_Gen5_2

# Monitor usage metrics - CPU
az monitor metrics list \
    --resource "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.DBforPostgreSQL/servers/$SERVER_NAME" \
    --metric cpu_percent \
    --interval PT1M

# Monitor usage metrics - Storage
az monitor metrics list \
    --resource "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.DBforPostgreSQL/servers/$SERVER_NAME" \
    --metric storage_used \
    --interval PT1M

# Scale up the server by provisionining more vCores within the same tier
az postgres server update \
    --resource-group $RESOURCE_GROUP \
    --name $SERVER_NAME \
    --sku-name GP_Gen5_4

# Scale down the server by provisioning fewer vCores within the same tier
az postgres server update \
    --resource-group $RESOURCE_GROUP \
    --name $SERVER_NAME \
    --sku-name GP_Gen5_2

# Scale up the server to provision a storage size of 7GB
# Storage size cannot be reduced
az postgres server update \
    --resource-group $RESOURCE_GROUP \
    --name $SERVER_NAME \
    --storage-size 7168

清除部署Clean up deployment

執行指令碼之後,請使用下列命令來移除資源群組及其相關聯的所有資源。Use the following command to remove the resource group and all resources associated with it after the script has been run.

#!/bin/bash
RESOURCE_GROUP = "" # enter your resource group name
az group delete --name $RESOURCE_GROUP

指令碼說明Script explanation

此指令碼會使用下表中簡述的命令:This script uses the commands outlined in the following table:

命令Command 注意事項Notes
az group createaz group create 建立用來存放所有資源的資源群組。Creates a resource group in which all resources are stored.
az postgres server createaz postgres server create 建立主控資料庫的 PostgreSQL 伺服器。Creates a PostgreSQL server that hosts the databases.
az postgres server updateaz postgres server update 更新 PostgreSQL 伺服器的屬性。Updates properties of the PostgreSQL server.
az monitor metrics listaz monitor metrics list 列出資源的度量值。List the metric value for the resources.
az group deleteaz group delete 刪除資源群組,包括所有的巢狀資源。Deletes a resource group including all nested resources.

後續步驟Next steps