你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

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

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

Diagram of Traffic Manager deployment environment using Azure PowerShell.

先决条件

如果还没有 Azure 订阅,请现在就创建一个免费帐户

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。

若要启动 Azure Cloud Shell,请执行以下操作:

选项 示例/链接
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 Screenshot that shows an example of Try It for Azure Cloud Shell.
转到 https://shell.azure.com 或选择启动 Cloud Shell 按钮可在浏览器中打开 Cloud Shell。 Button to launch Azure Cloud Shell.
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell,请执行以下操作:

  1. 启动 Cloud Shell。

  2. 选择代码块(或命令块)上的“复制”按钮以复制代码或命令。

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V,或在 macOS 上选择 Cmd+Shift+V 将代码或命令粘贴到 Cloud Shell 会话中。

  4. 选择“Enter”运行代码或命令。

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

创建资源组。

使用 New-AzResourceGroup 创建资源组。


# Variables
$Location1="EastUS"

# 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="EastUS"
$Location2="WestEurope"

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

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

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

$App1ResourceId=(New-AzWebApp -Name myWebAppEastUS -ResourceGroupName MyResourceGroup -Location $Location1 -AppServicePlan "myAppServicePlanEastUS").Id
$App2ResourceId=(New-AzWebApp -Name myWebAppWestEurope -ResourceGroupName MyResourceGroup -Location $Location2 -AppServicePlan "myAppServicePlanWestEurope").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.net

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

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

    注意

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

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

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

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

清理资源

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

Remove-AzResourceGroup -Name MyResourceGroup

后续步骤

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