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

使用 Azure PowerShell 的流量管理器子网替代

使用流量管理器子网替代可以更改配置文件的路由方法。 添加替代后,会使用预定义的 IP 范围到终结点的映射,基于最终用户的 IP 地址来定向流量。

子网替代的工作原理

将子网替代添加到流量管理器配置文件后,流量管理器会先检查最终用户的 IP 地址是否存在子网替代。 如果找到了一个替代,用户的 DNS 查询将被定向到相应的终结点。 如果找不到映射,流量管理器将回退到配置文件的原始路由方法。

可将 IP 地址范围指定为 CIDR 范围(例如 1.2.3.0/24)或地址范围(例如 1.2.3.4-5.6.7.8)。 与每个终结点关联的 IP 范围对于该终结点必须是唯一的。 不同终结点之间的 IP 范围出现任何重叠会导致流量管理器拒绝配置文件。

有两种类型的路由配置文件支持子网替代:

  • 地理 - 如果流量管理器找到了 DNS 查询的 IP 地址的子网替代,它会将该查询路由到终结点,而不管该终结点的运行状况如何。
  • 性能 - 如果流量管理器找到了 DNS 查询的 IP 地址的子网替代,它只会将流量路由到正常的终结点。 如果子网替代终结点不正常,流量管理器将回退到性能路由试探法。

先决条件

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 的连接。

创建流量管理器子网替代

若要创建流量管理器子网替代,可以使用 Azure PowerShell 将替代子网添加到流量管理器终结点。

向终结点添加 IP 地址范围

  1. 检索流量管理器终结点:

    若要启用子网替代,请使用 Get-AzTrafficManagerEndpoint 检索要将替代添加到的终结点,并将其存储在变量中。

    请将 Name、ProfileName 和 ResourceGroupName 替换为要更改的终结点的值。 在此示例中,我们将使用终结点名称 myAppServicePlan 和配置文件名称 myTrafficManagerProfile。

    
    $TrafficManagerEndpoint = Get-AzTrafficManagerEndpoint -Name "myAppServicePlan" -ProfileName "myTrafficManagerProfile" -ResourceGroupName "MyResourceGroup" -Type AzureEndpoints
    
    
  2. 将 IP 地址范围添加到终结点:

    若要将 IP 地址范围添加到终结点,请使用 Add-AzTrafficManagerIpAddressRange 添加范围。

    
    ### Add a range of IPs ###
    Add-AzTrafficManagerIPAddressRange -TrafficManagerEndpoint $TrafficManagerEndpoint -First "1.2.3.4" -Last "5.6.7.8"
    
    ### Add a subnet ###
    Add-AzTrafficManagerIPAddressRange -TrafficManagerEndpoint $TrafficManagerEndpoint -First "9.10.11.0" -Scope 24
    
    ### Add a range of IPs with a subnet ###
    Add-AzTrafficManagerIPAddressRange -TrafficManagerEndpoint $TrafficManagerEndpoint -First "12.13.14.0" -Last "12.13.14.31" -Scope 27
    
    

更新终结点

添加范围后,使用 Set-AzTrafficManagerEndpoint 更新终结点。


Set-AzTrafficManagerEndpoint -TrafficManagerEndpoint $TrafficManagerEndpoint

从终结点中删除 IP 地址范围

  1. 检索流量管理器终结点:

    若要启用子网替代,请使用 Get-AzTrafficManagerEndpoint 检索要将替代添加到的终结点,并将其存储在变量中。

    请将 Name、ProfileName 和 ResourceGroupName 替换为要更改的终结点的值。

    
    $TrafficManagerEndpoint = Get-AzTrafficManagerEndpoint -Name "myAppServicePlan" -ProfileName "myTrafficManagerProfile" -ResourceGroupName "MyResourceGroup" -Type AzureEndpoints
    
    
  2. 从终结点中删除 IP 地址范围:

    
    ### Remove a range of IPs ###
    Remove-AzTrafficManagerIpAddressRange -TrafficManagerEndpoint $TrafficManagerEndpoint -First "1.2.3.4" 
    
    ### Remove a subnet ###
    Remove-AzTrafficManagerIpAddressRange -TrafficManagerEndpoint $TrafficManagerEndpoint -First "9.10.11.0" 
    
    ### Remove a range of IPs with a subnet ###
    Remove-AzTrafficManagerIpAddressRange -TrafficManagerEndpoint $TrafficManagerEndpoint -First "12.13.14.0" 
    
    

更新终结点

删除范围后,使用 Set-AzTrafficManagerEndpoint 更新终结点。

Set-AzTrafficManagerEndpoint -TrafficManagerEndpoint $TrafficManagerEndpoint

后续步骤

详细了解流量管理器流量路由方法

了解子网流量路由方法