快速入门:使用 Azure 门户创建内部负载均衡器以对 VM 进行负载均衡Quickstart: Create an internal 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 an internal load balancer and two 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.

创建内部负载均衡器时,虚拟网络配置为负载均衡器的网络。When you create an internal load balancer, a virtual network is configured as the network for the load balancer.

虚拟网络中的专用 IP 地址配置为负载均衡器的前端(默认名称为 LoadBalancerFrontend)。A private IP address in the virtual network is configured as the frontend (named as LoadBalancerFrontend by default) for the load balancer.

前端 IP 地址可以是静态的,也可以是动态的 。The frontend IP address can be Static or Dynamic.

创建虚拟网络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 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 myResourceGroupLB created in the previous step.
    名称Name 输入“myLoadBalancer”Enter myLoadBalancer
    区域Region 选择“中国东部 2”。Select China East 2.
    类型Type 选择“内部”。Select Internal.
    SKUSKU 选择“标准”Select Standard
    虚拟网络Virtual network 选择在上一步中创建的 myVNet。Select myVNet created in the previous step.
    子网Subnet 选择在上一步中创建的 myBackendSubnet。Select myBackendSubnet created in the previous step.
    IP 地址分配IP address assignment 选择“动态”。Select Dynamic.
  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.

备注

后端池中的虚拟机将不含具有此配置的出站 Internet 连接。The virtual machines in the backend pool will not have outbound internet connectivity with this configuration.
有关提供出站连接的详细信息,请参阅:For more information on providing outbound connectivity see:
Azure 中的出站连接Outbound connections in Azure
用于提供连接的选项:Options for providing connectivity:
仅出站的负载均衡器配置Outbound-only load balancer configuration
什么是虚拟网络 NAT?What is Virtual Network NAT?

创建后端服务器Create backend servers

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

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

创建虚拟机Create virtual machines

在本部分中,你将创建两个 VM(myVM1 和 myVM2),它们都采用标准公共 IP 地址 。In this section, you'll create two VMs (myVM1 and myVM2) with a standard public IP address.

这些 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
  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 接受“myVM-ip”的默认值。Accept the default of myVM-ip.
    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.
    选择“确定”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 one additional VM with the following values and all the other settings the same as myVM1:

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

创建测试虚拟机Create test virtual machine

在本部分中,你将创建一个名为“myTestVM”的 VM。In this section, you'll create a VM named myTestVM. 此 VM 将用于测试负载均衡器配置。This VM will be used to test the load balancer configuration.

  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 输入“myTestVM”Enter myTestVM
    区域Region 选择“中国东部 2” Select China East 2
    可用性选项Availability Options 选择“无需基础结构冗余”Select No infrastructure redundancy required
    映像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
  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 选择在上一步中创建的 MyNSG。Select MyNSG created in the previous step.
  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.

安装 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,在 myVM2 上安装 IIS 和更新后的 iisstart.htm 文件。Repeat steps 1 to 6 to install IIS and the updated iisstart.htm file on myVM2.

测试负载均衡器Test the load balancer

  1. 在“概述”屏幕上找到负载均衡器的专用 IP 地址。Find the private IP address for the load balancer on the Overview screen. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后选择“myLoadBalancer” 。Select All services in the left-hand menu, select All resources, and then select myLoadBalancer.

  2. 在 myLoadBalancer 的“概览”中,记下或复制专用 IP 地址旁边的地址 。Make note or copy the address next to Private IP Address in the Overview of myLoadBalancer.

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

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

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

  6. 在 myTestVM 中打开 Internet Explorer 。Open Internet Explorer on myTestVM.

  7. 将上一步骤的 IP 地址输入到浏览器的地址栏。Enter the IP address from the previous step into the address bar of the browser. IIS Web 服务器的默认页会显示在浏览器上。The default page of IIS Web server is displayed on the browser.

    创建标准的内部负载均衡器

要查看所有三个 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 Internal Load Balancer
  • 已将两个 VM 连接到负载均衡器。Attached 2 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.