快速入门:使用 Azure 门户创建公共负载均衡器,以便对 VM 进行负载均衡Quickstart: Create a public load balancer to load balance VMs using the Azure portal

使用 Azure 门户创建公共负载均衡器和三个虚拟机,通过这种方式开始使用 Azure 负载均衡器。Get started with Azure Load Balancer by using the Azure portal to create a public load balancer and three virtual machines.

先决条件Prerequisites

登录 AzureSign in to Azure

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


备注

对于生产型工作负载,建议使用标准 SKU 负载均衡器。Standard SKU load balancer is recommended for production workloads. 有关 SKU 的详细信息,请参阅 Azure 负载均衡器 SKUFor more information about SKUs, see Azure Load Balancer SKUs.

在本部分,你将创建一个负载均衡器来对虚拟机进行负载均衡。In this section, you create a load balancer that load balances virtual machines.

可以创建公共负载均衡器或内部负载均衡器。You can create a public load balancer or an internal load balancer.

创建公共负载均衡器时,还会为该负载均衡器创建一个配置为前端(默认情况下命名为“LoadBalancerFrontend”)的新公共 IP 地址。When you create a public load balancer, you create a new public IP address that is configured as the frontend (named as LoadBalancerFrontend by default) for the load balancer.

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

  2. 在“创建负载均衡器”页的“基本信息”选项卡中,输入或选择以下信息:In the Basics tab of the Create load balancer page, enter, or select the following information:

    设置Setting Value
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择“新建”并在文本框中输入 myResourceGroupLB 。Select Create new and enter myResourceGroupLB in the text box.
    名称Name 输入“myLoadBalancer”Enter myLoadBalancer
    区域Region 选择“中国东部 2”。Select China East 2.
    类型Type 选择“公共”。Select Public.
    SKUSKU 选择“标准”Select Standard
    公共 IP 地址Public IP address 选择“新建”。Select Create new. 若要使用现有的公共 IP,请选择“使用现有项”。If you have an existing Public IP you would like to use, select Use existing.
    公共 IP 地址名称Public IP address name 在文本框中键入 myPublicIP。Type myPublicIP in the text box.
    添加一个公共 IPv6 地址Add a public IPv6 address 请选择“否”。Select No.
    有关 IPv6 地址和负载均衡器的详细信息,请参阅什么是适用于 Azure 虚拟网络的 IPv6?For more information on IPv6 addresses and load balancer, see What is IPv6 for Azure Virtual Network?
  3. 接受剩余设置的默认值,然后选择“查看 + 创建”。Accept the defaults for the remaining settings, and then select Review + create.

  4. 在“查看 + 创建”选项卡中,选择“创建”。In the Review + create tab, select Create.

    创建标准负载均衡器

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

在本部分中,你将配置:In this section, you configure:

  • 后端地址池的负载均衡器设置。Load balancer settings for a backend address pool.
  • 运行状况探测。A health probe.
  • 负载均衡器规则。A load balancer rule.

创建后端池Create a backend pool

后端地址池包含连接到负载均衡器的虚拟 (NIC) 的 IP 地址。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 select myLoadBalancer from the resources list.

  2. 在“设置”下,依次选择“后端池”、“添加”。Under Settings, select Backend pools, then select 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

负载均衡器使用运行状况探测器监视应用的状态。The load balancer monitors the status of your app with a health probe.

运行状况探测器基于 VM 对运行状况检查的响应,在负载均衡器中添加或删除 VM。The health probe adds or removes VMs from the load balancer based on their response to health checks.

创建名为 myHealthProbe 的运行状况探测来监视 VM 的运行状况。Create a health probe named myHealthProbe to monitor the health of the VMs.

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

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

    设置Setting Value
    名称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.
  3. 将剩余的字段保留默认设置,然后选择“确定”。Leave the rest the defaults and 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. 源端口和目标端口在规则中定义。The source and destination port are defined in the rule.

在本部分中,你将创建创建负载均衡器规则:In this section, you'll create a load balancer rule:

  • 该规则名为“myHTTPRule”。Named myHTTPRule.
  • 在名为“LoadBalancerFrontEnd”的前端中。In the frontend named LoadBalancerFrontEnd.
  • 正在侦听“端口 80”。Listening on Port 80.
  • 将负载均衡流量定向到“端口 80”上名为“myBackendPool”的后端 。Directs load balanced traffic to the backend named myBackendPool on Port 80.
  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中选择“myLoadBalancer”。Select All services in the left-hand menu, select All resources, and then select myLoadBalancer from the resources list.

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

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

    设置Setting Value
    名称Name 输入 myHTTPRuleEnter myHTTPRule.
    IP 版本IP Version 选择“IPv4”Select IPv4
    前端 IP 地址Frontend IP address 选择“LoadBalancerFrontEnd”Select LoadBalancerFrontEnd
    协议Protocol 选择“TCP”。Select TCP.
    端口Port 输入 80Enter 80.
    后端端口Backend port 输入 80Enter 80.
    后端池Backend pool 选择“myBackendPool”。Select myBackendPool.
    运行状况探测Health probe 选择“myHealthProbe”。Select myHealthProbe.
    创建隐式出站规则Create implicit outbound rules 请选择“否”。Select No.
  4. 将剩余的字段保留默认设置,然后选择“确定”。Leave the rest of the defaults and then select OK.

创建后端服务器Create backend servers

本部分的操作:In this section, you:

  • 创建虚拟网络。Create a virtual network.
  • 为负载均衡器的后端池创建三个虚拟机。Create three virtual machines for the backend pool of the load balancer.
  • 在虚拟机上安装 IIS 以测试负载均衡器。Install IIS on the virtual machines to test the load balancer.

创建虚拟网络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 选择“myResourceGroupLB”Select myResourceGroupLB
    实例详细信息Instance details
    名称Name 输入“myVNet”Enter myVNet
    区域Region 选择“中国东部 2” Select China East 2
  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 输入“10.1.0.0/16”Enter 10.1.0.0/16
  5. 在“子网名称”下,选择词语“默认”。Under Subnet name, select the word default.

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

    设置Setting Value
    子网名称Subnet name 输入“myBackendSubnet”Enter myBackendSubnet
    子网地址范围Subnet address range 输入“10.1.0.0/24”Enter 10.1.0.0/24
  7. 选择“保存” 。Select Save.

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

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

创建虚拟机Create virtual machines

在本部分中,你将创建三个 VM(“myVM1”、“myVM2”和“myVM3”) 。In this section, you'll create three VMs (myVM1, myVM2 and myVM3).

这些 VM 将添加到先前创建的负载均衡器的后端池中。These VMs are added to the backend pool of the load balancer that was created earlier.

  1. 在门户的左上方,选择“创建资源” > “计算” > “虚拟机” 。On the upper-left side of the portal, select Create a resource > Compute > Virtual machine.

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

    设置Setting Value
    项目详细信息Project Details
    订阅Subscription 选择 Azure 订阅Select your Azure subscription
    资源组Resource Group 选择“myResourceGroupLB”Select myResourceGroupLB
    实例详细信息Instance details
    虚拟机名称Virtual machine name 输入“myVM1”Enter myVM1
    区域Region 选择“中国东部 2” Select China East 2
    映像Image 选择“Windows Server 2019 Datacenter”Select Windows Server 2019 Datacenter
    大小Size 选择 VM 大小或采用默认设置Choose VM size or take default setting
    管理员帐户Administrator account
    用户名Username 输入用户名Enter a username
    密码Password 输入密码Enter a password
    确认密码Confirm password 重新输入密码Reenter password
    入站端口规则Inbound port rules
    公共入站端口Public inbound ports 选择“无”Select None
  3. 选择“网络”选项卡,或选择“下一步: 磁盘”,然后选择“下一步: 网络”。Select the Networking tab, or select Next: Disks, then Next: Networking.

  4. 在“网络”选项卡中,选择或输入:In the Networking tab, select or enter:

    设置Setting Value
    网络接口Network interface
    虚拟网络Virtual network myVNetmyVNet
    子网Subnet myBackendSubnetmyBackendSubnet
    公共 IPPublic IP 选择“无”。Select None.
    NIC 网络安全组NIC network security group 选择“高级”Select Advanced
    配置网络安全组Configure network security group 选择“新建”。Select Create new.
    在“创建网络安全组”中,在“名称”中输入“myNSG” 。In the Create network security group, enter myNSG in Name.
    在“入站规则”下,选择“+ 添加入站规则” 。Under Inbound rules, select +Add an inbound rule.
    在“目标端口范围”下,输入“80” 。Under Destination port ranges, enter 80.
    在“优先级”下,输入“100” 。Under Priority, enter 100.
    在“名称”中,输入“myHTTPRule”In Name, enter myHTTPRule
    选择“添加”Select Add
    选择“确定”Select OK
    负载均衡Load balancing
    是否将此虚拟机置于现有负载均衡解决方案之后?Place this virtual machine behind an existing load balancing solution? 选择“是”Select Yes
    负载均衡设置Load balancing settings
    负载均衡选项Load balancing options 选择“Azure 负载均衡”Select Azure load balancing
    选择负载均衡器Select a load balancer 选择“myLoadBalancer”Select myLoadBalancer
    选择后端池Select a backend pool 选择“myBackendPool”Select myBackendPool
  5. 选择“管理”选项卡,或者选择“下一步” > “管理”。Select the Management tab, or select Next > Management.

  6. 在“管理”选项卡中,选择或输入:In the Management tab, select or enter:

    设置Setting Value
    MonitoringMonitoring
    启动诊断Boot diagnostics 选择“关闭”Select Off
  7. 选择“查看 + 创建”。Select Review + create.

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

  9. 按照第 1 到第 8 步,使用以下值创建两个 VM,所有其他设置与 myVM1 相同:Follow the steps 1 to 8 to create two additional VMs with the following values and all the other settings the same as myVM1:

    设置Setting VM 2VM 2 VM 3VM 3
    名称Name myVM2myVM2 myVM3myVM3
    网络安全组Network security group 选择现有的“myNSG”Select the existing myNSG 选择现有的“myNSG”Select the existing myNSG

创建出站规则配置Create outbound rule configuration

负载均衡器出站规则为后端池中的 VM 配置出站 SNAT。Load balancer outbound rules configure outbound SNAT for VMs in the backend pool.

有关出站连接的详细信息,请参阅 Azure 中的出站连接For more information on outbound connections, see Outbound connections in Azure.

创建出站规则Create outbound rule

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

  2. 在“设置”下,依次选择“出站规则”、“添加”。Under Settings, select Outbound rules, then select Add.

  3. 使用以下值配置出站规则:Use these values to configure the outbound rules:

    设置Setting Value
    名称Name 输入 myOutboundRule。Enter myOutboundRule.
    前端 IP 地址Frontend IP address 选择“新建”。Select Create new.
    在“名称”中输入“LoadBalancerFrontEndOutbound”。In Name, enter LoadBalancerFrontEndOutbound.
    选择“IP 地址”或“IP 前缀”。Select IP address or IP prefix.
    在“公共 IP 地址”或“公共 IP 前缀”下选择“新建”。Select Create new under Public IP address or Public IP prefix.
    对于“名称”,请输入“myPublicIPOutbound”或“myPublicIPPrefixOutbound”。For Name, enter myPublicIPOutbound or myPublicIPPrefixOutbound.
    选择“添加” 。Select Add.
    空闲超时(分钟)Idle timeout (minutes) 将滑块移动到 15 分钟。Move slider to 15 minutes.
    TCP 重置TCP Reset 选择“启用”。Select Enabled.
    后端池Backend pool 选择“新建”。Select Create new.
    在“名称”中输入“myBackendPoolOutbound”。Enter myBackendPoolOutbound in Name.
    选择 添加Select Add.
    端口分配 -> 端口分配Port allocation -> Port allocation 选择“手动选择出站端口数”Select Manually choose number of outbound ports
    出站端口 -> 选择方式Outbound ports -> Choose by 选择“每个实例的端口数”Select Ports per instance
    出站端口 -> 每个实例的端口数Outbound ports -> Ports per instance 输入“10000”。Enter 10000.
  4. 选择 添加Select Add.

向出站池添加虚拟机Add virtual machines to outbound pool

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

  2. 在“设置”下,选择“后端池”。 Under Settings, select Backend pools.

  3. 选择“myBackendPoolOutbound”。Select myBackendPoolOutbound.

  4. 在“虚拟网络”中,选择“myVNet”。In Virtual network, select myVNet.

  5. 在“虚拟机”中,选择“+ 添加”。In Virtual machines, select + Add.

  6. 选中“myVM1”、“myVM2”和“myVM3”旁边的框。Check the boxes next to myVM1, myVM2, and myVM3.

  7. 选择“添加” 。Select Add.

  8. 选择“保存” 。Select Save.

安装 IISInstall IIS

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

  2. 在“概览”页上,选择“连接”。********On the Overview page, select Connect.

  3. 输入在 VM 创建过程中输入的用户名和密码。Enter the username and password entered during VM creation.

  4. 选择“连接”。Select Connect.

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

  6. 在 PowerShell 窗口中,运行以下命令以:In the PowerShell Window, run the following commands to:

    • 安装 IIS 服务器Install the IIS server
    • 删除默认的 iisstart.htm 文件Remove the default iisstart.htm file
    • 添加显示 VM 名称的新 iisstart.htm 文件: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)
    
  7. 关闭与 myVM1 之间的 RDP 会话。Close the RDP session with myVM1.

  8. 重复步骤 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 select 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 中的负载均衡器分配流量,可以自定义每个 VM 的 IIS Web 服务器的默认页面,然后从客户端计算机强制刷新 Web 浏览器。To see the load balancer distribute traffic across all three VMs, you can customize the default page of each VM's IIS Web server and then force-refresh your web browser from the client machine.

清理资源Clean up resources

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

后续步骤Next steps

在本快速入门中,请执行以下操作:In this quickstart, you:

  • 已创建 Azure 标准或基本负载均衡器Created an Azure Standard or Basic Load Balancer
  • 已将 3 个 VM 连接到负载均衡器。Attached 3 VMs to the load balancer.
  • 已配置负载均衡器流量规则、运行状况探测器,然后测试负载均衡器。Configured the load balancer traffic rule, health probe, and then tested the load balancer.

若要了解有关 Azure 负载均衡器的更多信息,请进一步阅读什么是 Azure 负载均衡器?负载均衡器常见问题To learn more about Azure Load Balancer, continue to What is Azure Load Balancer? and Load Balancer frequently asked questions.