Share via


路由傳送流量以達到應用程式的高可用性 - Azure CLI

這個指令碼會建立一個資源群組、兩個 App Service 方案、兩個 Web 應用程式、一個流量管理員設定檔和兩個流量管理員端點。 流量管理員會將流量導向主要區域中的應用程式,然後當主要區域中的應用程式無法使用時,將流量導向至次要區域。 在執行指令碼之前,您必須將 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 應用程式、流量管理員設定檔和所有相關資源。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
az group create 建立用來存放所有資源的資源群組。
az appservice plan create 建立 App Service 方案。 這就像是 Azure Web 應用程式的伺服器陣列。
az webapp create 在 App Service 方案內建立 Azure Web 應用程式。
az network traffic-manager profile create 建立 Azure 流量管理員設定檔。
az network traffic-manager endpoint create 新增端點至 Azure 流量管理員設定檔。

下一步

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件

您可以在 Azure 網路文件中找到其他的 App Service CLI 指令碼範例。