使用 Azure CLI 为实现应用程序的高可用性路由流量Route traffic for high availability of applications using Azure CLI

此脚本将创建一个资源组、两个应用服务计划、两个 Web 应用、一个流量管理器配置文件和两个流量管理器终结点。This script creates a resource group, two app service plans, two web apps, a traffic manager profile, and two traffic manager endpoints. 流量管理器将流量引导到一个区域(作为主要区域)中的应用程序;主要区域中的应用程序不可用时,引导到次要区域。Traffic Manager directs traffic to the application in one region as the primary region, and to the secondary region when the application in the primary region is unavailable. 执行脚本前,必须将 MyWebApp、MyWebAppL1 和 MyWebAppL2 值更改为 Azure 内的唯一值。Before executing the script, you must change the MyWebApp, MyWebAppL1 and MyWebAppL2 values to unique values across Azure. 运行脚本后,可以使用 URL mywebapp.trafficmanager.cn 访问主要区域中的应用。After running the script, you can access the app in the primary region with the URL mywebapp.trafficmanager.cn.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

#!/bin/bash

RgName1="MyResourceGroup1"
RgName2="MyResourceGroup2"
Location1="chinaeast"
Location2="chinanorth"

# The values of the variables below must be unique (replace with your own names).
WebApp="MyWebApp"
WebAppL1="MyWebAppL1"
WebAppL2="MyWebAppL2"

# Create a resource group in location one.
az group create \
  --name $RgName1 \
  --location $Location1

# Create a resource group in location two.
az group create \
  --name $RgName2 \
  --location $Location2

# 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).
az appservice plan create \
  --name $WebAppL1 \
  --resource-group $RgName1 \
  --sku S1
az webapp create \
  --name $WebAppL1 \
  --resource-group $RgName1 \
  --plan $WebAppL1
az webapp deployment source config \
  --name $WebAppL1 \
  --resource-group $RgName1 \
  --repo-url $gitrepo \
  --branch master \
  --manual-integration

# Create a hosting plan and website and deploy it in chinanorth (requires Standard 1 minimum SKU).
az appservice plan create \
  --name $WebAppL2 \
  --resource-group $RgName2 \
  --sku S1
az webapp create \
  --name $WebAppL2 \
  --resource-group $RgName2 \
  --plan $WebAppL2
az webapp deployment source config \
  --name $WebAppL2 \
  --resource-group $RgName2 \
  --repo-url $gitrepo \
  --branch master --manual-integration

# Create a Traffic Manager profile.
az network traffic-manager profile create \
  --name MyTrafficManagerProfile \
  --resource-group $RgName1 \
  --routing-method Priority \
  --unique-dns-name $WebApp

# Create an endpoint for the location one website deployment and set it as the priority target.
L1Id=$(az webapp show \
  --resource-group $RgName1 \
  --name $WebAppL1 \
  --query id \
  --out tsv)
az network traffic-manager endpoint create \
  --name MyEndPoint1 \
  --profile-name MyTrafficManagerProfile \
  --resource-group $RgName1 \
  --type azureEndpoints \
  --priority 1 \
  --target-resource-id $L1Id

# Create an endpoint for the location two website deployment and set it as the secondary target.
L2Id=$(az webapp show \
  --resource-group $RgName2 \
  --name $WebAppL2 \
  --query id --out tsv)
az network traffic-manager endpoint create \
  --name MyEndPoint2 \
  --profile-name MyTrafficManagerProfile \
  --resource-group $RgName1 \
  --type azureEndpoints \
  --priority 2 \
  --target-resource-id $L2Id

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组、应用服务应用以及所有相关资源。After the script sample has been run, the follow command can be used to remove the resource group, App Service app, and all related resources.

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

脚本说明Script explanation

此脚本使用以下命令创建资源组、Web 应用、流量管理器配置文件和所有相关资源。This script uses the following commands to create a resource group, web app, traffic manager profile, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az appservice plan createaz appservice plan create 创建应用服务计划。Creates an App Service plan. 这与 Azure Web 应用的服务器场类似。This is like a server farm for your Azure web app.
az webapp web createaz webapp web create 创建应用服务计划中的 Azure Web 应用。Creates an Azure web app within the App Service plan.
az network traffic-manager profile createaz network traffic-manager profile create 创建 Azure 流量管理器配置文件。Creates an Azure Traffic Manager profile.
az network traffic-manager endpoint createaz network traffic-manager endpoint create 将终结点添加到 Azure 流量管理器配置文件。Adds an endpoint to an Azure Traffic Manager Profile.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可在 Azure 网络文档中找到其他应用服务 CLI 脚本示例。Additional App Service CLI script samples can be found in the Azure Networking documentation.