このサンプル CLI スクリプトは、メトリックの照会後、単一の Azure Database for PostgreSQL フレキシブル サーバー インスタンスのコンピューティングとストレージをスケーリングします。 コンピューティングはスケールアップまたはスケールダウンすることができます。 ストレージはスケールアップすることのみが可能です。
重要
ストレージはスケールアップのみ可能で、スケールダウンすることはできません。
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 は無料のインタラクティブ シェルです。これを使って、この記事の手順を行うことができます。 一般的な Azure ツールが事前にインストールされており、アカウントで使用するように構成されています。
Cloud Shell を開くには、コード ブロックの右上隅にある [使ってみる] を選択します。 https://shell.azure.com に移動して、別のブラウザー タブで Cloud Shell を起動することもできます。
Cloud Shell が開いたら、環境で Bash が選択されていることを確認します。 後続のセッションでは、Bash 環境で Azure CLI を使用します。[コピー] を選択してコードのブロックをコピーし、Cloud Shell に貼り付けます。その後、Enter キーを押してそれを実行します。
Azure にサインインする
Cloud Shell は、サインインした最初のアカウントで自動的に認証されます。 別のサブスクリプションを使ってサインインするには、次のスクリプトを使用し、subscriptionId をご使用の Azure サブスクリプション ID に置き換えます。
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 コマンドを使用して Azure のグループを作成します。 | すべてのリソースを格納するリソース グループを作成します。 |
| az postgres server create コマンドを使用して、PostgreSQLサーバーを作成します。 | データベースをホストする Azure Database for PostgreSQL フレキシブル サーバー インスタンスを作成します。 |
| az postgres server update | Azure Database for PostgreSQL フレキシブル サーバー インスタンスのプロパティを更新します。 |
| az monitor metrics list | リソースのメトリック値を一覧表示します。 |
| az group delete コマンドを使用して、リソースグループを削除します。 | 入れ子になったリソースすべてを含むリソース グループを削除します。 |