快速入门:使用 Azure CLI 创建公共负载均衡器以对 VM 进行负载均衡Quickstart: Create a public 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.

创建公共 IP 地址Create a public IP address

若要通过 Internet 访问 Web 应用,需要负载均衡器有一个公共 IP 地址。To access your web app on the Internet, you need a public IP address for the load balancer.

使用 az network public-ip create 执行以下操作:Use az network public-ip create to:

  • 创建名为“myPublicIP”的标准公共 IP 地址。Create a standard public IP address named myPublicIP.
  • 在“myResourceGroupLB”中。In myResourceGroupLB.
  az network public-ip create \
    --resource-group myResourceGroupLB \
    --name myPublicIP \
    --sku Standard

创建标准负载均衡器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.
  • 与你在上一步中创建的公共 IP 地址 myPublicIP 关联。Associated with the public IP address myPublicIP that you created in the preceding step.
  az network lb create \
    --resource-group myResourceGroupLB \
    --name myLoadBalancer \
    --sku Standard \
    --public-ip-address myPublicIP \
    --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.
  • 使用前端 IP 地址启用出站源网络地址转换 (SNAT)。Enable outbound source network address translation (SNAT) using the frontend IP address.
  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 

配置虚拟网络Configure virtual network

需要先创建支持的虚拟网络资源,然后才能部署 VM 和测试负载均衡器。Before you deploy VMs and test 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.
  • 子网命名为“myBackendSubnet”。Subnet named myBackendSubnet.
  • 在 myResourceGroupLB 资源组中。In the myResourceGroupLB resource group.
  • “chinaeast2”的位置。Location of chinaeast2.
  az network vnet create \
    --resource-group myResourceGroupLB \
    --location chinaeast2 \
    --name myVNet \
    --subnet-name myBackendSubnet

创建网络安全组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.
  • 协议为“TCP”。Protocol TCP.
  • 方向为“入站”。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 tcp \
    --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 three 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.
  • 附加到 myBackEndPool 中的负载均衡器 myLoadBalancer 。Attached to load balancer myLoadBalancer in myBackEndPool.

  az network nic create \
    --resource-group myResourceGroupLB \
    --name myNicVM1 \
    --vnet-name myVNet \
    --subnet myBackEndSubnet \
    --network-security-group myNSG \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool

VM2VM2

  • 命名为“myNicVM2”。Named myNicVM2.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在子网“myBackendSubnet”中。In subnet myBackendSubnet.
  • 在网络安全组“myNSG”中。In network security group myNSG.
  • 附加到 myBackEndPool 中的负载均衡器 myLoadBalancer 。Attached to load balancer myLoadBalancer in myBackEndPool.
  az network nic create \
    --resource-group myResourceGroupLB \
    --name myNicVM2 \
    --vnet-name myVnet \
    --subnet myBackEndSubnet \
    --network-security-group myNSG \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool

VM3VM3

  • 命名为“myNicVM3”。Named myNicVM3.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在子网“myBackendSubnet”中。In subnet myBackendSubnet.
  • 在网络安全组“myNSG”中。In network security group myNSG.
  • 附加到 myBackEndPool 中的负载均衡器 myLoadBalancer 。Attached to load balancer myLoadBalancer in myBackEndPool.
  az network nic create \
    --resource-group myResourceGroupLB \
    --name myNicVM3 \
    --vnet-name myVnet \
    --subnet myBackEndSubnet \
    --network-security-group myNSG \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool

创建后端服务器Create backend servers

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

  • 用于服务器配置的名为 cloud-init.txt 的云配置文件。A cloud configuration file named cloud-init.txt for the server configuration.
  • 三个要用作负载均衡器后端服务器的虚拟机。Three 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 \
    --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 \
    --generate-ssh-keys \
    --custom-data cloud-init.txt \
    --no-wait

VM3VM3

  • 命名为“myVM3”。Named myVM3.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 附加到网络接口“myNicVM3”。Attached to network interface myNicVM3.
  • 虚拟机映像 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 myVM3 \
    --nics myNicVM3 \
    --image UbuntuLTS \
    --generate-ssh-keys \
    --custom-data cloud-init.txt \
    --no-wait

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

创建出站规则配置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.

创建出站公共 IP 地址或公共 IP 前缀。Create outbound public IP address or public IP prefix.

使用 az network public-ip create 为出站连接创建单个 IP。Use az network public-ip create to create a single IP for the outbound connectivity.

使用 az network public-ip create 为出站连接创建公共 IP 前缀。Use az network public-ip prefix create to create a public IP prefix for the outbound connectivity.

有关缩放出站 NAT 和出站连接的详细信息,请参阅使用多个 IP 地址缩放出站 NATFor more information on scaling outbound NAT and outbound connectivity, see Scale outbound NAT with multiple IP addresses.

公共 IPPublic IP

  • 命名为 myPublicIPOutbound。Named myPublicIPOutbound.
  • 在“myResourceGroupLB”中。In myResourceGroupLB.
  az network public-ip create \
    --resource-group myResourceGroupLB \
    --name myPublicIPOutbound \
    --sku Standard

公共 IP 前缀Public IP prefix

  • 命名为 myPublicIPPrefixOutbound。Named myPublicIPPrefixOutbound.
  • 在“myResourceGroupLB”中。In myResourceGroupLB.
  • 前缀长度为 28。Prefix length of 28.
  az network public-ip prefix create \
    --resource-group myResourceGroupLB \
    --name myPublicIPPrefixOutbound \
    --length 28

创建出站前端 IP 配置Create outbound frontend IP configuration

使用 az network lb frontend-ip create 创建新的前端 IP 配置:Create a new frontend IP configuration with az network lb frontend-ip create:

根据上一步中的决定选择公共 IP 或公共 IP 前缀命令。Select the public IP or public IP prefix commands based on decision in previous step.

公共 IPPublic IP

  • 命名为“myFrontEndOutbound”。Named myFrontEndOutbound.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 与公共 IP 地址 myPublicIPOutbound 关联。Associated with public IP address myPublicIPOutbound.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network lb frontend-ip create \
    --resource-group myResourceGroupLB \
    --name myFrontEndOutbound \
    --lb-name myLoadBalancer \
    --public-ip-address myPublicIPOutbound 

公共 IP 前缀Public IP prefix

  • 命名为“myFrontEndOutbound”。Named myFrontEndOutbound.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 与公共 IP 前缀 myPublicIPPrefixOutbound 关联。Associated with public IP prefix myPublicIPPrefixOutbound.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network lb frontend-ip create \
    --resource-group myResourceGroupLB \
    --name myFrontEndOutbound \
    --lb-name myLoadBalancer \
    --public-ip-prefix myPublicIPPrefixOutbound 

创建出站池Create outbound pool

使用 az network lb address create 创建新的出站池:Create a new outbound pool with az network lb address-pool create:

  • 命名为“myBackEndPoolOutbound”。Named myBackEndPoolOutbound.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network lb address-pool create \
    --resource-group myResourceGroupLB \
    --lb-name myLoadBalancer \
    --name myBackendPoolOutbound

创建出站规则Create outbound rule

使用 az network lb outbound-rule create 为出站后端池创建新的出站规则:Create a new outbound rule for the outbound backend pool with az network lb outbound-rule create:

  • 命名为“myOutboundRule”。Named myOutboundRule.
  • 在资源组“myResourceGroupLB”中。In resource group myResourceGroupLB.
  • 与负载均衡器 myLoadBalancer 关联Associated with load balancer myLoadBalancer
  • 与前端 myFrontEndOutbound 关联。Associated with frontend myFrontEndOutbound.
  • 协议为“所有”。Protocol All.
  • 空闲超时时间为 15。Idle timeout of 15.
  • 出站端口为 10000 个。10000 outbound ports.
  • 与后端池 myBackEndPoolOutbound 关联。Associated with backend pool myBackEndPoolOutbound.
  az network lb outbound-rule create \
    --resource-group myResourceGroupLB \
    --lb-name myLoadBalancer \
    --name myOutboundRule \
    --frontend-ip-configs myFrontEndOutbound \
    --protocol All \
    --idle-timeout 15 \
    --outbound-ports 10000 \
    --address-pool myBackEndPoolOutbound

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

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

VM1VM1

  • 在后端地址池“myBackEndPoolOutbound”中。In backend address pool myBackEndPoolOutbound.
  • 在资源组“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 myBackendPoolOutbound \
   --ip-config-name ipconfig1 \
   --nic-name myNicVM1 \
   --resource-group myResourceGroupLB \
   --lb-name myLoadBalancer

VM2VM2

  • 在后端地址池“myBackEndPoolOutbound”中。In backend address pool myBackEndPoolOutbound.
  • 在资源组“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 myBackendPoolOutbound \
   --ip-config-name ipconfig1 \
   --nic-name myNicVM2 \
   --resource-group myResourceGroupLB \
   --lb-name myLoadBalancer

VM3VM3

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

测试负载均衡器Test the load balancer

若要获取负载均衡器的公共 IP 地址,请使用 az network public-ip showTo get the public IP address of the load balancer, use az network public-ip show.

复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。Copy the public IP address, and then paste it into the address bar of your browser.

  az network public-ip show \
    --resource-group myResourceGroupLB \
    --name myPublicIP \
    --query [ipAddress] \
    --output tsv

测试负载均衡器

清理资源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 负载均衡器的更多信息,请进一步阅读什么是 Azure 负载均衡器?负载均衡器常见问题To learn more about Azure Load Balancer, continue to What is Azure Load Balancer? and Load Balancer frequently asked questions.