教程:通过 Azure 门户对从 Internet 到 VM 的流量进行负载均衡Tutorial: Load balance internet traffic to VMs using the Azure portal

可以通过负载均衡将传入请求分布到多个虚拟机,从而提供更高级别的可用性和可伸缩性。Load balancing provides a higher level of availability and scale by spreading incoming requests across multiple virtual machines. 本教程介绍 Azure 标准负载均衡器的各种组件,这些组件用于将 Internet 流量分发到 VM,并提供高可用性。In this tutorial, you learn about the different components of the Azure Standard Load Balancer that distribute internet traffic to VMs and provide high availability. 你将学习如何执行以下操作:You learn how to:

  • 创建 Azure 负载均衡器Create an Azure Load Balancer
  • 创建负载均衡器资源Create Load Balancer resources
  • 创建虚拟机并安装 IIS 服务器Create virtual machines and install IIS server
  • 查看负载均衡器的运作情况View Load Balancer in action
  • 在负载均衡器中添加和删除 VMAdd and remove VMs from a Load Balancer

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

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

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

创建标准负载均衡器Create a Standard Load Balancer

在本部分,我们将创建一个标准负载均衡器,以帮助对虚拟机进行负载均衡。In this section, you create a Standard Load Balancer that helps load balance virtual machines. 标准负载均衡器仅支持标准公共 IP 地址。Standard Load Balancer only supports a Standard Public IP address. 创建标准负载均衡器时,还必须为该标准负载均衡器创建一个配置为前端(默认情况下命名为 LoadBalancerFrontend)的新的标准公共 IP 地址。When you create a Standard Load Balancer, you must also create a new Standard Public IP address that is configured as the frontend (named as LoadBalancerFrontend by default) for the Standard Load Balancer.

  1. 在屏幕的左上方,单击“创建资源” > “网络” > “负载均衡器” 。On the top left-hand side of the screen, click 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 选择“新建”并在文本框中键入 myResourceGroupSLBSelect Create new and type myResourceGroupSLB in the text box.
    名称Name myLoadBalancermyLoadBalancer
    区域Region 选择“中国北部” 。Select China North.
    类型Type 选择“公共”。 Select Public.
    SKUSKU 选择“标准” 。Select Standard.
    公共 IP 地址Public IP address 选择“新建”。 Select Create new.
    公共 IP 地址名称Public IP address name 在文本框中键入 myPublicIP 。Type myPublicIP in the text box.
  3. 在“查看 + 创建”选项卡中,单击“创建” 。In the Review + create tab, click Create.

    创建标准负载均衡器

创建负载均衡器资源Create Load Balancer resources

在本部分中,我们将为后端地址池和运行状况探测配置负载均衡器设置,并指定均衡器规则。In this section, you configure Load Balancer settings for a backend address pool, a health probe, and specify a balancer rule.

创建后端地址池Create a backend address pool

若要向 VM 分发流量,后端地址池需包含连接到负载均衡器的虚拟 NIC 的 IP 地址。To distribute traffic to the VMs, a backend address pool contains the IP addresses of the virtual (NICs) connected to the Load Balancer. 创建后端地址池 myBackendPool 以包含用于对 Internet 流量进行负载均衡的虚拟机。Create the backend address pool myBackendPool to include virtual machines for load-balancing internet traffic.

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中单击“myLoadBalancer”。 Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.
  2. 在“设置”下单击“后端池”,然后单击“添加”。 Under Settings, click Backend pools, then click Add.
  3. 在“添加后端池”页上,键入 myBackendPool 作为后端池的名称,然后选择“添加”。 On the Add a backend pool page, for name, type myBackendPool, as the name for your backend pool, and then select Add.

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

若要允许负载均衡器监视应用的状态,请使用运行状况探测器。To allow the Load Balancer to monitor the status of your app, 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. 创建运行状况探测 myHealthProbe 以监视 VM 的运行状况。Create a health probe myHealthProbe to monitor the health of the VMs.

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中单击“myLoadBalancer”。 Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.

  2. 在“设置”下单击“运行状况探测”,然后单击“添加”。 Under Settings, click Health probes, then click Add.

  3. 使用以下值创建运行状况探测:Use these values to create the health probe:

    设置Setting ValueValue
    名称Name 输入 myHealthProbeEnter myHealthProbe.
    协议Protocol 选择“HTTP”。 Select HTTP.
    端口Port 输入 80Enter 80.
    时间间隔Interval 输入 15 作为两次探测尝试之间的时间间隔(以秒为单位)。Enter 15 for number of Interval in seconds between probe attempts.
    不正常阈值Unhealthy threshold 选择“2”,作为将 VM 视为不正常所要达到的不正常阈值或连续探测失败次数。 Select 2 for number of Unhealthy threshold or consecutive probe failures that must occur before a VM is considered unhealthy.
  4. 选择“确定” 。Select OK.

创建负载均衡器规则Create a Load Balancer rule

负载均衡器规则用于定义将流量分配给 VM 的方式。A Load Balancer rule is used to define how traffic is distributed to the VMs. 定义传入流量的前端 IP 配置和后端 IP 池以接收流量,同时定义所需的源端口和目标端口。You define the frontend IP configuration for the incoming traffic and the backend IP pool to receive the traffic, along with the required source and destination port. 创建负载均衡器规则 myLoadBalancerRuleWeb,以侦听前端 FrontendLoadBalancer 中的端口 80,并将经过负载均衡的网络流量发送到也使用端口 80 的后端地址池 myBackEndPoolCreate a Load Balancer rule myLoadBalancerRuleWeb for listening to port 80 in the frontend FrontendLoadBalancer and sending load-balanced network traffic to the backend address pool myBackEndPool also using port 80.

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中单击“myLoadBalancer”。 Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.

  2. 在“设置”下单击“负载均衡规则”,然后单击“添加” 。Under Settings, click Load balancing rules, then click Add.

  3. 使用以下值配置负载均衡规则:Use these values to configure the load-balancing rule:

    设置Setting ValueValue
    名称Name 输入 myHTTPRuleEnter myHTTPRule.
    协议Protocol 选择“TCP” 。Select TCP.
    端口Port 输入 80Enter 80.
    后端端口Backend port 输入 80Enter 80.
    后端池Backend pool 选择“myBackendPool”。 Select myBackendPool.
    运行状况探测Health probe 选择“myHealthProbe”。 Select myHealthProbe.
  4. 将剩余的字段保留默认设置,然后选择“确定”。 Leave the rest of the defaults and select OK.

创建后端服务器Create backend servers

在本部分,我们将创建一个虚拟网络,为负载均衡器的后端池创建三台虚拟机,然后在虚拟机上安装 IIS,以便对负载均衡器进行测试。In this section, you create a virtual network, create three virtual machines for the backend pool of the Load Balancer, and then install IIS on the virtual machines to help test the Load Balancer.

虚拟网络和参数Virtual network and parameters

在本部分中,你需要将步骤中的以下参数替换为以下信息:In this section you'll need to replace the following parameters in the steps with the information below:

参数Parameter ValueValue
<resource-group-name><resource-group-name> myResourceGroupSLB(选择现有资源组)myResourceGroupSLB (Select existing resource group)
<virtual-network-name><virtual-network-name> myVNetmyVNet
<region-name><region-name> 中国北部China North
<IPv4-address-space><IPv4-address-space> 10.1.0.0/1610.1.0.0/16
<subnet-name><subnet-name> mySubnetmySubnet
<subnet-address-range><subnet-address-range> 10.1.0.0/2410.1.0.0/24

创建虚拟网络Create the virtual network

在本部分,请创建虚拟网络和子网。In this section, you'll create a virtual network and subnet.

  1. 在屏幕的左上方选择“创建资源”>“网络”>“虚拟网络”,或者在搜索框中搜索“虚拟网络”。 On the upper-left side of the screen, select Create a resource > Networking > Virtual network or search for Virtual network in the search box.

  2. 在“创建虚拟网络” 的“基本信息”选项卡中输入或选择以下信息 :In Create virtual network, enter or select this information in the Basics tab:

    设置Setting Value
    项目详细信息Project Details
    订阅Subscription 选择 Azure 订阅Select your Azure subscription
    资源组Resource Group 选择“新建” ,输入 <resource-group-name> ,然后选择“确定”,或根据参数选择现有 <resource-group-name>Select Create new, enter <resource-group-name>, then select OK, or select an existing <resource-group-name> based on parameters.
    实例详细信息Instance details
    名称Name 输入 <virtual-network-name>Enter <virtual-network-name>
    区域Region 选择 <region-name>Select <region-name>
  3. 选择“IP 地址”选项卡 ,或选择页面底部的“下一步: IP 地址”按钮。Select the IP Addresses tab or select the Next: IP Addresses button at the bottom of the page.

  4. 在“IP 地址” 选项卡上,输入以下信息:In the IP Addresses tab, enter this information:

    设置Setting Value
    IPv4 地址空间IPv4 address space 输入 <IPv4-address-space>Enter <IPv4-address-space>
  5. 在“子网名称” 下,选择词语“默认” 。Under Subnet name, select the word default.

  6. 在“编辑子网”中输入以下信息: In Edit subnet, enter this information:

    设置Setting Value
    子网名称Subnet name 输入 <subnet-name>Enter <subnet-name>
    子网地址范围Subnet address range 输入 <subnet-address-range>Enter <subnet-address-range>
  7. 选择“保存”。 Select Save.

  8. 选择“查看 + 创建” 选项卡,或选择“查看 + 创建” 按钮。Select the Review + create tab or select the Review + create button.

  9. 选择“创建” 。Select Create.

创建虚拟机Create virtual machines

标准负载均衡器仅支持在后端池中具有标准 IP 地址的 VM。Standard Load Balancer only supports VMs with Standard IP addresses in the backend pool. 在本部分,我们将创建具有标准公共 IP 地址的三个 VM(myVM1myVM2myVM3),这些 VM 将添加到前面创建的标准负载均衡器后端池。In this section, you will create three VMs (myVM1, myVM2, and myVM3) with a Standard public IP address that are added to the backend pool of the Standard Load Balancer that was created earlier.

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

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

    • 订阅 > 资源组:选择“myResourceGroupSLB”。 Subscription > Resource Group: Select myResourceGroupSLB.
    • 实例详细信息 > 虚拟机名称:键入 myVM1Instance Details > Virtual machine name: Type myVM1.
    • 在“实例详细信息” > “区域”中,选择“中国北部”。 Instance Details > Region > select China North.
  3. 选择“网络” 选项卡,或选择“下一步: 磁盘”,然后选择“下一步: 网络”。Select the Networking tab, or select Next: Disks, then Next: Networking.

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

      • 虚拟网络myVnetVirtual network: myVnet
      • 子网myBackendSubnetSubnet: myBackendSubnet
      • 在“公共 IP”中选择“新建”,接着在“创建公共 IP 地址”窗口中针对“SKU”选择“标准”。 Public IP > select Create new, and in the Create public IP address window, for SKU, select Standard
    • 若要创建新的网络安全组(简称 NSG,一种防火墙),请在“网络安全组”下选择“高级”。 To create a new network security group (NSG), a type of firewall, under Network Security Group, select Advanced.

      1. 在“配置网络安全组”字段中,选择“新建”。 In the Configure network security group field, select Create new.
      2. 键入 myNetworkSecurityGroup,然后选择“确定”。 Type myNetworkSecurityGroup, and select OK.
    • 若要使 VM 成为负载均衡器后端池的一部分,请完成以下步骤:To make the VM a part of the Load Balancer's backend pool, complete the following steps:

      • 在“负载均衡”中,对于“将此虚拟机置于现有负载均衡解决方案之后?”,请选择“是” 。In Load Balancing, for Place this virtual machine behind an existing load balancing solution?, select Yes.
      • 在“负载均衡设置”中,为“负载均衡选项”选择“Azure 负载均衡器”。 In Load balancing settings, for Load balancing options, select Azure load balancer.
      • 对于“选择负载均衡器”,请选择“myLoadBalancer”。 For Select a load balancer, myLoadBalancer.
  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. 按步骤额外创建两个具有标准 SKU 公共 IP 地址的 VM - myVM2myVM3,所有其他设置与 myVM1 相同。Follow the steps to create two additional VMs - myVM2 and myVM3, with a Standard SKU public IP address, and all the other settings the same as myVM1.

创建网络安全组规则Create network security group rule

在本部分,我们将创建一个网络安全组规则,以允许使用 HTTP 进行入站连接。In this section, you create a network security group rule to allow inbound connections using HTTP.

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中单击位于“myResourceGroupSLB”资源组中的“myNetworkSecurityGroup”。 Select All services in the left-hand menu, select All resources, and then from the resources list click myNetworkSecurityGroup that is located in the myResourceGroupSLB resource group.
  2. 在“设置”下单击“入站安全规则”,然后单击“添加”。 Under Settings, click Inbound security rules, and then click Add.
  3. 为名为 myHTTPRule 的入站安全规则输入以下值,以允许来自端口 80 的入站 HTTP 连接:Enter these values for the inbound security rule named myHTTPRule to allow for an inbound HTTP connections using port 80:
    • 服务标记 - Service Tag - for Source.
    • Internet - 源服务标记Internet - for Source service tag
    • 80 - 目标端口范围80 - for Destination port ranges
    • TCP - 协议TCP - for Protocol
    • 允许 - 操作Allow - for Action
    • 100 - 优先级100 for Priority
    • myHTTPRule - 名称myHTTPRule for name
    • 允许 HTTP - 说明Allow HTTP - for description
  4. 选择“添加” 。Select Add.

在 VM 上安装 IISInstall IIS on VMs

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

  2. 在“概览”页上单击“连接”,以便通过 RDP 连接到 VM 中。 On the Overview page, click Connect to RDP into the VM.

  3. 在“连接到虚拟机”弹出窗口中选择“下载 RDP 文件”,然后打开已下载的 RDP 文件。 In the Connect to virtual machine pop-up window, select Download RDP File, and then Open the downloaded RDP file.

  4. 在“远程桌面连接”窗口中单击“连接”。 In the Remote Desktop Connection window, click Connect.

  5. 使用在创建此 VM 过程中提供的凭据登录到 VM。Log into the VM with the credentials that you provided during the creation of this VM. 此时会通过虚拟机 myVM1 启动远程桌面会话。This launches a remote desktop session with virtual machine - myVM1.

  6. 在服务器桌面上,导航到“Windows 管理工具” > “Windows PowerShell”。On the server desktop, navigate to Windows Administrative Tools>Windows PowerShell.

  7. 在 PowerShell 窗口中,运行以下命令安装 IIS 服务器,删除默认 iisstart.htm 文件,然后添加显示 VM 名称的新 iisstart.htm 文件:In the PowerShell Window, run the following commands to install the IIS server, remove the default iisstart.htm file, and then add a new iisstart.htm file that displays the name of the VM:

    
     # install IIS server role
     Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
     # remove default htm file
      remove-item  C:\inetpub\wwwroot\iisstart.htm
    
     # Add a new htm file that displays server name
      Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from " + $env:computername)
    
  8. 关闭与 myVM1 之间的 RDP 会话。Close the RDP session with myVM1.

  9. 重复步骤 1 到步骤 6,在 myVM2myVM3 上安装 IIS 和已更新的 iisstart.htm 文件。Repeat steps 1 to 6 to install IIS and the updated iisstart.htm file on myVM2 and myVM3.

测试负载均衡器Test the Load Balancer

  1. 在“概览”屏幕上找到负载均衡器的公共 IP 地址。 Find the public IP address for the Load Balancer on the Overview screen. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后单击“myPublicIP”。 Select All services in the left-hand menu, select All resources, and then click myPublicIP.

  2. 复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。Copy the public IP address, and then paste it into the address bar of your browser. IIS Web 服务器的默认页会显示在浏览器上。The default page of IIS Web server is displayed on the browser.

    IIS Web 服务器

若要查看负载均衡器如何在运行应用的三个 VM 之间分配流量,可以强制刷新 Web 浏览器。To see the Load Balancer distribute traffic across the three VMs running your app, you can force-refresh your web browser.

在后端池中删除或添加 VMRemove or add VMs from the backend pool

建议对运行应用的 VM 执行维护,例如安装 OS 更新。You may need to perform maintenance on the VMs running your app, such as installing OS updates. 若要应对应用增加的流量,建议添加更多 VM。To deal with increased traffic to your app, you may need to add additional VMs. 本部分介绍如何在负载均衡器中删除或添加 VM (myVM1)。This section shows you how to remove or add a VM (myVM1) from the Load Balancer.

从后端池中删除 VMRemove VM from a backend pool

若要从后端池中删除 myVM1,请完成以下步骤:To remove myVM1 from the backend pool, complete the following steps:

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中单击“myLoadBalancer”。 Select All services in the left-hand menu, select All resources, and then click myLoadBalancer from the resources list.
  2. 在“设置”下,单击“后端池”,然后在后端池列表中单击 myBackendPoolUnder Settings, click Backend pools, then within the backend pool's list, click myBackendPool.
  3. 在“myBackendPool”页上,若要删除 VM1,请选择显示“myVM1”的行末尾处的删除图标,然后单击“保存”。 On the myBackendPool page, to remove VM1 select the delete icon at the end of the row that displays myVM1, and then click Save.

myVM1 不再位于后端地址池中时,可以对 myVM1 执行任何维护任务,例如安装软件更新。With myVM1 no longer in the backend address pool, you can perform any maintenance tasks on myVM1, such as installing software updates. 删除 VM1 之后,会在 myVM2myVM3 之间均衡负载。In the absence of VM1, the load is now balanced across myVM2 and myVM3.

将 VM 添加到后端池Add VM to a backend pool

若要将 myVM1 添加到后端池,请完成以下步骤:To add myVM1 back to the backend pool, complete the following steps:

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中选择“myVM1”。 Select All services in the left-hand menu, select All resources, and then select myVM1 from the resources list.
  2. 在“VM1”页中的“设置”下,选择“网络” 。In the VM1 page, under Settings, select Networking.
  3. 在“网络”页中选择“负载均衡”选项卡,然后选择“添加负载均衡”。 In the Networking page, select the Load balancing tab, and then select Add load balancing.
  4. 在“添加负载均衡”页中执行以下操作: In the Add load balancing page, do the following:
    1. 对于“负载均衡选项”,请选择“Azure 负载均衡器” 。For Load balancing options, select Azure load balancer.
    2. 对于“选择负载均衡器”,请选择“myLoadBalancer”。 For Select a load balancer, select myLoadBalancer.
    3. 对于“选择后端池”,请选择“myBackendPool”。 For Select a backend pool, select myBackendPool.

清理资源Clean up resources

如果不再需要本文中创建的资源组、负载均衡器和所有相关资源,可将其删除。When they are no longer needed, delete the resource group, Load Balancer, and all related resources. 为此,请选择包含负载均衡器的“myResouceGroupSLB”资源组,然后选择“删除”。 To do so, select the myResouceGroupSLB resource group that contains the Load Balancer, and then select Delete.