快速入门:使用 Azure PowerShell 创建流量管理器配置文件以实现 Web 应用程序的高可用性Quickstart: Create a Traffic Manager profile for a highly available web application using Azure PowerShell

本快速入门介绍如何创建流量管理器配置文件,以便实现 Web 应用程序的高度可用性。This quickstart describes how to create a Traffic Manager profile that delivers high availability for your web application.

在本快速入门中,我们将创建 Web 应用程序的两个实例。In this quickstart, you'll create two instances of a web application. 每个实例在不同的 Azure 区域运行。Each of them is running in a different Azure region. 需根据终结点优先级创建流量管理器配置文件。You'll create a Traffic Manager profile based on endpoint priority. 此配置文件将用户流量定向到运行 Web 应用程序的主站点。The profile directs user traffic to the primary site running the web application. 流量管理器持续监视 Web 应用程序。Traffic Manager continuously monitors the web application. 如果主站点不可用,它会提供目标为备份站点的自动故障转移。If the primary site is unavailable, it provides automatic failover to the backup site.

如果还没有 Azure 订阅,请现在就创建一个试用帐户If you don't have an Azure subscription, create a trial account now.

如果选择在本地安装并使用 PowerShell,则本文需要 Azure PowerShell 模块 5.4.1 或更高版本。If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 5.4.1 or later. 运行 Get-Module -ListAvailable Az 查找已安装的版本。Run Get-Module -ListAvailable Az to find the installed version. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 来创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

创建资源组。Create a Resource Group

使用 New-AzResourceGroup 创建资源组。Create a resource group using New-AzResourceGroup.


# Variables
$Location1="ChinaNorth"

# Create a Resource Group
New-AzResourceGroup -Name MyResourceGroup -Location $Location1

创建流量管理器配置文件Create a Traffic Manager profile

使用 New-AzTrafficManagerProfile 创建流量管理器配置文件,以根据终结点优先级定向用户流量。Create a Traffic Manager profile using New-AzTrafficManagerProfile that directs user traffic based on endpoint priority.


# Generates a random value
$Random=(New-Guid).ToString().Substring(0,8)
$mytrafficmanagerprofile="mytrafficmanagerprofile$Random"

New-AzTrafficManagerProfile `
-Name $mytrafficmanagerprofile `
-ResourceGroupName MyResourceGroup `
-TrafficRoutingMethod Priority `
-MonitorPath '/' `
-MonitorProtocol "HTTP" `
-RelativeDnsName $mytrafficmanagerprofile `
-Ttl 30 `
-MonitorPort 80

创建 Web 应用Create Web Apps

本快速入门需要两个部署在两个不同的 Azure 区域(中国北部和中国东部)的 Web 应用程序实例。 For this quickstart, you'll need two instances of a web application deployed in two different Azure regions (China North and China East). 每个都可以充当流量管理器的主终结点和故障转移终结点。Each will serve as primary and failover endpoints for Traffic Manager.

创建 Web 应用服务计划Create Web App Service plans

使用 New-AzAppServicePlan 为要部署在两个不同 Azure 区域中的两个 Web 应用程序实例创建 Web 应用服务计划。Create Web App service plans using New-AzAppServicePlan for the two instances of the web application that you will deploy in two different Azure regions.


# Variables
$App1Name="AppServiceTM1$Random"
$App2Name="AppServiceTM2$Random"
$Location1="ChinaNorth"
$Location2="ChinaEast"

# Create an App service plan
New-AzAppservicePlan -Name "$App1Name-Plan" -ResourceGroupName MyResourceGroup -Location $Location1 -Tier Standard
New-AzAppservicePlan -Name "$App2Name-Plan" -ResourceGroupName MyResourceGroup -Location $Location2 -Tier Standard

在应用服务计划中创建 Web 应用Create a Web App in the App Service Plan

在应用服务计划中使用 New-AzWebApp 在“中国北部”和“中国东部”Azure 区域中创建 Web 应用程序的两个实例。 Create two instances the web application using New-AzWebApp in the App Service plans in the China North and China East Azure regions.

$App1ResourceId=(New-AzWebApp -Name $App1Name -ResourceGroupName MyResourceGroup -Location $Location1 -AppServicePlan "$App1Name-Plan").Id
$App2ResourceId=(New-AzWebApp -Name $App2Name -ResourceGroupName MyResourceGroup -Location $Location2 -AppServicePlan "$App2Name-Plan").Id

添加流量管理器终结点Add Traffic Manager endpoints

使用 New-AzTrafficManagerEndpoint 将两个 Web 应用作为流量管理器终结点添加到流量管理器配置文件,如下所示:Add the two Web Apps as Traffic Manager endpoints using New-AzTrafficManagerEndpoint to the Traffic Manager profile as follows:

  • 将“中国北部”Azure 区域中的 Web 应用添加为主要终结点,以路由所有用户流量。 Add the Web App located in the China North Azure region as the primary endpoint to route all the user traffic.
  • 将“中国东部”Azure 区域中的 Web 应用添加为故障转移终结点。 Add the Web App located in the China East Azure region as the failover endpoint. 当主终结点不可用时,流量自动路由到故障转移终结点。When the primary endpoint is unavailable, traffic automatically routes to the failover endpoint.
New-AzTrafficManagerEndpoint -Name "$App1Name-$Location1" `
-ResourceGroupName MyResourceGroup `
-ProfileName "$mytrafficmanagerprofile" `
-Type AzureEndpoints `
-TargetResourceId $App1ResourceId `
-EndpointStatus "Enabled"

New-AzTrafficManagerEndpoint -Name "$App2Name-$Location2" `
-ResourceGroupName MyResourceGroup `
-ProfileName "$mytrafficmanagerprofile" `
-Type AzureEndpoints `
-TargetResourceId $App2ResourceId `
-EndpointStatus "Enabled"

测试流量管理器配置文件Test Traffic Manager profile

在此部分,需检查流量管理器配置文件的域名。In this section, you'll check the domain name of your Traffic Manager profile. 此外还需将主终结点配置为不可用。You'll also configure the primary endpoint to be unavailable. 最后可以看到该 Web 应用仍然可用。Finally, you get to see that the web app is still available. 这是因为流量管理器将流量发送到故障转移终结点。It's because Traffic Manager sends the traffic to the failover endpoint.

确定 DNS 名称Determine the DNS name

使用 Get-AzTrafficManagerProfile 确定流量管理器配置文件的 DNS 名称。Determine the DNS name of the Traffic Manager profile using Get-AzTrafficManagerProfile.

Get-AzTrafficManagerProfile -Name $mytrafficmanagerprofile `
-ResourceGroupName MyResourceGroup

复制 RelativeDnsName 值。Copy the RelativeDnsName value. 流量管理器配置文件的 DNS 名称为“http://<* relativednsname *>.trafficmanager.cn” ** 。The DNS name of your Traffic Manager profile is http://< relativednsname >.trafficmanager.cn.

查看正在运行的流量管理器View Traffic Manager in action

  1. 在 Web 浏览器中输入流量管理器配置文件的 DNS 名称 (http://<* relativednsname *>.trafficmanager.cn ** ),以查看 Web 应用的默认网站。In a web browser, enter the DNS name of your Traffic Manager profile (http://< relativednsname >.trafficmanager.cn) to view your Web App's default website.

    Note

    在本快速入门方案中,所有请求都路由到主终结点。In this quickstart scenario, all requests route to the primary endpoint. 它设置为“优先级 1”。 It is set to Priority 1.

  2. 若要查看流量管理器故障转移如何进行,请使用 Disable-AzTrafficManagerEndpoint 禁用主要站点。To view Traffic Manager failover in action, disable your primary site using Disable-AzTrafficManagerEndpoint.

    Disable-AzTrafficManagerEndpoint -Name $App1Name-$Location1 `
    -Type AzureEndpoints `
    -ProfileName $mytrafficmanagerprofile `
    -ResourceGroupName MyResourceGroup `
    -Force
    
  3. 复制流量管理器配置文件的 DNS 名称 (http://<* relativednsname *>.trafficmanager.cn ** ),以在新的 Web 浏览器会话中查看该网站。Copy the DNS name of your Traffic Manager profile (http://< relativednsname >.trafficmanager.cn) to view the website in a new web browser session.

  4. 验证 Web 应用是否仍然可用。Verify that the web app is still available.

清理资源Clean up resources

完成后,请使用 Remove-AzResourceGroup 删除资源组、Web 应用程序和所有相关资源。When you're done, delete the resource groups, web applications, and all related resources using Remove-AzResourceGroup.

Remove-AzResourceGroup -Name MyResourceGroup

后续步骤Next steps

在本快速入门中,我们创建了一个可为 Web 应用程序提供高可用性的流量管理器配置文件。In this quickstart, you created a Traffic Manager profile that provides high availability for your web application. 若要详细了解如何路由流量,请继续学习流量管理器教程。To learn more about routing traffic, continue to the Traffic Manager tutorials.