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

此脚本将创建一个资源组、两个应用服务计划、两个 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 PowerShell 指南中的说明安装 Azure PowerShell,并运行 Connect-AzAccount -Environment AzureChinaCloud 创建与 Azure 的连接。If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

如果没有 Azure 订阅,请在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

示例脚本Sample script

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

# Variables for common values
$rgName1="MyResourceGroup1"
$rgName2="MyResourceGroup2"
$location1="chinaeast"
$location2="chinanorth"

# The values of the variables below must be unique (replace with your own names).
$webApp1="mywebapp$(Get-Random)"
$webApp2="mywebapp$(Get-Random)"
$webAppL1="MyWebAppL1"
$webAppL2="MyWebAppL2"

# Create a resource group in location one.
New-AzResourceGroup -Name $rgName1 -Location $location1

# Create a resource group in location two.
New-AzResourceGroup -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).

$appServicePlan = New-AzAppServicePlan -Name $webappl1 -ResourceGroupName $rgName1 `
  -Location $location1 -Tier Standard 

$web1 = New-AzWebApp -ResourceGroupName $rgname1 -Name $webApp1 -Location $location1 `
  -AppServicePlan $webappl1

# Configure GitHub deployment from your GitHub repo and deploy once.
$PropertiesObject = @{
    repoUrl = "$gitrepo";
    branch = "master";
    isManualIntegration = "true";
}

Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $rgname1 `
-ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webapp1/web `
-ApiVersion 2015-08-01 -Force

# Create a hosting plan and website and deploy it in location two (requires Standard 1 minimum SKU).

$appServicePlan = New-AzAppServicePlan -Name $webappl2 -ResourceGroupName $rgName2 `
  -Location $location2 -Tier Standard 

$web2 = New-AzWebApp -ResourceGroupName $rgname2 -Name $webApp2 `
  -Location $location2 -AppServicePlan $webappl2

$PropertiesObject = @{
    repoUrl = "$gitrepo";
    branch = "master";
    isManualIntegration = "true";
}

Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $rgname2 `
  -ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webapp2/web `
  -ApiVersion 2015-08-01 -Force

# Create a Traffic Manager profile.
$tm = New-AzTrafficManagerProfile -Name 'MyTrafficManagerProfile' -ResourceGroupName $rgname1 `
  -TrafficRoutingMethod Priority -RelativeDnsName $web1.Name -Ttl 60 `
  -MonitorProtocol HTTP -MonitorPort 80 -MonitorPath /

# Create an endpoint for the location one website deployment and set it as the priority target.
$endpoint = New-AzTrafficManagerEndpoint -Name 'MyEndPoint1' -ProfileName $tm.Name `
  -ResourceGroupName $rgname1 -Type AzureEndpoints -Priority 1 `
  -TargetResourceId $web1.Id -EndpointStatus Enabled

# Create an endpoint for the location two website deployment and set it as the secondary target.
$endpoint2 = New-AzTrafficManagerEndpoint -Name 'MyEndPoint2' -ProfileName $tm.Name `
  -ResourceGroupName $rgname1 -Type AzureEndpoints -Priority 2 `
  -TargetResourceId $web2.Id -EndpointStatus Enabled

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup1
Remove-AzResourceGroup -Name myResourceGroup2

脚本说明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.

CommandCommand 说明Notes
New-AzResourceGroupNew-AzResourceGroup 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
New-AzAppServicePlanNew-AzAppServicePlan 创建应用服务计划。Creates an App Service plan. 这与 Azure Web 应用的服务器场类似。This is like a server farm for your Azure web app.
New-AzWebAppNew-AzWebApp 创建应用服务计划中的 Azure Web 应用。Creates an Azure web app within the App Service plan.
Set-AzResourceSet-AzResource 创建应用服务计划中的 Azure Web 应用。Creates an Azure web app within the App Service plan.
New-AzTrafficManagerProfileNew-AzTrafficManagerProfile 创建 Azure 流量管理器配置文件。Creates an Azure Traffic Manager profile.
New-AzTrafficManagerEndpointNew-AzTrafficManagerEndpoint 将终结点添加到 Azure 流量管理器配置文件。Adds an endpoint to an Azure Traffic Manager Profile.

后续步骤Next steps

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

可在 Azure 网络概述文档中找到其他网络 PowerShell 脚本示例。Additional networking PowerShell script samples can be found in the Azure Networking Overview documentation.