使用 Azure CLI 路由傳送流量以達到應用程式的高可用性
這個指令碼會建立一個資源群組、兩個 App Service 方案、兩個 Web 應用程式、一個流量管理員設定檔和兩個流量管理員端點。 流量管理員會將流量導向主要區域中的應用程式,然後當主要區域中的應用程式無法使用時,將流量導向至次要區域。 在執行指令碼之前,您必須將 MyWebApp、MyWebAppL1 和 MyWebAppL2 值都變更為整個 Azure 中的唯一值。 在執行指令碼之後,您可以透過 URL mywebapp.trafficmanager.net 來存取主要區域中的應用程式。
如果您沒有 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 是免費的互動式 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 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶。
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
執行指令碼
# 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
清除資源
您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。
az group delete --name $resourceGroup1
az group delete --name $resourceGroup2
範例參考
此指令碼使用下列命令來建立資源群組、Web 應用程式、流量管理員設定檔和所有相關資源。 下表中的每個命令都會連結至命令特定的文件。
Command | 注意 |
---|---|
az group create | 建立用來存放所有資源的資源群組。 |
az appservice plan create | 建立 App Service 方案。 這就像是 Azure Web 應用程式的伺服器陣列。 |
az webapp web 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 指令碼範例。