教程:使用流量管理器控制加权终结点的流量路由Tutorial: Control traffic routing with weighted endpoints by using Traffic Manager

本教程介绍如何通过 Azure 流量管理器使用加权路由方法控制用户流量在终结点之间的路由。This tutorial describes how to use Azure Traffic Manager to control routing of user traffic between endpoints by using the weighted routing method. 在此路由方法中,需要为流量管理器配置文件配置中的每个终结点分配权重。In this routing method, you assign weights to each endpoint in the Traffic Manager profile configuration. 然后,系统会根据分配给每个终结点的权重进行用户流量路由。User traffic is then routed based on the weight assigned to each endpoint. 该权重是从 1 到 1,000 的整数。The weight is an integer from 1 to 1,000. 分配给终结点的权重值越高,其优先级就越高。The higher the weight value assigned to an endpoint, the higher its priority.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 在 IIS 上创建两个运行基本网站的 VM。Create two VMs running a basic website on IIS.
  • 创建两个测试 VM 以查看流量管理器的运作方式。Create two test VMs to view Traffic Manager in action.
  • 为运行 IIS 的 VM 配置 DNS 名称。Configure a DNS name for the VMs running IIS.
  • 创建流量管理器配置文件。Create a Traffic Manager profile.
  • 将 VM 终结点添加到流量管理器配置文件。Add VM endpoints to the Traffic Manager profile.
  • 查看正在运行的流量管理器。View Traffic Manager in action.

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

先决条件Prerequisites

为了查看流量管理器的运作方式,请为本教程部署以下各项:To see Traffic Manager in action, deploy the following for this tutorial:

  • 在以下不同的 Azure 区域运行的基本网站的两个实例:中国东部和中国北部。Two instances of basic websites running in different Azure regions: China East and China North.
  • 用于测试流量管理器的两个测试 VM:一个位于中国东部,另一个位于中国北部。Two test VMs for testing Traffic Manager: one in China East and the other in China North. 测试 VM 用于演示流量管理器如何将用户流量路由到分配给其终结点的权重更高的网站。The test VMs are used to illustrate how Traffic Manager routes user traffic to a website that has higher weight assigned to its endpoint.

登录 AzureSign in to Azure

登录到 Azure 门户Sign in to the Azure portal.

创建网站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. 若要创建这两个网站,请完成以下步骤:To create the two websites, complete the following steps:

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

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

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

  1. 在 Azure 门户的左上角,选择“创建资源” ,在“新建” 页的搜索筛选器中输入 Windows Server 2019 Datacenter,然后按 Enter 键。On the upper, left corner of the Azure portal, select Create a resource , Enter Windows Server 2019 Datacenter in search filter of New page, then press the Enter key.

  2. 在“创建虚拟机”中,在“基本信息”选项卡中键入或选择以下值: In Create a virtual machine, type or select the following values in the Basics tab:

    • 订阅 > 资源组:选择“新建”,然后键入 myResourceGroupTM1Subscription > Resource Group: Select Create new and then type myResourceGroupTM1.
    • 实例详细信息 > 虚拟机名称:键入“myIISVMChinaEast” 。Instance Details > Virtual machine name: Type myIISVMChinaEast.
    • “实例详细信息” > “区域”: 选择“中国东部”。 Instance Details > Region: Select China East.
    • “管理员帐户” > “用户名”:输入所选用户名。Administrator Account > Username: Enter a user name of your choosing.
    • “管理员帐户” > “密码”:输入所选密码。Administrator Account > Password: Enter a password of your choosing. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    • “入站端口规则” > “公共入站端口”:选择“允许所选端口” 。Inbound Port Rules > Public inbound ports: Select Allow selected ports.
    • “入站端口规则” > “选择入站端口”:在下拉框中选择“RDP”和“HTTP”。 Inbound Port Rules > Select inbound ports: Select RDP and HTTP in the pull down box.
  3. 选择“管理”选项卡,或者选择“下一步: 磁盘”,然后选择“下一步: 网络”,然后选择“下一步: 管理”。Select the Management tab, or select Next: Disks, then Next: Networking, then Next: Management. 在“监视” 下,将“启动诊断” 设置为“关闭”。 Under Monitoring, set Boot diagnostics to Off.

  4. 选择“查看 + 创建” 。Select Review + create.

  5. 查看设置,并单击“创建”。 Review the settings, and then click Create.

  6. 按步骤创建另一个 VM,其名称为“myIISVMChinaNorth” ,其“资源组”名称为“myResourceGroupTM2” ,其“位置”为“中国北部”,所有其他设置与 myIISVMChinaEast 相同。 Follow the steps to create a second VM named myIISVMChinaNorth, with a Resource group name of myResourceGroupTM2, a location of China North, and all the other settings the same as myIISVMChinaEast.

  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 webpage

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

  1. 在左侧菜单中选择“所有资源” 。Select All resources on the left menu. 从资源列表选择“myResourceGroupTM1”资源组中的“myIISVMChinaEast”。 From the resource list, select myIISVMChinaEast in the myResourceGroupTM1 resource group.

  2. 在“概览”页上,选择“连接”。 On the Overview page, select Connect. 在“连接到虚拟机”中选择“下载 RDP 文件”。 In Connect to virtual machine, select Download RDP file.

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

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

  5. 你可能会在登录过程中收到证书警告。You might 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, browse to Windows Administrative Tools > Server Manager.

  7. 在 VM1 上打开 Windows PowerShell。Open Windows PowerShell on VM1. 使用以下命令安装 IIS 服务器并更新默认的 .htm 文件。Use the following commands to install the 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 " + $env:computername)
    

    安装 IIS 并自定义网页

  8. 关闭与 myIISVMChinaEast 之间的 RDP 连接。Close the RDP connection with myIISVMChinaEast.

  9. 重复步骤 1-8。Repeat steps 1-8. myResourceGroupTM2 资源组中的 VM myIISVMChinaNorth 建立 RDP 连接,以安装 IIS 并自定义其默认网页。Create an RDP connection with the VM myIISVMChinaNorth within the myResourceGroupTM2 resource group, to install IIS and customize its default webpage.

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

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

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

创建测试 VMCreate a test VM

在本部分中,将在每个 Azure 区域(“中国东部”和“中国北部”)创建一个 VM(myVMChinaEast 和 myVMChinaNorth ) 。In this section, you create a VM (myVMChinaEast and myVMChinaNorth) in each Azure region (China East and China North). 将使用这些 VM 来测试流量管理器如何将流量路由到其权重值更高的网站终结点。You will use these VMs to test how Traffic Manager routes traffic to the website endpoint that has the higher weight value.

  1. 在 Azure 门户的左上角,选择“创建资源” ,在“新建” 页的搜索筛选器中输入 Windows Server 2019 Datacenter,然后按 Enter 键。On the upper, left corner of the Azure portal, select Create a resource , Enter Windows Server 2019 Datacenter in search filter of New page, then press the Enter key.

  2. 在“创建虚拟机”中,在“基本信息”选项卡中键入或选择以下值: In Create a virtual machine, type or select the following values in the Basics tab:

    • 订阅 > 资源组:选择“myResourceGroupTM1” 。Subscription > Resource Group: Select myResourceGroupTM1.
    • 实例详细信息 > 虚拟机名称:键入“myVMChinaEast” 。Instance Details > Virtual machine name: Type myVMChinaEast.
    • “实例详细信息” > “区域”: 选择“中国东部”。 Instance Details > Region: Select China East.
    • “管理员帐户” > “用户名”:输入所选用户名。Administrator Account > Username: Enter a user name of your choosing.
    • “管理员帐户” > “密码”:输入所选密码。Administrator Account > Password: Enter a password of your choosing. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    • “入站端口规则” > “公共入站端口”:选择“允许所选端口” 。Inbound Port Rules > Public inbound ports: Select Allow selected ports.
    • “入站端口规则” > “选择入站端口”:在下拉框中选择“RDP”。 Inbound Port Rules > Select inbound ports: Select RDP in the pull down box.
  3. 选择“管理”选项卡,或者选择“下一步: 磁盘”,然后选择“下一步: 网络”,然后选择“下一步: 管理”。Select the Management tab, or select Next: Disks, then Next: Networking, then Next: Management. 在“监视” 下,将“启动诊断” 设置为“关闭”。 Under Monitoring, set Boot diagnostics to Off.

  4. 选择“查看 + 创建” 。Select Review + create.

  5. 查看设置,并单击“创建”。 Review the settings, and then click Create.

  6. 按步骤创建另一个 VM,其名称为“myVMChinaNorth” ,其“资源组”名称为“myResourceGroupTM2” ,其“位置”为“中国北部”,所有其他设置与 myVMChinaEast 相同。 Follow the steps to create a second VM named myVMChinaNorth, with a Resource group name of myResourceGroupTM2, a location of China North, and all the other settings the same as myVMChinaEast.

  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

根据权重路由方法创建流量管理器配置文件。Create a Traffic Manager profile based on the Weighted routing method.

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

  2. 在“创建流量管理器配置文件”中,输入或选择以下信息 。In Create Traffic Manager profile, enter or select the following information. 对于其他设置,请接受默认值,然后选择“创建”。 Accept the defaults for the other settings, and then select Create.

    设置Setting Value
    NameName 输入 trafficmanager.cn 区域中的唯一名称。Enter a unique name within the trafficmanager.cn zone. 它会生成 DNS 名称 trafficmanager.cn,用于访问流量管理器配置文件。It results in the DNS name trafficmanager.cn, which is used to access your Traffic Manager profile.
    路由方法Routing method 选择权重路由方法。Select the Weighted routing method.
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择“使用现有”,然后选择“myResourceGroupTM1” 。Select Use existing and then select myResourceGroupTM1.

    创建流量管理器配置文件

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

添加两个运行 IIS 服务器的 VM(myIISVMChinaEast 和 myIISVMChinaNorth),以便将用户流量路由到其中。Add the two VMs running the IIS servers myIISVMChinaEast and myIISVMChinaNorth, to route user traffic to them.

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

  2. 在“流量管理器配置文件” 的“设置” 部分,选择“终结点” > “添加”。In Traffic Manager profile, in the Settings section, select Endpoints > Add.

  3. 输入或选择以下信息。Enter or select the following information. 对于其他设置,请接受默认值,然后选择“确定”。 Accept the defaults for the other settings, and then select OK.

    设置Setting Value
    类型Type 输入 Azure 终结点。Enter the Azure endpoint.
    NameName 输入 myChinaEastEndpointEnter myChinaEastEndpoint.
    目标资源类型Target resource type 选择“公共 IP 地址”。 Select 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.
    重量Weight 输入 100Enter 100.
  4. 重复步骤 2 和 3,为公共 IP 地址 myIISVMChinaNorth-ip 添加名为 myChinaNorthEndpoint 的另一个终结点。Repeat steps 2 and 3 to add another endpoint named myChinaNorthEndpoint for the public IP address myIISVMChinaNorth-ip. 此地址与名为 myIISVMChinaNorth 的 IIS 服务器 VM 相关联。This address is associated with the IIS server VM named myIISVMChinaNorth. 输入 25 作为“权重”。 For Weight, enter 25.

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

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

若要查看流量管理器的运作方式,请完成以下步骤:To view 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.

确定流量管理器配置文件的 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, select the Traffic Manager profile.

  2. 选择“概述”。 Select Overview.

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

    流量管理器 DNS 名称

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

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

  1. 在左侧菜单中选择“所有资源” 。Select All resources on the left menu. 从资源列表选择 myResourceGroupTM1 资源组中的 myVMChinaEastFrom the resource list, select myVMChinaEast in the myResourceGroupTM1 resource group.

  2. 在“概览”页上,选择“连接”。 On the Overview page, select Connect. 在“连接到虚拟机”中选择“下载 RDP 文件”。 In Connect to virtual machine, select Download RDP file.

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

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

  5. 你可能会在登录过程中收到证书警告。You might 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, enter the DNS name of your Traffic Manager profile to view your website. 系统会将你路由到托管在 IIS 服务器 myIISVMChinaEast 上的网站,因为为它分配的权重较高,为 100You're routed to website hosted on the IIS server myIISVMChinaEast because it's assigned a higher weight of 100. 为 IIS 服务器 myIISVMChinaNorth 分配的终结点权重值较低,为 25The IIS server myIISVMChinaNorth is assigned a lower endpoint weight value of 25.

    测试流量管理器配置文件

  7. 对 VM myVMChinaNorth 重复步骤 1-6,查看加权网站的响应。Repeat steps 1-6 on the VM myVMChinaNorth to see the weighted website response.

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

不再需要本教程中创建的资源组时,可将其删除。When you no longer need the resource groups that you created in this tutorial, you can delete them. 为此,请选择资源组(ResourceGroupTM1ResourceGroupTM2),然后选择“删除”。 To do so, select the resource group (ResourceGroupTM1 or ResourceGroupTM2), and then select Delete.

后续步骤Next steps