Azure CLI を使用して単一の Azure Database for PostgreSQL - フレキシブル サーバー インスタンスを監視およびスケーリングする

適用対象: Azure Database for PostgreSQL - シングル サーバー Azure Database for PostgreSQL - フレキシブル サーバー

このサンプル CLI スクリプトは、メトリックのクエリを実行した後、単一の Azure Database for PostgreSQL フレキシブル サーバー インスタンスのコンピューティングとストレージをスケーリングします。 コンピューティングはスケールアップまたはスケールダウンすることができます。 ストレージはスケールアップすることのみが可能です。

重要

ストレージはスケールアップのみ可能で、スケールダウンすることはできません。

Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

前提条件

  • Azure Cloud Shell で Bash 環境を使用します。 詳細については、「Azure Cloud Shell の Bash のクイックスタート」を参照してください。

  • CLI リファレンス コマンドをローカルで実行する場合、Azure CLI をインストールします。 Windows または macOS で実行している場合は、Docker コンテナーで Azure CLI を実行することを検討してください。 詳細については、「Docker コンテナーで Azure CLI を実行する方法」を参照してください。

    • ローカル インストールを使用する場合は、az login コマンドを使用して Azure CLI にサインインします。 認証プロセスを完了するには、ターミナルに表示される手順に従います。 その他のサインイン オプションについては、Azure CLI でのサインインに関するページを参照してください。

    • 初回使用時にインストールを求められたら、Azure CLI 拡張機能をインストールします。 拡張機能の詳細については、Azure CLI で拡張機能を使用する方法に関するページを参照してください。

    • az version を実行し、インストールされているバージョンおよび依存ライブラリを検索します。 最新バージョンにアップグレードするには、az upgrade を実行します。

サンプル スクリプト

Azure Cloud Shell を起動する

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 は、サインインした最初のアカウントで自動的に認証されます。 別のサブスクリプションを使用してサインインするには、次のスクリプトを使用し、<Subscription ID> をご使用の Azure サブスクリプション ID に置き換えます。 Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

subscription="<subscriptionId>" # add subscription 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 データベースをホストする Azure Database for PostgreSQL フレキシブル サーバー インスタンスを作成します。
az postgres server update Azure Database for PostgreSQL フレキシブル サーバー インスタンスのプロパティを更新します。
az monitor metrics list リソースのメトリック値を一覧表示します。
az group delete 入れ子になったリソースすべてを含むリソース グループを削除します。

次のステップ