Share via


高可用性アプリケーションのためのトラフィックのルーティング - Azure CLI

このスクリプトは、1 つのリソース グループ、2 つの App Service プラン、2 つの Web アプリ、1 つの Traffic Manager プロファイル、および 2 つの Traffic Manager エンドポイントを作成します。 Traffic Manager は、プライマリ リージョンのアプリケーションにトラフィックを誘導し、プライマリ リージョンのアプリケーションが利用できない場合には、セカンダリ リージョンにトラフィックを誘導します。 スクリプトを実行する前に、MyWebApp、MyWebAppL1、および MyWebAppL2 の値を、Azure 全体で一意の値に変更する必要があります。 スクリプトを実行した後は、URL mywebapp.trafficmanager.net でプライマリ リージョンのアプリケーションにアクセスできます。

このサンプルを実行するには、最新バージョンの Azure CLI をインストールします。 開始するには、az login を実行して、Azure との接続を作成します。

Azure CLI のサンプルは、bash シェル用に記述されています。 このサンプルを Windows PowerShell またはコマンド プロンプトで実行するには、スクリプトの要素を変更する必要があります。

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

サンプル スクリプト

#!/bin/bash
# Passed validation in Cloud Shell on 2/28/2022

# <FullScript>
# Route traffic for high availability of applications

# Variables for Traffic Manager resources
let "randomIdentifier=$RANDOM*$RANDOM"
location1="East US"
location2="West Europe"
resourceGroup1="msdocs-tm-rg1-$randomIdentifier"
resourceGroup2="msdocs-tm-rg2-$randomIdentifier"
tag="direct-traffic-for-increased-application-availability"
webApp="msdocs-webapp-tm-$randomIdentifier"
webAppL1="msdocs-tm-webapp-L1-$randomIdentifier"
webAppL2="msdocs-tm-webapp-L2-$randomIdentifier"
trafficManagerProfile="msdocs-traffic-manager-profile-$randomIdentifier"

# Create a resource group in location one
echo "Creating $resourceGroup1 in $location1..."
az group create --name $resourceGroup1 --location "$location1" --tags $tag

# Create a resource group in location two
echo "Creating $resourceGroup2 in $location2..."
az group create --name $resourceGroup2 --location "$location2" --tags $tag

# Create a website deployed from GitHub in both regions (replace with your own GitHub URL).
gitrepo="https://github.com/Azure-Samples/app-service-web-dotnet-get-started.git"

# Create a hosting plan and website and deploy it in location one (requires Standard 1 minimum SKU).
echo "Creating $webAppL1 app service plan"
az appservice plan create \
  --name $webAppL1 \
  --resource-group $resourceGroup1 \
  --sku S1

echo "Creating $webAppL1 web app"
az webapp create \
  --name $webAppL1 \
  --resource-group $resourceGroup1 \
  --plan $webAppL1

echo "Deploying $gitrepo to $webAppL1"
az webapp deployment source config \
  --name $webAppL1 \
  --resource-group $resourceGroup1 \
  --repo-url $gitrepo \
  --branch master \
  --manual-integration

# Create a hosting plan and website and deploy it in westus (requires Standard 1 minimum SKU).
echo "Creating $webAppL2 app service plan"
az appservice plan create \
  --name $webAppL2 \
  --resource-group $resourceGroup2 \
  --sku S1

echo "Creating $webAppL2 web app"
az webapp create \
  --name $webAppL2 \
  --resource-group $resourceGroup2 \
  --plan $webAppL2

echo "Deploying $gitrepo to $webAppL2"
az webapp deployment source config \
  --name $webAppL2 \
  --resource-group $resourceGroup2 \
  --repo-url $gitrepo \
  --branch master --manual-integration

# Create a Traffic Manager profile.
echo "Creating $trafficManagerProfile for $webApp"
az network traffic-manager profile create \
  --name $trafficManagerProfile \
  --resource-group $resourceGroup1 \
  --routing-method Priority \
  --unique-dns-name $webApp

# Create a traffic manager endpoint for the location one website deployment and set it as the priority target.
echo "Create traffic manager endpoint for $webAppL1"
l1Id=$(az webapp show \
  --resource-group $resourceGroup1 \
  --name $webAppL1 \
  --query id \
  --out tsv)
az network traffic-manager endpoint create \
  --name endPoint1 \
  --profile-name $trafficManagerProfile \
  --resource-group $resourceGroup1 \
  --type azureEndpoints \
  --priority 1 \
  --target-resource-id $l1Id

# Create a traffic manager endpoint for the location two website deployment and set it as the secondary target.
echo "Create traffic manager endpoint for $webAppL1"
l2Id=$(az webapp show \
  --resource-group $resourceGroup2 \
  --name $webAppL2 \
  --query id --out tsv)
az network traffic-manager endpoint create \
  --name endPoint2 \
  --profile-name $trafficManagerProfile \
  --resource-group $resourceGroup1 \
  --type azureEndpoints \
  --priority 2 \
  --target-resource-id $l2Id
# </FullScript>

# echo "Deleting all resources"
# az group delete --name $resourceGroup1 -y
# az group delete --name $resourceGroup2 -y

デプロイのクリーンアップ

サンプル スクリプトの実行後、次のコマンドを使用すると、リソース グループ、App Service アプリ、すべての関連リソースなどが削除できます。

az group delete --name myResourceGroup1 --yes
az group delete --name myResourceGroup2 --yes

スクリプトの説明

このスクリプトでは、次のコマンドを使用して、リソース グループ、Web アプリ、Traffic Manager プロファイル、およびすべての関連リソースを作成します。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド メモ
az group create すべてのリソースを格納するリソース グループを作成します。
az appservice plan create App Service プランを作成します。 App Service プランとは、Azure Web アプリ用のサーバー ファームのようなものです。
az webapp create App Service プラン内に Azure Web アプリを作成します。
az network traffic-manager profile create Azure Traffic Manager プロファイルを作成します。
az network traffic-manager endpoint create Azure Traffic Manager プロファイルにエンドポイントを追加します。

次のステップ

Azure CLI の詳細については、Azure CLI のドキュメントのページをご覧ください。

その他の App Service の CLI サンプル スクリプトは、Azure のネットワークに関するドキュメントのページにあります。