使用 Azure CLI 缩放具有高可用性体系结构的全球应用服务应用Scale an App Service app worldwide with a high-availability architecture using Azure CLI

此示例脚本将创建一个资源组、两个应用服务计划、两个应用、一个流量管理器配置文件和两个流量管理器终结点。This sample script creates a resource group, two App Service plans, two apps, a traffic manager profile, and two traffic manager endpoints. 完成本练习后,会获得一个高可用性体系结构,该体系结构基于最低网络延迟提供应用的全局可用性。Once the exercise is complete, you have a high-available architecture, which provides global availability of your app based on the lowest network latency.

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

如果选择在本地安装并使用 CLI,则需使用 Azure CLI 2.0 或更高版本。If you choose to install and use the CLI locally, you need Azure CLI version 2.0 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

#/bin/bash

# Variables
trafficManagerDnsName="myDnsName$random"
app1Name="AppServiceTM1$random"
app2Name="AppServiceTM2$random"
location1="ChinaNorth"
location2="ChinaEast"

# Create a Resource Group
az group create --name myResourceGroup --location $location1

# Create a Traffic Manager Profile
az network traffic-manager profile create --name $trafficManagerDnsName-tmp --resource-group myResourceGroup --routing-method Performance --unique-dns-name $trafficManagerDnsName

# Create App Service Plans in two Regions
az appservice plan create --name $app1Name-Plan --resource-group myResourceGroup --location $location1 --sku S1
az appservice plan create --name $app2Name-Plan --resource-group myResourceGroup --location $location2 --sku S1

# Add a Web App to each App Service Plan
site1=$(az webapp create --name $app1Name --plan $app1Name-Plan --resource-group myResourceGroup --query id --output tsv)
site2=$(az webapp create --name $app2Name --plan $app2Name-Plan --resource-group myResourceGroup --query id --output tsv)

# Assign each Web App as an Endpoint for high-availabilty
az network traffic-manager endpoint create -n $app1Name-$location1 --profile-name $trafficManagerDnsName-tmp -g myResourceGroup --type azureEndpoints --target-resource-id $site1
az network traffic-manager endpoint create -n $app2Name-$location2 --profile-name $trafficManagerDnsName-tmp -g myResourceGroup --type azureEndpoints --target-resource-id $site2

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

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

命令Command 注释Notes
az group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az appservice plan create 创建应用服务计划。Creates an App Service plan.
az webapp create 创建应用服务应用。Creates an App Service app.
az network traffic-manager profile create 创建 Azure 流量管理器配置文件。Creates an Azure Traffic Manager profile.
az 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 App Service documentation.