此範例 CLI 腳本在查詢指標後,擴展單一 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器的運算與儲存空間。 你可以擴展或縮減運算資源。 你只能擴充儲存空間。
這很重要
你只能放大儲存空間,不能縮小。
如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
先決條件
使用 Azure Cloud Shell 中的 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱 如何在 Docker 容器中執行 Azure CLI。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
範例腳本
啟動 Azure Cloud Shell
Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。
若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以前往 https://shell.azure.com ,在新的瀏覽器分頁中開啟 Cloud Shell。
當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。
登入 Azure
Cloud Shell 會在首次登入的帳戶下自動完成身份驗證。 使用下列腳本使用不同的訂用帳戶登入,並將 subscriptionId 取代為您的 Azure 訂用帳戶標識碼。
如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
執行指令碼
# Monitor and scale a single PostgreSQL server
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
subscriptionId="$(az account show --query id -o tsv)"
location="East US"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tag="scale-postgresql-server"
server="msdocs-postgresql-server-$randomIdentifier"
sku="GP_Gen5_2"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
scaleUpSku="GP_Gen5_4"
scaleDownSku="GP_Gen5_2"
storageSize="102400"
echo "Using resource group $resourceGroup with login: $login, password: $password..."
# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag
# Create a PostgreSQL server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
echo "Creating $server in $location..."
az postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku
# Monitor usage metrics - CPU
echo "Returning the CPU usage metrics for $server"
az monitor metrics list --resource "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.DBforPostgresql/servers/$server" --metric cpu_percent --interval PT1M
# Monitor usage metrics - Storage
echo "Returning the storage usage metrics for $server"
az monitor metrics list --resource "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.DBforPostgresql/servers/$server" --metric storage_used --interval PT1M
# Scale up the server by provisionining more vCores within the same tier
echo "Scaling up $server by changing the SKU to $scaleUpSku"
az postgres server update --resource-group $resourceGroup --name $server --sku-name $scaleUpSku
# Scale down the server by provisioning fewer vCores within the same tier
echo "Scaling down $server by changing the SKU to $scaleDownSku"
az postgres server update --resource-group $resourceGroup --name $server --sku-name $scaleDownSku
# Scale up the server to provision a storage size of 10GB
# Storage size cannot be reduced
echo "Scaling up the storage size for $server to $storageSize"
az postgres server update --resource-group $resourceGroup --name $server --storage-size $storageSize
清除部署
您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。
az group delete --name $resourceGroup
範例參考
此指令碼會使用下表中簡述的命令:
| 命令 | 注意事項 |
|---|---|
| 請輸入命令:az group create 來建立群組。 | 建立一個資源群組來儲存所有資源。 |
| az postgres server create 這是一個用於建立 PostgreSQL 伺服器的命令 | 建立一個用來裝載資料庫的 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器。 |
| az postgres 伺服器更新 | 更新 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器的屬性。 |
| az monitor 指標清單 | 列出資源的計量值。 |
| az 群組刪除 | 刪除包含所有巢狀資源的資源群組。 |