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

快速入门:使用 Azure PowerShell 创建 Azure Front Door(经典版)服务

重要

Azure Front Door(经典版)将于 2027 年 3 月 31 日停用。 为了避免任何服务中断,请务必在 2027 年 3 月之前将 Azure Front Door(经典版)配置文件迁移到 Azure Front Door 标准层或高级层。 有关详细信息,请参阅 Azure Front Door(经典版)停用

通过使用 Azure PowerShell 创建高度可用且高性能的全局 Web 应用程序,开始使用 Azure Front Door(经典版)。

Azure Front Door 会将 Web 流量定向到后端池中的特定资源。 你已定义前端域,请将资源添加到后端池,并创建路由规则。 本文使用一个后端池的简单配置,其中包含两个 Web 应用资源和一个使用默认路径匹配“/*”的路由规则。

使用 PowerShell 的 Front Door 环境示意图。

先决条件

  • 具有活动订阅的 Azure 帐户。 免费创建帐户
  • 本地安装的 Azure PowerShell 或 Azure Cloud Shell

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

Azure Cloud Shell

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

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

选项 示例/链接
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 显示 Azure Cloud Shell 的“试用”示例的屏幕截图。
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。 用于启动 Azure Cloud Shell 的按钮。
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 显示 Azure 门户中的 Cloud Shell 按钮的屏幕截图

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

  1. 启动 Cloud Shell。

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

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

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

创建资源组

在 Azure 中,可将相关的资源分配到资源组。 可以使用现有资源组,也可以创建新组。

使用 New-AzResourceGroup 创建资源组:

New-AzResourceGroup -Name myResourceGroupFD -Location centralus

创建 Web 应用的两个实例

本快速入门要求 Web 应用程序的两个实例在不同的 Azure 区域中运行。 这两个 Web 应用程序实例在“主动/主动”模式下运行,因此其中的任何一个实例都可以接收流量。 此配置不同于“主动/备用”配置,在后一种配置中,只有一个实例充当故障转移节点。

如果还没有 Web 应用,请使用以下脚本设置两个示例 Web 应用。

# Create first web app in Central US region.
$webapp1 = New-AzWebApp `
-Name "WebAppContoso-1" `
-Location centralus `
-ResourceGroupName myResourceGroupFD `
-AppServicePlan myAppServicePlanCentralUS

# Create second web app in South Central US region.
$webapp2 = New-AzWebApp `
-Name "WebAppContoso-2" `
-Location southcentralus `
-ResourceGroupName myResourceGroupFD `
-AppServicePlan myAppServicePlanEastUS

创建 Front Door

本部分详细介绍如何创建和配置 Front Door 的以下组件:

  • 前端对象包含 Front Door 默认域。
  • 后端池是一组等效后端,Front Door 会将客户端请求负载均衡到这些后端。
  • 路由规则会将前端主机和匹配的 URL 路径模式映射到特定的后端池。

创建前端对象

前端对象为 Front Door 配置主机名。 默认情况下,主机名的后缀为“*.azurefd.net”。

# Create a unique name
$fdname = "contoso-frontend-$(Get-Random)"

#Create the frontend object
$FrontendEndObject = New-AzFrontDoorFrontendEndpointObject `
-Name "frontendEndpoint1" `
-HostName $fdname".azurefd.net"

创建后端池

后端池包含在本快速入门教程开头创建的两个 Web 应用。 此步骤中定义的运行状况探测和负载均衡设置使用默认值。

# Create backend objects that points to the hostname of the web apps
$backendObject1 = New-AzFrontDoorBackendObject `
-Address $webapp1.DefaultHostName
$backendObject2 = New-AzFrontDoorBackendObject `
-Address $webapp2.DefaultHostName

# Create a health probe object
$HealthProbeObject = New-AzFrontDoorHealthProbeSettingObject `
-Name "HealthProbeSetting"

# Create the load balancing setting object
$LoadBalancingSettingObject = New-AzFrontDoorLoadBalancingSettingObject `
-Name "Loadbalancingsetting" `
-SampleSize "4" `
-SuccessfulSamplesRequired "2" `
-AdditionalLatencyInMilliseconds "0"

# Create a backend pool using the backend objects, health probe, and load balancing settings
$BackendPoolObject = New-AzFrontDoorBackendPoolObject `
-Name "myBackendPool" `
-FrontDoorName $fdname `
-ResourceGroupName myResourceGroupFD `
-Backend $backendObject1,$backendObject2 `
-HealthProbeSettingsName "HealthProbeSetting" `
-LoadBalancingSettingsName "Loadbalancingsetting"

创建路由规则

路由规则会将后端池映射到前端域,并将默认路径匹配值设置为“/*”。

# Create a routing rule mapping the frontend host to the backend pool
$RoutingRuleObject = New-AzFrontDoorRoutingRuleObject `
-Name LocationRule `
-FrontDoorName $fdname `
-ResourceGroupName myResourceGroupFD `
-FrontendEndpointName "frontendEndpoint1" `
-BackendPoolName "myBackendPool" `
-PatternToMatch "/*"

创建 Front Door

现在,你已创建了必需的对象,接下来创建 Front Door:

# Creates the Front Door
New-AzFrontDoor `
-Name $fdname `
-ResourceGroupName myResourceGroupFD `
-RoutingRule $RoutingRuleObject `
-BackendPool $BackendPoolObject `
-FrontendEndpoint $FrontendEndObject `
-LoadBalancingSetting $LoadBalancingSettingObject `
-HealthProbeSetting $HealthProbeObject

部署成功后,可以按照下一部分中的步骤对其进行测试。

测试 Front Door

运行以下命令,获取 Front Door 的主机名。

# Gets Front Door in resource group and output the hostname of the frontend domain.
$fd = Get-AzFrontDoor -ResourceGroupName myResourceGroupFD
$fd.FrontendEndpoints[0].Hostname

打开 Web 浏览器并输入从命令获取的主机名。 Azure Front Door 会将你的请求定向到后端资源之一。

Front Door 测试页

清理资源

如果不再需要使用 Front Door 创建的资源,请删除资源组。 删除资源组时,也会删除 Front Door 及其所有相关资源。

若要删除资源组,请调用 Remove-AzResourceGroup cmdlet:

Remove-AzResourceGroup -Name myResourceGroupFD

后续步骤

在本快速入门中,我们创建了:

  • Front Door
  • 两个 Web 应用

若要了解如何将自定义域添加到 Front Door,请继续学习 Front Door 教程。