快速入门:使用 Azure CLI 创建内部负载均衡器以对 VM 进行负载均衡Quickstart: Create an internal load balancer to load balance VMs using Azure CLI

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

先决条件Prerequisites

本快速入门需要 Azure CLI 2.0.28 或更高版本。This quickstart requires Azure CLI version 2.0.28 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

创建资源组Create a resource group

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

使用 az group create 创建资源组:Create a resource group with az group create:

  • 命名为“myResourceGroupLB”。Named myResourceGroupLB.
  • 在“chinaeast2”位置。In the chinaeast2 location.
  az group create \
    --name myResourceGroupLB \
    --location chinaeast2

备注

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

配置虚拟网络Configure virtual network

需要先创建支持的虚拟网络资源,然后才能部署 VM 和负载均衡器。Before you deploy VMs and deploy your load balancer, create the supporting virtual network resources.

创建虚拟网络Create a virtual network

使用 az network vnet create 创建虚拟网络:Create a virtual network using az network vnet create:

  • 命名为“myVNet”。Named myVNet.
  • 地址前缀为 10.1.0.0/16。Address prefix of 10.1.0.0/16.
  • 子网命名为“myBackendSubnet”。Subnet named myBackendSubnet.
  • 子网前缀为 10.1.0.0/24。Subnet prefix of 10.1.0.0/24.
  • 在 myResourceGroupLB 资源组中。In the myResourceGroupLB resource group.
  • “chinaeast2”的位置。Location of chinaeast2.
  az network vnet create \
    --resource-group myResourceGroupLB \
    --location chinaeast2 \
    --name myVNet \
    --address-prefixes 10.1.0.0/16 \
    --subnet-name myBackendSubnet \
    --subnet-prefixes 10.1.0.0/24

创建网络安全组Create a network security group

对于标准负载均衡器,后端地址中的 VM 需要具有属于网络安全组的网络接口。For a standard load balancer, the VMs in the backend address for are required to have network interfaces that belong to a network security group.

使用 az network nsg create 创建网络安全组:Create a network security group using az network nsg create:

  • 命名为“myNSG”。Named myNSG.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  az network nsg create \
    --resource-group myResourceGroupLB \
    --name myNSG

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

使用 az network nsg rule create 创建网络安全组规则:Create a network security group rule using az network nsg rule create:

  • 命名为“myNSGRuleHTTP”。Named myNSGRuleHTTP.
  • 在上一步创建的网络安全组“myNSG”中。In the network security group you created in the previous step, myNSG.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 协议为“(*)”。Protocol (*).
  • 方向为“入站”。Direction Inbound.
  • 源为“(*)”。Source (*).
  • 目标为“(*)”。Destination (*).
  • 目标端口为“端口 80”。Destination port Port 80.
  • 访问为“允许”。Access Allow.
  • 优先级为“200”。Priority 200.
  az network nsg rule create \
    --resource-group myResourceGroupLB \
    --nsg-name myNSG \
    --name myNSGRuleHTTP \
    --protocol '*' \
    --direction inbound \
    --source-address-prefix '*' \
    --source-port-range '*' \
    --destination-address-prefix '*' \
    --destination-port-range 80 \
    --access allow \
    --priority 200

为虚拟机创建网络接口Create network interfaces for the virtual machines

使用 az network nic create 创建两个网络接口:Create two network interfaces with az network nic create:

VM1VM1

  • 命名为“myNicVM1”。Named myNicVM1.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在子网“myBackendSubnet”中。In subnet myBackendSubnet.
  • 在网络安全组“myNSG”中。In network security group myNSG.
  az network nic create \
    --resource-group myResourceGroupLB \
    --name myNicVM1 \
    --vnet-name myVNet \
    --subnet myBackEndSubnet \
    --network-security-group myNSG

VM2VM2

  • 命名为“myNicVM2”。Named myNicVM2.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在子网“myBackendSubnet”中。In subnet myBackendSubnet.
  • 在网络安全组“myNSG”中。In network security group myNSG.
  az network nic create \
    --resource-group myResourceGroupLB \
    --name myNicVM2 \
    --vnet-name myVnet \
    --subnet myBackEndSubnet \
    --network-security-group myNSG

创建后端服务器Create backend servers

在本节中,创建以下项:In this section, you create:

  • 用于服务器配置的名为 cloud-init.txt 的云配置文件。A cloud configuration file named cloud-init.txt for the server configuration.
  • 两个要用作负载均衡器后端服务器的虚拟机。Two virtual machines to be used as backend servers for the load balancer.

创建 cloud-init 配置文件Create cloud-init configuration file

使用 cloud-init 配置文件在 Linux 虚拟机上安装 NGINX 并运行“Hello World”Node.js 应用。Use a cloud-init configuration file to install NGINX and run a 'Hello World' Node.js app on a Linux virtual machine.

在当前 shell 中,创建一个名为 cloud-init.txt 的文件。In your current shell, create a file named cloud-init.txt. 复制以下配置并将其粘贴到 shell 中。Copy and paste the following configuration into the shell. 请确保正确复制整个 cloud-init 文件,尤其是第一行:Ensure that you copy the whole cloud-init file correctly, especially the first line:

#cloud-config
package_upgrade: true
packages:
  - nginx
  - nodejs
  - npm
write_files:
  - owner: www-data:www-data
  - path: /etc/nginx/sites-available/default
    content: |
      server {
        listen 80;
        location / {
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection keep-alive;
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
  - owner: azureuser:azureuser
  - path: /home/azureuser/myapp/index.js
    content: |
      var express = require('express')
      var app = express()
      var os = require('os');
      app.get('/', function (req, res) {
        res.send('Hello World from host ' + os.hostname() + '!')
      })
      app.listen(3000, function () {
        console.log('Hello world app listening on port 3000!')
      })
runcmd:
  - service nginx restart
  - cd "/home/azureuser/myapp"
  - npm init
  - npm install express -y
  - nodejs index.js

创建虚拟机Create virtual machines

使用 az vm create 创建虚拟机:Create the virtual machines with az vm create:

VM1VM1

  • 命名为“myVM1”。Named myVM1.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 附加到网络接口“myNicVM1”。Attached to network interface myNicVM1.
  • 虚拟机映像 UbuntuLTS。Virtual machine image UbuntuLTS.
  • 你在上述步骤中创建的配置文件 cloud-init.txt。Configuration file cloud-init.txt you created in step above.
  az vm create \
    --resource-group myResourceGroupLB \
    --name myVM1 \
    --nics myNicVM1 \
    --image UbuntuLTS \
    --admin-user azureuser \
    --generate-ssh-keys \
    --custom-data cloud-init.txt \
    --no-wait
    

VM2VM2

  • 命名为“myVM2”。Named myVM2.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 附加到网络接口“myNicVM2”。Attached to network interface myNicVM2.
  • 虚拟机映像 UbuntuLTS。Virtual machine image UbuntuLTS.
  • 你在上述步骤中创建的配置文件 cloud-init.txt。Configuration file cloud-init.txt you created in step above.
  az vm create \
    --resource-group myResourceGroupLB \
    --name myVM2 \
    --nics myNicVM2 \
    --image UbuntuLTS \
    --admin-user azureuser \
    --generate-ssh-keys \
    --custom-data cloud-init.txt \
    --no-wait

可能需要花费几分钟时间才能部署 VM。It may take a few minutes for the VMs to deploy.

创建标准负载均衡器Create standard load balancer

本部分详细介绍如何创建和配置负载均衡器的以下组件:This section details how you can create and configure the following components of the load balancer:

  • 前端 IP 池,用于在负载均衡器上接收传入网络流量。A frontend IP pool that receives the incoming network traffic on the load balancer.
  • 后端 IP 池,前端池将负载均衡的网络流量发送到此处。A backend IP pool where the frontend pool sends the load balanced network traffic.
  • 运行状况探测,用于确定后端 VM 实例的运行状况。A health probe that determines health of the backend VM instances.
  • 负载均衡器规则,用于定义如何将流量分配给 VM。A load balancer rule that defines how traffic is distributed to the VMs.

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

使用 az network lb create 创建公共负载均衡器:Create a public load balancer with az network lb create:

  • 命名为 myLoadBalancer。Named myLoadBalancer.
  • 前端池命名为 myFrontEnd。A frontend pool named myFrontEnd.
  • 后端池命名为 myBackEndPool。A backend pool named myBackEndPool.
  • 与虚拟网络 myVNet 相关联。Associated with the virtual network myVNet.
  • 与后端子网 myBackendSubnet 相关联。Associated with the backend subnet myBackendSubnet.
  az network lb create \
    --resource-group myResourceGroupLB \
    --name myLoadBalancer \
    --sku Standard \
    --vnet-name myVnet \
    --subnet myBackendSubnet \
    --frontend-ip-name myFrontEnd \
    --backend-pool-name myBackEndPool       

创建运行状况探测Create the health probe

运行状况探测会检查所有虚拟机实例,以确保它们可以发送网络流量。A health probe checks all virtual machine instances to ensure they can send network traffic.

从负载均衡器中删除未通过探测检查的虚拟机。A virtual machine with a failed probe check is removed from the load balancer. 解决故障后,虚拟机将重新添加到负载均衡器中。The virtual machine is added back into the load balancer when the failure is resolved.

使用 az network lb probe create 创建运行状况探测:Create a health probe with az network lb probe create:

  • 监视虚拟机的运行状况。Monitors the health of the virtual machines.
  • 命名为“myHealthProbe”。Named myHealthProbe.
  • 协议为“TCP”。Protocol TCP.
  • 监视“端口 80”。Monitoring Port 80.
  az network lb probe create \
    --resource-group myResourceGroupLB \
    --lb-name myLoadBalancer \
    --name myHealthProbe \
    --protocol tcp \
    --port 80   

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

负载均衡器规则定义:A load balancer rule defines:

  • 针对传入流量的前端 IP 配置。Frontend IP configuration for the incoming traffic.
  • 用于接收流量的后端 IP 池。The backend IP pool to receive the traffic.
  • 所需的源和目标端口。The required source and destination port.

使用 az network lb rule create 创建负载均衡器规则:Create a load balancer rule with az network lb rule create:

  • 命名为“myHTTPRule”Named myHTTPRule
  • 对前端池“myFrontEnd”中的“端口 80”进行侦听 。Listening on Port 80 in the frontend pool myFrontEnd.
  • 使用“端口 80”将负载均衡的网络流量发送到后端地址池“myBackEndPool” 。Sending load-balanced network traffic to the backend address pool myBackEndPool using Port 80.
  • 使用运行状况探测“myHealthProbe”。Using health probe myHealthProbe.
  • 协议为“TCP”。Protocol TCP.
  • 空闲超时 15 分钟。Idle timeout of 15 minutes.
  • 启用 TCP 重置。Enable TCP reset.
  az network lb rule create \
    --resource-group myResourceGroupLB \
    --lb-name myLoadBalancer \
    --name myHTTPRule \
    --protocol tcp \
    --frontend-port 80 \
    --backend-port 80 \
    --frontend-ip-name myFrontEnd \
    --backend-pool-name myBackEndPool \
    --probe-name myHealthProbe \
    --disable-outbound-snat true \
    --idle-timeout 15 \
    --enable-tcp-reset true

备注

后端池中的虚拟机将不含具有此配置的出站 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?

将虚拟机添加到负载均衡器后端池Add virtual machines to load balancer backend pool

使用 az network nic ip-config address-pool add 将虚拟机添加到后端池:Add the virtual machines to the backend pool with az network nic ip-config address-pool add:

VM1VM1

  • 在后端地址池“myBackEndPool”中。In backend address pool myBackEndPool.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 与网络接口 myNicVM1 和 ipconfig1 关联 。Associated with network interface myNicVM1 and ipconfig1.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network nic ip-config address-pool add \
   --address-pool myBackendPool \
   --ip-config-name ipconfig1 \
   --nic-name myNicVM1 \
   --resource-group myResourceGroupLB \
   --lb-name myLoadBalancer

VM2VM2

  • 在后端地址池“myBackEndPool”中。In backend address pool myBackEndPool.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 与网络接口 myNicVM2 和 ipconfig1 关联 。Associated with network interface myNicVM2 and ipconfig1.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network nic ip-config address-pool add \
   --address-pool myBackendPool \
   --ip-config-name ipconfig1 \
   --nic-name myNicVM2 \
   --resource-group myResourceGroupLB \
   --lb-name myLoadBalancer

测试负载均衡器Test the load balancer

创建测试虚拟机Create test virtual machine

使用 az network nic create 创建网络接口:Create the network interface with az network nic create:

  • 命名为 myNicTestVM。Named myNicTestVM.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在子网“myBackendSubnet”中。In subnet myBackendSubnet.
  • 在网络安全组“myNSG”中。In network security group myNSG.
  az network nic create \
    --resource-group myResourceGroupLB \
    --name myNicTestVM \
    --vnet-name myVNet \
    --subnet myBackEndSubnet \
    --network-security-group myNSG

使用 az vm create 创建虚拟机:Create the virtual machine with az vm create:

  • 命名为 myTestVM。Named myTestVM.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 附加到网络接口“myNicTestVM”。Attached to network interface myNicTestVM.
  • 虚拟机映像 Win2019Datacenter。Virtual machine image Win2019Datacenter.
  • 选择 <adminpass> 和 <adminuser> 的值 。Choose values for <adminpass> and <adminuser>.
  az vm create \
    --resource-group myResourceGroupLB \
    --name myTestVM \
    --nics myNicTestVM \
    --image Win2019Datacenter \
    --admin-username <adminuser> \
    --admin-password <adminpass> \
    --no-wait

部署虚拟机需要几分钟时间。Can take a few minutes for the virtual machine to deploy.

测试Test

  1. 登录 Azure 门户。Sign in to the Azure portal.

  2. 在“概述”屏幕上找到负载均衡器的专用 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.

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

  4. 在左侧菜单中选择“所有服务”,选择“所有资源”,然后在资源列表中,选择“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.

  5. 在“概览”页上,选择“连接”,然后选择“RDP” 。On the Overview page, select Connect, then RDP.

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

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

  8. 将上一步骤的 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

如果不再需要资源组、负载均衡器和所有相关的资源,使用 az group delete 命令将它们删除。When no longer needed, use the az group delete command to remove the resource group, load balancer, and all related resources.

  az group delete \
    --name myResourceGroupLB

后续步骤Next steps

在本快速入门中In this quickstart

  • 你创建了一个标准或公共负载均衡器You created a standard or public load balancer
  • 附加了虚拟机。Attached virtual machines.
  • 配置了负载均衡器流量规则和运行状况探测。Configured the load balancer traffic rule and health probe.
  • 测试了负载均衡器。Tested the load balancer.

若要详细了解 Azure 负载均衡器,请继续学习To learn more about Azure Load Balancer, continue to