使用 Azure PowerShell 的流量管理器子网替代Traffic Manager subnet override using Azure Powershell

使用流量管理器子网替代可以更改配置文件的路由方法。Traffic Manager subnet override allows you to alter the routing method of a profile. 添加替代后,会使用预定义的 IP 范围到终结点的映射,基于最终用户的 IP 地址来定向流量。The addition of an override will direct traffic based upon the end user's IP address with a predefined IP range to endpoint mapping.

子网替代的工作原理How subnet override works

将子网替代添加到流量管理器配置文件后,流量管理器会先检查最终用户的 IP 地址是否存在子网替代。When subnet overrides are added to a traffic manager profile, Traffic Manager will first check if there's a subnet override for the end user's IP address. 如果找到了一个替代,用户的 DNS 查询将定向到相应的终结点。If one is found, the user's DNS query will be directed to the corresponding endpoint. 如果找不到映射,流量管理器将回退到配置文件的原始路由方法。If a mapping isn't found, Traffic Manager will fall back to the profile's original routing method.

可将 IP 地址范围指定为 CIDR 范围(例如 1.2.3.0/24)或地址范围(例如 1.2.3.4-5.6.7.8)。The IP address ranges can be specified as either CIDR ranges (for example, 1.2.3.0/24) or as address ranges (for example, 1.2.3.4-5.6.7.8). 与每个终结点关联的 IP 范围对于该终结点必须是唯一的。The IP ranges associated with each endpoint must be unique to that endpoint. 不同终结点之间的 IP 范围出现任何重叠会导致流量管理器拒绝配置文件。Any overlap of IP ranges among different endpoints will cause the profile to be rejected by Traffic Manager.

有两种类型的路由配置文件支持子网替代:There are two types of routing profiles that support subnet overrides:

  • 地理 - 如果流量管理器找到了 DNS 查询的 IP 地址的子网替代,它会将该查询路由到终结点,而不管该终结点的运行状况如何。Geographic - If Traffic Manager finds a subnet override for the DNS query's IP address, it will route the query to the endpoint whatever the health of the endpoint is.
  • 性能 - 如果流量管理器找到了 DNS 查询的 IP 地址的子网替代,它只会将流量路由到正常的终结点。Performance - If Traffic Manager finds a subnet override for the DNS query's IP address, it will only route the traffic to the endpoint if it's healthy. 如果子网替代终结点不正常,流量管理器将回退到性能路由试探法。Traffic Manager will fall back to the performance routing heuristic if the subnet override endpoint isn't healthy.

创建流量管理器子网替代Create a Traffic Manager subnet override

若要创建流量管理器子网替代,可以使用 Azure PowerShell 将替代子网添加到流量管理器终结点。To create a Traffic Manager subnet override, you can use Azure PowerShell to add the subnets for the override to the Traffic Manager endpoint.

Azure PowerShellAzure PowerShell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可以通过从计算机运行 PowerShell 来运行命令。You can run the commands by running PowerShell from your computer.
如果在计算机上运行 PowerShell,则需要 Azure PowerShell 模块 1.0.0 或更高版本。If you run PowerShell from your computer, you need the Azure PowerShell module, 1.0.0 or later. 可以运行 Get-Module -ListAvailable Az 来查找已安装的版本。You can run Get-Module -ListAvailable Az to find the installed version. 如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块If you need to install or 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 sign in to Azure.

  1. 检索流量管理器终结点:Retrieve the Traffic Manager endpoint:

    若要启用子网替代,请使用 Get-AzTrafficManagerEndpoint 检索要将替代添加到的终结点,并将其存储在变量中。To enable the subnet override, retrieve the endpoint you wish to add the override to and store it in a variable using Get-AzTrafficManagerEndpoint.

    请将 Name、ProfileName 和 ResourceGroupName 替换为要更改的终结点的值。Replace the Name, ProfileName, and ResourceGroupName with the values of the endpoint that you're changing.

    
    $TrafficManagerEndpoint = Get-AzTrafficManagerEndpoint -Name "contoso" -ProfileName "ContosoProfile" -ResourceGroupName "ResourceGroup" -Type AzureEndpoints
    
    
  2. 将 IP 地址范围添加到终结点:Add the IP address range to the endpoint:

    若要将 IP 地址范围添加到终结点,请使用 Add-AzTrafficManagerIpAddressRange 添加范围。To add the IP address range to the endpoint, you'll use Add-AzTrafficManagerIpAddressRange to add the range.

    
    ### 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 更新终结点。Once the ranges are added, use Set-AzTrafficManagerEndpoint to update the endpoint.

    
    Set-AzTrafficManagerEndpoint -TrafficManagerEndpoint $TrafficManagerEndpoint
    
    

可以使用 Remove-AzTrafficManagerIpAddressRange 删除 IP 地址范围。Removal of the IP address range can be completed by using Remove-AzTrafficManagerIpAddressRange.

  1. 检索流量管理器终结点:Retrieve the Traffic Manager endpoint:

    若要启用子网替代,请使用 Get-AzTrafficManagerEndpoint 检索要将替代添加到的终结点,并将其存储在变量中。To enable the subnet override, retrieve the endpoint you wish to add the override to and store it in a variable using Get-AzTrafficManagerEndpoint.

    请将 Name、ProfileName 和 ResourceGroupName 替换为要更改的终结点的值。Replace the Name, ProfileName, and ResourceGroupName with the values of the endpoint that you're changing.

    
    $TrafficManagerEndpoint = Get-AzTrafficManagerEndpoint -Name "contoso" -ProfileName "ContosoProfile" -ResourceGroupName "ResourceGroup" -Type AzureEndpoints
    
    
  2. 从终结点中删除 IP 地址范围:Remove the IP address range from the endpoint:

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

    删除范围后,使用 Set-AzTrafficManagerEndpoint 更新终结点。Once the ranges are removed, use Set-AzTrafficManagerEndpoint to update the endpoint.

    
    Set-AzTrafficManagerEndpoint -TrafficManagerEndpoint $TrafficManagerEndpoint
    
    

后续步骤Next steps

详细了解流量管理器流量路由方法Learn more about Traffic Manager traffic routing methods.

了解子网流量路由方法Learn about the Subnet traffic-routing method