使用 Azure CLI 为实现应用程序的高可用性路由流量

此脚本将创建一个资源组、两个应用服务计划、两个 Web 应用、一个流量管理器配置文件和两个流量管理器终结点。 流量管理器将流量引导到一个区域(作为主要区域)中的应用程序;主要区域中的应用程序不可用时,引导到次要区域。 执行脚本前,必须将 MyWebApp、MyWebAppL1 和 MyWebAppL2 值更改为 Azure 内的唯一值。 运行脚本后,可以使用 URL mywebapp.trafficmanager.cn 访问主要区域中的应用。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

先决条件

  • 如需在本地运行 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

使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

az cloud set -n AzureChinaCloud
az login

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="chinaeast"
location2="chinanorth"
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 chinaeast (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 应用、流量管理器配置文件和所有相关资源。 表中的每条命令均链接到特定于命令的文档。

命令 说明
az group create 创建用于存储所有资源的资源组。
az appservice plan create 创建应用服务计划。 这与 Azure Web 应用的服务器场类似。
az webapp web create 创建应用服务计划中的 Azure Web 应用。
az network traffic-manager profile create 创建 Azure 流量管理器配置文件。
az network traffic-manager endpoint create 将终结点添加到 Azure 流量管理器配置文件。

后续步骤

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可在 Azure 网络文档中找到其他应用服务 CLI 脚本示例。