教程:在 Azure 门户中通过基本负载均衡器对内部流量负载进行均衡Tutorial: Balance internal traffic load with a Basic load balancer in the Azure portal

负载均衡将传入请求分布到虚拟机 (VM),以便提供更高级别的可用性和可伸缩性。Load balancing provides a higher level of availability and scale by spreading incoming requests across virtual machines (VMs). 可以使用 Azure 门户创建基本负载均衡器,并在 VM 之间对内部流量进行均衡。You can use the Azure portal to create a Basic load balancer and balance internal traffic among VMs. 本教程介绍如何创建并配置内部负载均衡器、后端服务器以及“基本”定价层的网络资源。This tutorial shows you how to create and configure an internal load balancer, back-end servers, and network resources at the Basic pricing tier.

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

如果你愿意,可以使用 Azure CLIAzure PowerShell 而不是门户来完成这些步骤。If you prefer, you can do these steps using the Azure CLI or Azure PowerShell instead of the portal.

若要完成本教程中的这些步骤,请通过 https://portal.azure.cn 登录 Azure 门户。To do the steps using this tutorial, sign in to the Azure portal at https://portal.azure.cn.

创建 VNet、后端服务器和测试 VMCreate a VNet, back-end servers, and a test VM

首先,创建虚拟网络 (VNet)。First, create a virtual network (VNet). 在 VNet 中,创建两个 VM,用于基本负载均衡器的后端池,再创建第三个 VM,用于测试负载均衡器。In the VNet, create two VMs to use for the back-end pool of your Basic load balancer, and a third VM to use for testing the load balancer.

创建虚拟网络Create a virtual network

  1. 在门户的左上方,选择“创建资源” > “网络” > “虚拟网络”。On the upper-left side of the portal, select Create a resource > Networking > Virtual network.

  2. 在“创建虚拟网络”窗格中键入或选择以下值:In the Create virtual network pane, type or select these values:

    • 名称:键入“MyVNet”。Name: Type MyVNet.
    • 资源组:选择“新建”,输入 MyResourceGroupLB,然后选择“确定”。ResourceGroup: Select Create new, then enter MyResourceGroupLB, and select OK.
    • 子网 > 名称:键入“MyBackendSubnet”。Subnet > Name: Type MyBackendSubnet.
  3. 选择“创建” 。Select Create.

    创建虚拟网络

创建虚拟机Create virtual machines

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

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

    • 订阅 > 资源组:下拉并选择“MyResourceGroupLB”。Subscription > Resource Group: Drop down and select MyResourceGroupLB.
    • 实例详细信息 > 虚拟机名称:键入 MyVM1Instance Details > Virtual machine name: Type MyVM1.
    • “实例详细信息” > “可用性选项”:Instance Details > Availability Options:
      1. 在下拉列表中选择“可用性集”。Drop down and select Availability set.
      2. 选择“新建”,键入 MyAvailabilitySet,然后选择“确定”。Select Create new, type MyAvailabilitySet, and select OK.
  3. 选择“网络”选项卡,或选择“下一步: 磁盘”,然后选择“下一步:网络”Select the Networking tab, or select Next: Disks, then Next: Networking.

    确保选中以下项:Make sure the following are selected:

    • 虚拟网络MyVNetVirtual network: MyVNet
    • 子网MyBackendSubnetSubnet: MyBackendSubnet

    在“网络安全组”下:Under Network Security Group:

    1. 选择“高级”。Select Advanced.
    2. 转到“配置网络安全组”下拉列表,选择“无”。Drop down Configure network security group and select None.
  4. 选择“管理”选项卡,或者选择“下一步” > “管理”。Select the Management tab, or select Next > Management. 在“监视”下,将“启动诊断”设置为“关闭”。Under Monitoring, set Boot diagnostics to Off.

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

  6. 检查设置,然后选择“创建”。Review the settings, and then select Create.

  7. 按步骤创建另一个 VM,其名称为 MyVM2,所有其他设置与 MyVM1 相同。Follow the steps to create a second VM named MyVM2, with all the other settings the same as MyVM1.

  8. 按步骤创建第三个 VM,请名称为 MyTestVMFollow the steps again to create a third VM named MyTestVM.

创建基本负载均衡器Create a Basic load balancer

使用门户创建基本的内部负载均衡器。Create a Basic internal load balancer by using the portal. 创建的名称和 IP 地址自动配置为负载均衡器的前端。The name and IP address you create are automatically configured as the load balancer's front end.

  1. 在门户的左上方,选择“创建资源” > “网络” > “负载均衡器”。On the upper-left side of the portal, select Create a resource > Networking > Load Balancer.

  2. 在“创建负载均衡器”页的“基本”选项卡中输入或选择以下信息,接受其余的默认设置,然后选择“查看 + 创建”:In the Basics tab of the Create load balancer page, enter or select the following information, accept the defaults for the remaining settings, and then select Review + create:

    设置Setting ValueValue
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择“新建”并在文本框中键入 MyResourceGroupLB。Select Create new and type MyResourceGroupLB in the text box.
    NameName myLoadBalancermyLoadBalancer
    区域Region 选择“中国东部 2”。Select China East 2.
    类型Type 选择“内部”。Select Internal.
    SKUSKU 选择“基本”。Select Basic.
    虚拟网络Virtual network 选择“MyVNet”。Select MyVNet.
    IP 地址分配IP address assignment 选择“静态”。Select Static.
    专用 IP 地址Private IP address 键入一个位于虚拟网络和子网地址空间中的地址,例如 10.3.0.7Type an address that is in the address space of your virtual network and subnet, for example 10.3.0.7.
  3. 在“查看 + 创建”选项卡中,单击“创建”。In the Review + create tab, click Create.

创建基本负载均衡器资源Create Basic load balancer resources

在本部分中,请为后端地址池和运行状况探测配置负载均衡器设置,并指定负载均衡器规则。In this section, you configure load balancer settings for a back-end address pool and a health probe, and specify load balancer rules.

创建后端地址池Create a back-end address pool

若要向 VM 分配流量,请让负载均衡器使用后端地址池。To distribute traffic to the VMs, the load balancer uses a back-end address pool. 后端地址池包含连接到负载均衡器的虚拟网络接口 (NIC) 的 IP 地址。The back-end address pool contains the IP addresses of the virtual network interfaces (NICs) that are connected to the load balancer.

若要创建包含 VM1 和 VM2 的后端地址池,请执行以下操作:To create a back-end address pool that includes VM1 and VM2:

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中选择“MyLoadBalancer”。Select All resources on the left menu, and then select MyLoadBalancer from the resource list.

  2. 在“设置”下,依次选择“后端池”、“添加”。Under Settings, select Backend pools, and then select Add.

  3. 在“添加后端池”页上,键入或选择以下值:On the Add a backend pool page, type or select the following values:

    • 名称:键入 MyBackendPoolName: Type MyBackendPool.
    • 关联到:在下拉列表中选择“可用性集”。Associated to: Drop down and select Availability set.
    • 可用性集:选择“MyAvailabilitySet”。Availability set: Select MyAvailabilitySet.
  4. 选择“添加目标网络 IP 配置”。Select Add a target network IP configuration.

    1. MyVM1MyVM2 添加到后端池。Add MyVM1 and MyVM2 to the back-end pool.
    2. 添加每台计算机以后,请在下拉列表中选择其网络 IP 配置After you add each machine, drop down and select its Network IP configuration.

    Note

    请勿向池添加 MyTestVMDo not add MyTestVM to the pool.

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

    添加后端地址池

  6. 在“后端池”页上展开 MyBackendPool,确保 VM1VM2 都已列出。On the Backend pools page, expand MyBackendPool and make sure both VM1 and VM2 are listed.

创建运行状况探测器Create a health probe

若要允许负载均衡器监视 VM 状态,请使用运行状况探测。To allow the load balancer to monitor VM status, you use a health probe. 运行状况探测器基于其对运行状况检查的响应,从负载均衡器中动态添加或删除 VM。The health probe dynamically adds or removes VMs from the load balancer rotation based on their response to health checks.

若要创建运行状况探测来监视 VM 的运行状况,请执行以下操作:To create a health probe to monitor the health of the VMs:

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中选择“MyLoadBalancer”。Select All resources on the left menu, and then select MyLoadBalancer from the resource list.

  2. 在“设置”下,依次选择“运行状况探测”、“添加”。Under Settings, select Health probes, and then select Add.

  3. 在“添加运行状况探测”页上,键入或选择以下值:On the Add a health probe page, type or select the following values:

    • 名称:键入 MyHealthProbeName: Type MyHealthProbe.
    • 协议:下拉并选择“HTTP”。Protocol: Drop down and select HTTP.
    • 端口:键入 80Port: Type 80.
    • 路径:接受 / 作为默认 URI。Path: Accept / for the default URI. 可以将此值替换为任何其他的 URI。You can replace this value with any other URI.
    • 时间间隔:键入 15Interval: Type 15. 时间间隔是两次探测尝试之间的秒数。Interval is the number of seconds between probe attempts.
    • 不正常阈值:键入 2Unhealthy threshold: Type 2. 此值是将 VM 视为不正常之前发生的连续探测失败次数。This value is the number of consecutive probe failures that occur before a VM is considered unhealthy.
  4. 选择“确定”。Select OK.

    添加探测

创建负载均衡器规则Create a load balancer rule

负载均衡器规则定义如何将流量分配给 VM。A load balancer rule defines how traffic is distributed to the VMs. 此规则定义传入流量的前端 IP 配置、用于接收流量的后端 IP 池,以及所需的源和目标端口。The rule defines the front-end IP configuration for incoming traffic, the back-end IP pool to receive the traffic, and the required source and destination ports.

名为 MyLoadBalancerRule 的负载均衡器规则在前端 LoadBalancerFrontEnd 中侦听端口 80。The load balancer rule named MyLoadBalancerRule listens to port 80 in the front-end LoadBalancerFrontEnd. 该规则也在端口 80 上将网络流量发送到后端地址池 MyBackendPoolThe rule sends network traffic to the back-end address pool MyBackendPool, also on port 80.

若要创建负载均衡器规则,请执行以下操作:To create the load balancer rule:

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中选择“MyLoadBalancer”。Select All resources on the left menu, and then select MyLoadBalancer from the resource list.

  2. 在“设置”下,依次选择“负载均衡规则”、“添加”。Under Settings, select Load balancing rules, and then select Add.

  3. 在“添加负载均衡规则”页上,键入或选择以下值(如果还没有这些值):On the Add load balancing rule page, type or select the following values, if not already present:

    • 名称:键入 MyLoadBalancerRuleName: Type MyLoadBalancerRule.
    • 前端 IP 地址: 键入 LoadBalancerFrontEnd(如果不存在)。Frontend IP address: Type LoadBalancerFrontEnd if not present.
    • 协议:选择“TCP”。Protocol: Select TCP.
    • 端口:键入 80Port: Type 80.
    • 后端端口:键入 80Backend port: Type 80.
    • 后端池:选择“MyBackendPool”。Backend pool: Select MyBackendPool.
    • 运行状况探测:选择“MyHealthProbe”。Health probe: Select MyHealthProbe.
  4. 选择“确定” 。Select OK.

    添加负载均衡器规则

测试负载均衡器Test the load balancer

在后端服务器上安装 Internet Information Services (IIS),然后使用 MyTestVM 通过其专用 IP 地址测试负载均衡器。Install Internet Information Services (IIS) on the back-end servers, then use MyTestVM to test the load balancer using its private IP address. 每个后端 VM 提供不同版本的默认 IIS 网页,因此可以看到负载均衡器在两个 VM 之间分发请求。Each back-end VM serves a different version of the default IIS web page, so you can see the load balancer distribute requests between the two VMs.

在门户中的 MyLoadBalancer 的“概览”页上,在“专用 IP 地址”下找到其 IP 地址。In the portal, on the Overview page for MyLoadBalancer, find its IP address under Private IP Address. 将鼠标悬停在地址上,选择“复制”图标对其进行复制。Hover over the address and select the Copy icon to copy it. 在此示例中,它是 10.3.0.7In this example, it is 10.3.0.7.

通过 RDP 连接到 VMConnect to the VMs with RDP

首先,通过远程桌面 (RDP) 连接到所有三个 VM。First, connect to all three VMs with Remote Desktop (RDP).

Note

默认情况下,这些 VM 已经打开 RDP(远程桌面)端口,因此可以进行远程桌面访问。By default, the VMs already have the RDP (Remote Desktop) port open to allow remote desktop access.

若要通过远程桌面 (RDP) 连接到 VM,请执行以下操作:To remote desktop (RDP) into the VMs:

  1. 在门户的左侧菜单中选择“所有资源”。In the portal, select All resources on the left menu. 在资源列表中,选择“MyResourceGroupLB”资源组中的每个 VM。From the resource list, select each VM in the MyResourceGroupLB resource group.

  2. 在“概览”页上选择“连接”,然后选择“下载 RDP 文件”。On the Overview page, select Connect, and then select Download RDP file.

  3. 打开下载的 RDP 文件,然后选择“连接”。Open the RDP file you downloaded, and select Connect.

  4. 在“Windows 安全性”屏幕上选择“更多选择”,然后选择“使用其他帐户”。On the Windows Security screen, select More choices and then Use a different account.

    输入用户名和密码,然后选择“确定”。Enter username and password and then select OK.

  5. 使用“是”对任何证书请求进行响应。Respond Yes to any certificate prompt.

    VM 桌面会在新窗口中打开。The VM desktop opens in a new window.

安装 IIS 并替换后端 VM 上的默认 IIS 页。Install IIS and replace the default IIS page on the back-end VMs

在每个后端服务器上,使用 PowerShell 安装 IIS,并将默认的 IIS 网页替换为自定义的页面。On each back-end server, use PowerShell to install IIS and replace the default IIS web page with a customized page.

Note

也可在服务器管理器中使用添加角色和功能向导来安装 IIS。You can also use the Add Roles and Features Wizard in Server Manager to install IIS.

若要使用 PowerShell 来安装 IIS 并更新默认的网页,请执行以下操作:To install IIS and update the default web page with PowerShell:

  1. 在 MyVM1 和 MyVM2 上,通过“开始”菜单启动 Windows PowerShellOn MyVM1 and on MyVM2, launch Windows PowerShell from the Start menu.

  2. 运行以下命令,以便安装 IIS 并替换默认的 IIS 网页:Run the following commands to install IIS and replace the default IIS web page:

     # 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)
    
  3. 选择“断开连接”,关闭与 MyVM1 和 MyVM2 的 RDP 连接。Close the RDP connections with MyVM1 and MyVM2 by selecting Disconnect. 请勿关闭 VM。Do not shut down the VMs.

测试负载均衡器Test the load balancer

  1. 在 MyTestVM 上打开 Internet Explorer,在出现配置请求时使用“确定”进行响应。On MyTestVM, open Internet Explorer, and respond OK to any configuration prompts.

  2. 将负载均衡器的专用 IP 地址 (10.3.0.7) 粘贴或键入到浏览器的地址栏中。Paste or type the load balancer's private IP address (10.3.0.7) into the address bar of the browser.

    自定义的 IIS Web 服务器默认页会显示在浏览器中。The customized IIS web server default page appears in the browser. 会显示 Hello World from MyVM1Hello World from MyVM2 消息。The message reads either Hello World from MyVM1, or Hello World from MyVM2.

  3. 刷新浏览器,可以看到负载均衡器在 VM 之间分配流量。Refresh the browser to see the load balancer distribute traffic across VMs. 在进行不同的尝试时,可能还需要清除浏览器缓存。You may also need to clear your browser cache between attempts.

    当负载均衡器将请求分配到每个后端 VM 时,有时会显示 MyVM1 页,有时会显示 MyVM2 页。Sometimes the MyVM1 page appears, and other times the MyVM2 page appears, as the load balancer distributes the requests to each back-end VM.

    新建 IIS 默认页

清理资源Clean up resources

若要在不再需要的情况下删除负载均衡器和所有相关的资源,请打开 MyResourceGroupLB 资源组,然后选择“删除资源组”。To delete the load balancer and all related resources when you no longer need them, open the MyResourceGroupLB resource group and select Delete resource group.