使用流量管理器,基于用户子网,将流量定向到特定终结点Direct traffic to specific endpoints based on user subnet using Traffic Manager

本文介绍如何配置子网流量路由方法。This article describes how to configure the subnet traffic-routing method. 借助子网流量路由方法,可以将一组 IP 地址范围映射到特定终结点,当流量管理器接收到请求后,它会检查请求的源 IP 并返回相关的终结点 。The Subnet traffic-routing method allows you to map a set of IP address ranges to specific endpoints and when a request is received by Traffic Manager, it inspects the source IP of the request and returns the endpoint associated with it.

在本文讨论的方案中,使用子网路由时,流量将路由到内部网站或生产网站,具体取决于用户查询的 IP 地址。In the scenario discussed in this article, using subnet routing, depending on the IP address of the user's query, traffic is either routed to an internal website or a production website.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

为了查看流量管理器的运作方式,本教程要求部署以下各项:In order to see the Traffic Manager in action, this tutorial requires that you deploy the following:

  • 两个基本网站在不同的 Azure 区域运行 - 中国东部(作为内部网站)和中国北部(作为生产网站) 。two basic websites running in different Azure regions - China East (serves as internal website) and China North (serves as production website).
  • 用于测试流量管理器的两个测试 VM - 一个 VM 位于“中国东部”,另一个 VM 位于“中国北部”。 two test VMs for testing the Traffic Manager - one VM in China East and the second VM in China North.

测试 VM 用于演示流量管理器如何基于用户查询发起的子网,将用户流量路由到内部网站或生产网站。The test VMs are used to illustrate how Traffic Manager routes user traffic to the internal website or the production website based on subnet from where the user query originates.

登录 AzureSign in to Azure

通过 https://portal.azure.cn 登录到 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn.

创建网站Create websites

在本部分,我们将创建两个网站实例,以便为 Azure 区域中的流量管理器配置文件提供两个服务终结点。In this section, you create two website instances that provide the two service endpoints for the Traffic Manager profile in two Azure regions. 通过以下步骤创建两个网站:Creating the two websites includes the following steps:

  1. 创建两个用于运行基本网站的 VM - 一个 VM 位于“中国东部”,另一个位于“中国北部”。 Create two VMs for running a basic website - one in China East, and the other in China North.
  2. 在每个 VM 上安装 IIS 服务器,并更新默认网站页面,该页面描述当用户访问网站时要连接到的 VM 名称。Install IIS server on each VM and update the default website page that describes the VM name that a user is connected to when visiting the website.

创建用于运行网站的 VMCreate VMs for running websites

本部分介绍如何创建两个分别位于 Azure 区域“中国东部”和“中国北部”的 VM:myIISVMChinaEastmyIISVMChinaNorthIn this section, you create two VMs myIISVMChinaEast and myIISVMChinaNorth in the China East and China North Azure regions.

  1. 在 Azure 门户的左上角选择“创建资源” > “虚拟机” > “Windows Server 2016 VM” 。On the upper, left corner of the Azure portal, select Create a resource > Virtual Machines > Windows Server 2016 VM.

  2. 对于“基本信息”输入或选择以下信息,接受剩下的默认设置,然后选择“创建” :Enter, or select, the following information for Basics, accept the defaults for the remaining settings, and then select Create:

    设置Setting Value
    NameName myIISVMChinaEastmyIISVMChinaEast
    用户名User name 输入所选用户名。Enter a user name of your choosing.
    密码Password 输入所选密码。Enter a password of your choosing. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    资源组Resource group 选择“新建” ,然后键入 myResourceGroupTM1Select New and then type myResourceGroupTM1.
    LocationLocation 选择“中国东部”。 Select China East.
  3. 在“选择大小”下选择 VM 大小 。Select a VM size under Choose a size.

  4. 对于“设置” 选择以下值,然后选择“确定” :Select the following values for Settings, then select OK:

    设置Setting Value
    虚拟网络Virtual network 选择“虚拟网络”,在“创建虚拟网络”中,为“名称”输入 myVNet1,为“子网”输入 mySubnetSelect Virtual network, in Create virtual network, for Name, enter myVNet1, for subnet, enter mySubnet.
    网络安全组Network Security Group 选择“基本”,在“选择公共入站端口”下拉列表中选择“HTTP”和“RDP” Select Basic, and in Select public inbound ports drop-down, select HTTP and RDP
    启动诊断Boot diagnostics 选择“已禁用”。 Select Disabled.
  5. 在“摘要”中的“创建”下,选择“创建”以启动 VM 部署 。Under Create in the Summary, select Create to start the VM deployment.

  6. 再次完成步骤 1-6,并做出以下更改:Complete steps 1-6 again, with the following changes:

    设置Setting ValueValue
    资源组Resource group 选择“新建” ,然后键入 myResourceGroupTM2Select New, and then type myResourceGroupTM2
    LocationLocation 中国北部China North
    VM 名称VM Name myIISVMChinaNorthmyIISVMChinaNorth
    虚拟网络Virtual network 选择“虚拟网络”,在“创建虚拟网络”中,为“名称”输入 myVNet2,为“子网”输入 mySubnetSelect Virtual network, in Create virtual network, for Name, enter myVNet2, for subnet, enter mySubnet.
  7. 创建 VM 可能需要数分钟的时间。The VMs take a few minutes to create. 在两个 VM 完成创建之前,不要继续执行剩余的步骤。Do not continue with the remaining steps until both VMs are created.

    创建 VM

安装 IIS 并自定义默认网页Install IIS and customize the default web page

本部分介绍如何在 myIISVMChinaEastmyIISVMChinaNorth 这两个 VM 上安装 IIS 服务器,然后如何更新默认网站页面。In this section, you install the IIS server on the two VMs - myIISVMChinaEast & myIISVMChinaNorth, and then update the default website page. 自定义网站页面显示从 Web 浏览器访问网站时要连接到的 VM 的名称。The customized website page shows the name of the VM that you are connecting to when you visit the website from a web browser.

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中,单击位于 myResourceGroupTM1 资源组中的“myIISVMChinaEast”。 Select All resources in the left-hand menu, and then from the resources list click myIISVMChinaEast that is located in the myResourceGroupTM1 resource group.
  2. 在“概述”页上单击“连接”,然后在“连接到虚拟机”中选择“下载 RDP 文件”。 On the Overview page, click Connect, and then in Connect to virtual machine, select Download RDP file.
  3. 打开下载的 rdp 文件。Open the downloaded rdp file. 出现提示时,选择“连接” 。If prompted, select Connect. 输入在创建 VM 时指定的用户名和密码。Enter the user name and password you specified when creating the VM. 可能需要选择“更多选择” ,然后选择“使用其他帐户” ,以指定在创建 VM 时输入的凭据。You may need to select More choices, then Use a different account, to specify the credentials you entered when you created the VM.
  4. 选择“确定” 。Select OK.
  5. 你可能会在登录过程中收到证书警告。You may receive a certificate warning during the sign-in process. 如果收到警告,请选择“是”或“继续”以继续连接。 If you receive the warning, select Yes or Continue, to proceed with the connection.
  6. 在服务器桌面上导航到“Windows 管理工具” > “服务器管理器”。On the server desktop, navigate to Windows Administrative Tools>Server Manager.
  7. 在 myIISVMChinaEast 上启动 Windows PowerShell,使用以下命令安装 IIS 服务器并更新默认的 htm 文件 。Launch Windows PowerShell on myIISVMChinaEast and using the following commands to install IIS server and update the default htm file.
    # Install IIS
    Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
    # Remove default htm file
    remove-item C:\inetpub\wwwroot\iisstart.htm
    
    #Add custom htm file
    Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from my test website server - " + $env:computername)
    
  8. 关闭与 myIISVMChinaEast 之间的 RDP 连接。Close the RDP connection with myIISVMChinaEast.
  9. 重复步骤 1-6,与 myResourceGroupTM2 资源组中的 VM myIISVMChinaNorth 建立 RDP 连接,以安装 IIS 并自定义其默认网页。Repeat steps 1-6 with by creating an RDP connection with the VM myIISVMChinaNorth within the myResourceGroupTM2 resource group to install IIS and customize its default web page.
  10. 在 myIISVMChinaNorth 上启动 Windows PowerShell,使用以下命令安装 IIS 服务器并更新默认的 htm 文件 。Launch Windows PowerShell on myIISVMChinaNorth and using the following commands to install IIS server and update the default htm file.
    # Install IIS
    Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
    # Remove default htm file
    remove-item C:\inetpub\wwwroot\iisstart.htm
    
    #Add custom htm file
    Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from my production website server - " + $env:computername)
    

为运行 IIS 的 VM 配置 DNS 名称Configure DNS names for the VMs running IIS

流量管理器基于服务终结点的 DNS 名称路由用户流量。Traffic Manager routes user traffic based on DNS name of the service endpoints. 本部分介绍如何为 IIS 服务器 myIISVMChinaEastmyIISVMChinaNorth 配置 DNS 名称。In this section, you configure the DNS names for the IIS servers - myIISVMChinaEast and myIISVMChinaNorth.

  1. 在左侧菜单中单击“所有资源”,然后在资源列表中,选择位于 myResourceGroupTM1 资源组中的“myIISVMChinaEast”。 Click All resources in the left-hand menu, and then from the resources list, select myIISVMChinaEast that is located in the myResourceGroupTM1 resource group.
  2. 在“概述”页上的“DNS 名称”下,选择“配置”。 On the Overview page, under DNS name, select Configure.
  3. 在“配置”页上的 DNS 名称标签下添加唯一的名称,然后选择“保存”。 On the Configuration page, under DNS name label, add a unique name, and then select Save.
  4. 针对位于 myResourceGroupTM1 资源组中名为 myIISVMChinaNorth 的 VM 重复步骤 1-3。Repeat steps 1-3, for the VM named myIISVMChinaNorth that is located in the myResourceGroupTM1 resource group.

创建测试 VMCreate test VMs

在本部分中,将在每个 Azure 区域(“中国东部”和“中国北部”)创建一个 VM(myVMChinaEast 和 myVMChinaNorth ) 。In this section, you create a VM (myVMChinaEast and myVMChinaNorth) in each Azure region (China East and China North. 稍后将使用这些 VM 来测试当你浏览到该网站时,流量管理器如何将流量路由到最近的 IIS 服务器。You will use these VMs to test how Traffic Manager routes traffic to the nearest IIS server when you browse to the website.

  1. 在 Azure 门户的左上角选择“创建资源” > “虚拟机” > “Windows Server 2016 VM” 。On the upper, left corner of the Azure portal, select Create a resource > Virtual Machines > Windows Server 2016 VM.

  2. 对于“基本信息”输入或选择以下信息,接受剩下的默认设置,然后选择“创建” :Enter, or select, the following information for Basics, accept the defaults for the remaining settings, and then select Create:

    设置Setting Value
    NameName myVMChinaEastmyVMChinaEast
    用户名User name 输入所选用户名。Enter a user name of your choosing.
    密码Password 输入所选密码。Enter a password of your choosing. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    资源组Resource group 选择“现有”,然后选择“myResourceGroupTM1” 。Select Existing and then select myResourceGroupTM1.
  3. 在“选择大小”下选择 VM 大小 。Select a VM size under Choose a size.

  4. 对于“设置” 选择以下值,然后选择“确定” :Select the following values for Settings, then select OK:

    设置Setting Value
    虚拟网络Virtual network 选择“虚拟网络”,在“创建虚拟网络”中,为“名称”输入 myVNet3,为“子网”输入 mySubnet3 。Select Virtual network, in Create virtual network, for Name, enter myVNet3, for subnet, enter mySubnet3.
    网络安全组Network Security Group 选择“基本”,在“选择公共入站端口”下拉列表中选择“HTTP”和“RDP” Select Basic, and in Select public inbound ports drop-down, select HTTP and RDP
    启动诊断Boot diagnostics 选择“已禁用”。 Select Disabled.
  5. 在“摘要”中的“创建”下,选择“创建”以启动 VM 部署 。Under Create in the Summary, select Create to start the VM deployment.

  6. 再次完成步骤 1-5,但需要做出以下更改:Complete steps 1-5 again, with the following changes:

    设置Setting ValueValue
    VM 名称VM Name myVMChinaNorthmyVMChinaNorth
    资源组Resource group 选择“现有”,然后键入 myResourceGroupTM2 Select Existing, and then type myResourceGroupTM2
    虚拟网络Virtual network 选择“虚拟网络”,在“创建虚拟网络”中,为“名称”输入 myVNet4,为“子网”输入 mySubnet4 。Select Virtual network, in Create virtual network, for Name, enter myVNet4, for subnet, enter mySubnet4.
  7. 创建 VM 可能需要数分钟的时间。The VMs take a few minutes to create. 在两个 VM 完成创建之前,不要继续执行剩余的步骤。Do not continue with the remaining steps until both VMs are created.

创建流量管理器配置文件Create a Traffic Manager profile

创建一个流量管理器配置文件,该配置文件允许根据请求的源 IP 返回特定终结点。Create a Traffic Manager profile that allows you to return specific endpoints based on the source IP of the request.

  1. 在屏幕左上方,选择“创建资源” > “网络” > “全部查看” > “流量管理器配置文件” > “创建” 。On the top left-hand side of the screen, select Create a resource > Networking > See All > Traffic Manager profile > Create.

  2. 在“创建流量管理器配置文件”中输入或选择以下信息,接受剩下的默认设置,然后选择“创建” :In the Create Traffic Manager profile, enter or select, the following information, accept the defaults for the remaining settings, and then select Create:

    设置Setting Value
    NameName 此名称必须在 trafficmanager.cn 区域中唯一,并会生成用于访问流量管理器配置文件的 DNS 名称 trafficmanager.cn。This name needs to be unique within the trafficmanager.cn zone and results in the DNS name, trafficmanager.cn that is used to access your Traffic Manager profile.
    路由方法Routing method 选择“子网”路由方法 。Select the Subnet routing method.
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择“现有”,然后输入 myResourceGroupTM1 。Select Existing and enter myResourceGroupTM1.

    创建流量管理器配置文件

添加流量管理器终结点Add Traffic Manager endpoints

添加运行 IIS 服务器的两个 VM (myIISVMChinaEast & myIISVMChinaNorth),根据用户查询的子网路由用户流量 。Add the two VMs running the IIS servers - myIISVMChinaEast & myIISVMChinaNorth to route user traffic based on the subnet of the user's query.

  1. 在门户的搜索栏中,搜索在前面部分创建的流量管理器配置文件名称,并在显示的结果中选择该配置文件。In the portal's search bar, search for the Traffic Manager profile name that you created in the preceding section and select the profile in the results that the displayed.

  2. 在“流量管理器配置文件” 的“设置” 部分单击“终结点” ,然后单击“添加”。 In Traffic Manager profile, in the Settings section, click Endpoints, and then click Add.

  3. 输入或选择以下信息,保留剩下的默认设置,然后选择“确定” :Enter, or select, the following information, accept the defaults for the remaining settings, and then select OK:

    设置Setting Value
    类型Type Azure 终结点Azure endpoint
    NameName myTestWebSiteEndpointmyTestWebSiteEndpoint
    目标资源类型Target resource type 公共 IP 地址Public IP Address
    目标资源Target resource 选择公共 IP 地址以显示同一订阅下具有公共 IP 地址的资源列表。Choose a Public IP address to show the listing of resources with Public IP addresses under the same subscription. 在“资源”中,选择名为 myIISVMChinaEast-ip 的公共 IP 地址。 In Resource, select the public IP address named myIISVMChinaEast-ip. 这是中国东部的 IIS 服务器 VM 的公共 IP 地址。This is the public IP address of the IIS server VM in China East.
    子网路由设置Subnet routing settings 添加 myVMChinaEast 测试 VM 的 IP 地址 。Add the IP address of myVMChinaEast test VM. 源自此 VM 的任何用户查询都将定向到 myTestWebSiteEndpoint 。Any user query originating from this VM will be directed to the myTestWebSiteEndpoint.
  4. 重复步骤 2 和 3,为名为 myIISVMChinaNorth 的 IIS 服务器 VM 所关联的公共 IP 地址 myIISVMChinaNorth-ip 添加名为 myProductionEndpoint 的另一个终结点。Repeat steps 2 and 3 to add another endpoint named myProductionEndpoint for the public IP address myIISVMChinaNorth-ip that is associated with the IIS server VM named myIISVMChinaNorth. 对于“子网路由”设置,添加测试 VM myVMChinaNorth 的 IP 地址 。For Subnet routing settings, add the IP address of the test VM - myVMChinaNorth. 源自此测试 VM 的任何用户查询都将路由到终结点 myProductionWebsiteEndpoint 。Any user query from this test VM will be routed to the endpoint - myProductionWebsiteEndpoint.

  5. 添加完这两个终结点后,这两个终结点会显示在“流量管理器配置文件” 中,并且其监视状态为“联机” 。When the addition of both endpoints is complete, they are displayed in Traffic Manager profile along with their monitoring status as Online.

    添加流量管理器终结点

测试流量管理器配置文件Test Traffic Manager profile

本部分测试流量管理器如何将用户流量从给定子网路由到特定终结点。In this section, you test how the Traffic Manager routes user traffic from a given subnet to a specific endpoint. 若要查看流量管理器的运作方式,请完成以下步骤:To view the Traffic Manager in action, complete the following steps:

  1. 确定流量管理器配置文件的 DNS 名称。Determine the DNS name of your Traffic Manager profile.

  2. 按如下所述查看流量管理器的运作方式:View Traffic Manager in action as follows:

    • 在位于“中国东部”区域的测试 VM (myVMChinaEast) 上,通过 Web 浏览器浏览到流量管理器配置文件的 DNS 名称。 From the test VM (myVMChinaEast) that is located in the China East region, in a web browser, browse to the DNS name of your Traffic Manager profile.

    • 在位于“中国北部”区域的测试 VM (myVMChinaNorth ) 上,通过 Web 浏览器浏览到流量管理器配置文件的 DNS 名称。 From the test VM (myVMChinaNorth) that is located in the China North region, in a web browser, browse to the DNS name of your Traffic Manager profile.

确定流量管理器配置文件的 DNS 名称Determine DNS name of Traffic Manager profile

在本教程中,为简单起见,我们将使用流量管理器配置文件的 DNS 名称来访问网站。In this tutorial, for simplicity, you use the DNS name of the Traffic Manager profile to visit the websites.

可按如下所述确定流量管理器配置文件的 DNS 名称:You can determine the DNS name of the Traffic Manager profile as follows:

  1. 在门户的搜索栏中,搜索在前面部分中创建的流量管理器配置文件名称。In the portal's search bar, search for the Traffic Manager profile name that you created in the preceding section. 在显示的结果中,单击流量管理器配置文件。In the results that are displayed, click the traffic manager profile.

  2. 单击“概览”。 Click Overview.

  3. “流量管理器配置文件” 会显示新建的流量管理器配置文件的 DNS 名称。The Traffic Manager profile displays the DNS name of your newly created Traffic Manager profile. 在生产部署中,可以使用 DNS CNAME 记录配置一个指向流量管理器域名的虚构域名。In production deployments, you configure a vanity domain name to point to the Traffic Manager domain name, using a DNS CNAME record.

    流量管理器 DNS 名称

查看正在运行的流量管理器View Traffic Manager in action

在本部分,我们可以查看流量管理器的运作方式。In this section, you can see the Traffic Manager is action.

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中,单击位于 myResourceGroupTM1 资源组中的“myVMChinaEast”。 Select All resources in the left-hand menu, and then from the resources list click myVMChinaEast that is located in the myResourceGroupTM1 resource group.

  2. 在“概述”页上单击“连接”,然后在“连接到虚拟机”中选择“下载 RDP 文件”。 On the Overview page, click Connect, and then in Connect to virtual machine, select Download RDP file.

  3. 打开下载的 rdp 文件。Open the downloaded rdp file. 出现提示时,选择“连接” 。If prompted, select Connect. 输入在创建 VM 时指定的用户名和密码。Enter the user name and password you specified when creating the VM. 可能需要选择“更多选择” ,然后选择“使用其他帐户” ,以指定在创建 VM 时输入的凭据。You may need to select More choices, then Use a different account, to specify the credentials you entered when you created the VM.

  4. 选择“确定” 。Select OK.

  5. 你可能会在登录过程中收到证书警告。You may receive a certificate warning during the sign-in process. 如果收到警告,请选择“是”或“继续”以继续连接。 If you receive the warning, select Yes or Continue, to proceed with the connection.

  6. 在 VM myVMChinaEast 上的 Web 浏览器中,键入流量管理器配置文件的 DNS 名称,以查看网站。In a web browser on the VM myVMChinaEast, type the DNS name of your Traffic Manager profile to view your website. 由于 VM myVMChinaEast IP 地址与终结点 myIISVMChinaEast 关联,因此 Web 浏览器启动测试网站服务器 myIISVMChinaEast 。Since the VM myVMChinaEast IP address is associated with the endpoint myIISVMChinaEast, the web browser launches the Test website server - myIISVMChinaEast.

    测试流量管理器配置文件

  7. 接下来,使用步骤 1-5 连接到位于“中国北部”的 VM myVMChinaNorth,然后从此 VM 浏览到流量管理器配置文件域名。 Next, connect to the VM myVMChinaNorth located in China North using steps 1-5 and browse to the Traffic Manager profile domain name from this VM. 由于 VM myVMChinaNorth IP 地址与终结点 myIISVMChinaNorth 关联,因此 Web 浏览器将启动测试网站服务器 myIISVMChinaNorth 。Since the VM myVMChinaNorth IP address is associated with the endpoint myIISVMChinaNorth, the web browser launches the Test website server - myIISVMChinaNorth.

删除流量管理器配置文件Delete the Traffic Manager profile

不再需要时,请删除资源组(ResourceGroupTM1ResourceGroupTM2)。When no longer needed, delete the resource groups (ResourceGroupTM1 and ResourceGroupTM2). 为此,请选择资源组(ResourceGroupTM1ResourceGroupTM2),然后选择“删除”。 To do so, select the resource group (ResourceGroupTM1 or ResourceGroupTM2), and then select Delete.

后续步骤Next steps