快速入门:使用 Azure PowerShell 创建流量管理器配置文件以实现 Web 应用程序的高可用性

本快速入门介绍如何创建流量管理器配置文件,以便实现 Web 应用程序的高度可用性。

在本快速入门中,我们将创建 Web 应用程序的两个实例。 每个实例在不同的 Azure 区域运行。 需根据终结点优先级创建流量管理器配置文件。 此配置文件将用户流量定向到运行 Web 应用程序的主站点。 流量管理器持续监视 Web 应用程序。 如果主站点不可用,它会提供目标为备份站点的自动故障转移。

Diagram of Traffic Manager deployment environment using Azure PowerShell.

先决条件

如果没有 Azure 订阅,请立即创建一个试用版订阅

如果选择在本地安装并使用 PowerShell,则本文需要 Azure PowerShell 模块 5.4.1 或更高版本。 运行 Get-Module -ListAvailable Az 查找已安装的版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。

创建资源组。

使用 New-AzResourceGroup 创建资源组。


# Variables
$Location1="ChinaEast"

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

创建流量管理器配置文件

使用 New-AzTrafficManagerProfile 创建流量管理器配置文件,以根据终结点优先级定向用户流量。


# 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 应用

本快速入门需要两个部署在两个不同的 Azure 区域(中国北部和中国东部)的 Web 应用程序实例。 每个都可以充当流量管理器的主终结点和故障转移终结点。

创建 Web 应用服务计划

使用 New-AzAppServicePlan 为要部署在两个不同 Azure 区域的两个 Web 应用程序实例创建 Web 应用服务计划。


# Variables
$Location1="ChinaEast"
$Location2="ChinaNorth"

# Create an App service plan
New-AzAppservicePlan -Name "myAppServicePlanChinaEast$Random" -ResourceGroupName MyResourceGroup -Location $Location1 -Tier Standard
New-AzAppservicePlan -Name "myAppServicePlanChinaNorth$Random" -ResourceGroupName MyResourceGroup -Location $Location2 -Tier Standard

在应用服务计划中创建 Web 应用

在应用服务计划中使用 New-AzWebApp 在“中国东部”和“中国北部”Azure 区域中创建 Web 应用程序的两个实例 。

$App1ResourceId=(New-AzWebApp -Name myWebAppChinaEast -ResourceGroupName MyResourceGroup -Location $Location1 -AppServicePlan "myAppServicePlanChinaEast").Id
$App2ResourceId=(New-AzWebApp -Name myWebAppChinaNorth -ResourceGroupName MyResourceGroup -Location $Location2 -AppServicePlan "myAppServicePlanChinaNorth").Id

添加流量管理器终结点

使用 New-AzTrafficManagerEndpoint 将两个 Web 应用作为流量管理器终结点添加到流量管理器配置文件,如下所示:

  • 将“中国东部”Azure 区域中的 Web 应用添加为主要终结点,以路由所有用户流量。
  • 将“中国北部”Azure 区域中的 Web 应用添加为故障转移终结点。 当主终结点不可用时,流量自动路由到故障转移终结点。
New-AzTrafficManagerEndpoint -Name "myPrimaryEndpoint" `
-ResourceGroupName MyResourceGroup `
-ProfileName "$mytrafficmanagerprofile" `
-Type AzureEndpoints `
-TargetResourceId $App1ResourceId `
-EndpointStatus "Enabled"

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

测试流量管理器配置文件

在此部分,需检查流量管理器配置文件的域名。 此外还需将主终结点配置为不可用。 最后可以看到该 Web 应用仍然可用。 这是因为流量管理器将流量发送到故障转移终结点。

确定 DNS 名称

使用 Get-AzTrafficManagerProfile 确定流量管理器配置文件的 DNS 名称。

Get-AzTrafficManagerProfile -Name $mytrafficmanagerprofile `
-ResourceGroupName MyResourceGroup

复制 RelativeDnsName 值。 流量管理器配置文件的 DNS 名称为“http://<relativednsname>.trafficmanager.cn”。

查看正在运行的流量管理器

  1. 在 Web 浏览器中输入流量管理器配置文件的 DNS 名称 (http://<relativednsname>.trafficmanager.cn),以查看 Web 应用的默认网站。

    注意

    在本快速入门方案中,所有请求都路由到主终结点。 它设置为“优先级 1”。

  2. 若要查看流量管理器故障转移如何进行,请使用 Disable-AzTrafficManagerEndpoint 禁用主要站点。

    Disable-AzTrafficManagerEndpoint -Name "myPrimaryEndpoint" `
    -Type AzureEndpoints `
    -ProfileName $mytrafficmanagerprofile `
    -ResourceGroupName MyResourceGroup `
    -Force
    
  3. 复制流量管理器配置文件的 DNS 名称 (http://<relativednsname>.trafficmanager.cn),以在新的 Web 浏览器会话中查看该网站。

  4. 验证 Web 应用是否仍然可用。

清理资源

完成后,请使用 Remove-AzResourceGroup 删除资源组、Web 应用程序和所有相关资源。

Remove-AzResourceGroup -Name MyResourceGroup

后续步骤

在本快速入门中,我们创建了一个可为 Web 应用程序提供高可用性的流量管理器配置文件。 若要详细了解如何路由流量,请继续学习流量管理器教程。