快速入门:使用 Azure 门户创建基本负载均衡器Quickstart: Create a Basic Load Balancer by using 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 load balancer and balance traffic among VMs. 本快速入门介绍如何创建并配置负载均衡器、后端服务器以及“基本”定价层的网络资源。This quickstart shows you how to create and configure a load balancer, back-end servers, and network resources at the Basic pricing tier.

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

若要执行本快速入门中的任务,请登录 Azure 门户To do the tasks in this quickstart, sign in to the Azure portal.

创建基本负载均衡器Create a Basic Load Balancer

首先,使用门户创建公共基本负载均衡器。First, create a public Basic Load Balancer by using the portal. 创建的名称和公共 IP 地址自动配置为负载均衡器的前端。The name and public IP address you create are automatically configured as the load balancer's front end.

  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 选择“新建”并在文本框中键入 MyResourceGroupLB。Select Create new and type MyResourceGroupLB in the text box.
    NameName myLoadBalancermyLoadBalancer
    区域Region 选择“中国北部”。Select China North.
    类型Type 选择“公共”。Select Public.
    SKUSKU 选择“基本”。Select Basic.
    公共 IP 地址Public IP address 选择“新建”。Select Create new.
    公共 IP 地址名称Public IP address name MyPublicIPMyPublicIP
    分配Assignment 静态Static
  3. 在“查看 + 创建”选项卡中,单击“创建”。In the Review + create tab, click Create.

创建后端服务器Create back-end servers

接下来,请创建一个虚拟网络,并为基本负载均衡器的后端池创建两个虚拟机。Next, create a virtual network and two virtual machines for the back-end pool of your Basic 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: Drop down Select existing and select MyResourceGroupLB.
    • 子网 > “名称”:键入“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
    • 公共 IPMyVM1-ipPublic IP: MyVM1-ip

    若要创建新的网络安全组(简称 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.
  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,其“公共 IP”地址为 MyVM2-ip,所有其他设置与 MyVM1 相同。Follow the steps to create a second VM named MyVM2, with a Public IP address of MyVM2-ip, and all the other settings the same as MyVM1.

为 VM 创建 NSG 规则Create NSG rules for the VMs

在本部分,请为 VM 创建网络安全组 (NSG) 规则,允许入站 Internet (HTTP) 和远程桌面 (RDP) 连接。In this section, you create network security group (NSG) rules for the VMs, to allow inbound internet (HTTP) and remote desktop (RDP) connections.

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

  2. 在“设置”下,依次选择“入站安全规则”、“添加”。Under Settings, select Inbound security rules, and then select Add.

  3. 在“添加入站安全规则”对话框中,键入或选择以下内容作为 HTTP 规则:In the Add inbound security rule dialog, for the HTTP rule, type or select the following:

    • :选择“服务标记”。Source: Select Service Tag.
    • 源服务标记:选择“Internet”。Source service tag: Select Internet.
    • 目标端口范围:键入 80。Destination port ranges: Type 80.
    • 协议:选择“TCP”。Protocol: Select TCP.
    • 操作:选择“允许”。Action: Select Allow.
    • 优先级:键入 100。Priority: Type 100.
    • 名称:键入“MyHTTPRule”。Name: Type MyHTTPRule.
    • 说明:键入“允许 HTTP”。Description: Type Allow HTTP.
  4. 选择“设置” (应用程序对象和服务主体对象)。Select Add.

    创建 NSG 规则

  5. 重复入站 RDP 规则的步骤,使用下述不同的值:Repeat the steps for the inbound RDP rule, with the following differing values:

    • 目标端口范围:键入“3389”。Destination port ranges: Type 3389.
    • “优先级”:键入“200”。Priority: Type 200.
    • 名称:键入 MyRDPRuleName: Type MyRDPRule.
    • 说明:键入“允许 RDP”。Description: Type Allow RDP.

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

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

创建后端地址池Create a backend 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 each virtual machine (MyVM1 and MyVM2) that you created to the back-end pool.
    2. 添加每台计算机以后,请在下拉列表中选择其网络 IP 配置After you add each machine, drop down and select its Network IP configuration.
  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:

    • 名称:键入 MyLoadBalancerRuleName: Type MyLoadBalancerRule.
    • 前端 IP 地址:键入 LoadBalancerFrontendFrontend IP address: Type LoadBalancerFrontend.
    • 协议:选择“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

需使用公共 IP 地址在 VM 上测试负载均衡器。You'll use the public IP address to test the load balancer on the VMs.

在门户中的 MyLoadBalancer 的“概览”页上,在“公共 IP 地址”下找到其公共 IP 地址。In the portal, on the Overview page for MyLoadBalancer, find its public IP address under Public IP Address. 将鼠标悬停在地址上,选择“复制”图标对其进行复制。Hover over the address and select the Copy icon to copy it.

在 VM 上安装 IISInstall IIS on the VMs

在虚拟机上安装 Internet Information Services (IIS) 以帮助测试负载均衡器。Install Internet Information Services (IIS) on the virtual machines to help test the load balancer.

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

  1. 在门户的左侧菜单中选择“所有资源”。In the portal, select All resources on the left menu. 在资源列表中,选择“MyResourceGroupLB”资源组中的“MyVM1”。From the resource list, select MyVM1 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 select OK.

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

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

安装 IISTo install IIS

  1. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中选择位于“myResourceGroupSLB”资源组中的“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 myResourceGroupSLB resource group.

  2. 在“概述”页上选择“连接”,以通过 RDP 连接到 VM。On the Overview page, select Connect to RDP into the VM.

  3. 使用在创建此 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.

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

  5. 在 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)
    
  6. 关闭与 myVM1 之间的 RDP 会话。Close the RDP session with myVM1.

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

  8. 对虚拟机 MyVM2 重复这些步骤,唯一例外是将目标服务器设置为 MyVM2Repeat the steps for the virtual machine MyVM2, except set the destination server to MyVM2.

测试负载均衡器Test the load balancer

打开浏览器并将负载均衡器的公共 IP 地址粘贴到浏览器的地址栏中。Open a browser and paste your load balancer's public IP address into the browser's address bar. IIS Web 服务器默认页会显示在浏览器中。The IIS web server default page should appear in the browser.

IIS Web 服务器

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

清理资源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.

后续步骤Next steps

在本快速入门中,你创建了一个基本层负载均衡器。In this quickstart, you created a Basic-tier load balancer. 你创建并配置了资源组、网络资源、后端服务器、运行状况探测,以及适用于负载均衡器的规则。You created and configured a resource group, network resources, back-end servers, a health probe, and rules to use with the load balancer. 你在 VM 上安装了 IIS,并使用它来测试负载均衡器。You installed IIS on the VMs and used it to test the load balancer.

若要了解有关 Azure 负载均衡器的详细信息,请继续学习相关教程。To learn more about Azure Load Balancer, continue to the tutorials.