快速入门:使用 Azure CLI 创建标准负载均衡器以对 VM 进行负载均衡Quickstart: Create a Standard Load Balancer to load balance VMs using Azure CLI

本快速入门演示如何创建公共负载均衡器。This quickstart shows you how to create a public Load Balancer. 为了测试负载均衡器,需要部署两个运行 Ubuntu 服务器的虚拟机 (VM),并在两个 VM 之间对一个 Web 应用进行负载均衡。To test the load balancer, you deploy two virtual machines (VMs) running Ubuntu server and load balance a web app between the two VMs.

本教程要求运行 Azure CLI 2.0.28 或更高版本。This tutorial requires that you are running a version of the 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

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

以下示例在“chinanorth” 位置创建名为“myResourceGroupSLB” 的资源组:The following example creates a resource group named myResourceGroupSLB in the chinanorth location:

  az group create \
    --name myResourceGroupSLB \
    --location chinanorth

创建公共 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 createmyResourceGroupSLB 中创建名为 myPublicIP 的标准公共 IP 地址。Use az network public-ip create to create a Standard Public IP address named myPublicIP in myResourceGroupSLB.

  az network public-ip create --resource-group myResourceGroupSLB --name myPublicIP --sku standard

创建 Azure 负载均衡器Create Azure 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

使用 az network lb create 创建名为 myLoadBalancer 的公共 Azure 负载均衡器,该负载均衡器包括名为 myFrontEnd 的前端池、名为 myBackEndPool 的后端池(与在前一步中创建的公共 IP 地址 myPublicIP 相关联)。Create a public Azure Load Balancer with az network lb create named myLoadBalancer that includes a frontend pool named myFrontEnd, a backend pool named myBackEndPool that is associated with the public IP address myPublicIP that you created in the preceding step. 使用 --sku basic 创建基本公共 IP。Use --sku basic to create a Basic Public IP. Azure 建议将标准 SKU 用于生产工作负荷。Azure recommends Standard SKU for production workloads.

  az network lb create \
    --resource-group myResourceGroupSLB \
    --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 make sure they can send network traffic. 探测器检查失败的虚拟机实例将从负载均衡器中删除,直到它恢复联机状态并且探测器检查确定它运行正常。The virtual machine instance with failed probe checks is removed from the load balancer until it goes back online and a probe check determines that it's healthy. 使用 az network lb probe create 创建运行状况探测,以监视虚拟机的运行状况。Create a health probe with az network lb probe create to monitor the health of the virtual machines.

  az network lb probe create \
    --resource-group myResourceGroupSLB \
    --lb-name myLoadBalancer \
    --name myHealthProbe \
    --protocol tcp \
    --port 80   

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

负载均衡器规则定义传入流量的前端 IP 配置和后端 IP 池以接收流量,同时定义所需源和目标端口。A load balancer rule defines the frontend IP configuration for the incoming traffic and the backend IP pool to receive the traffic, along with the required source and destination port. 使用 az network lb rule create 创建负载均衡器规则 myLoadBalancerRuleWeb,以便侦听前端池 myFrontEnd 中的端口 80,并且将经过负载均衡的网络流量发送到也使用端口 80 的后端地址池 myBackEndPoolCreate a load balancer rule myLoadBalancerRuleWeb with az network lb rule create for listening to port 80 in the frontend pool myFrontEnd and sending load-balanced network traffic to the backend address pool myBackEndPool also using port 80.

  az network lb rule create \
    --resource-group myResourceGroupSLB \
    --lb-name myLoadBalancer \
    --name myHTTPRule \
    --protocol tcp \
    --frontend-port 80 \
    --backend-port 80 \
    --frontend-ip-name myFrontEnd \
    --backend-pool-name myBackEndPool \
    --probe-name myHealthProbe  

配置虚拟网络Configure virtual network

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

创建虚拟网络Create a virtual network

使用 az network vnet createmyResourceGroup 中创建名为 myVnet 的虚拟网络,该虚拟网络包含名为 mySubnet 的子网。Create a virtual network named myVnet with a subnet named mySubnet in the myResourceGroup using az network vnet create.

  az network vnet create \
    --resource-group myResourceGroupSLB \
    --location chinanorth \
    --name myVnet \
    --subnet-name mySubnet

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

对于标准负载均衡器,后端地址池中的 VM 需要具有属于网络安全组的 NIC。For a Standard Load Balancer, the VMs in the backend address for are required to have NICs that belong to a Network Security group. 创建网络安全组,以定义虚拟网络的入站连接。Create network security group to define inbound connections to your virtual network.

  az network nsg create \
    --resource-group myResourceGroupSLB \
    --name myNetworkSecurityGroup

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

创建网络安全组规则,以允许通过端口 80 的入站连接。Create a network security group rule to allow inbound connections through port 80.

  az network nsg rule create \
    --resource-group myResourceGroupSLB \
    --nsg-name myNetworkSecurityGroup \
    --name myNetworkSecurityGroupRuleHTTP \
    --protocol tcp \
    --direction inbound \
    --source-address-prefix '*' \
    --source-port-range '*' \
    --destination-address-prefix '*' \
    --destination-port-range 80 \
    --access allow \
    --priority 200

创建 NICCreate NICs

使用 az network nic create 创建三个网络接口,并将它们与公共 IP 地址和网络安全组关联。Create three network interfaces with az network nic create and associate them with the Public IP address and the network security group.


  az network nic create \
    --resource-group myResourceGroupSLB \
    --name myNicVM1 \
    --vnet-name myVnet \
    --subnet mySubnet \
    --network-security-group myNetworkSecurityGroup \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool

  az network nic create \
    --resource-group myResourceGroupSLB \
    --name myNicVM2 \
    --vnet-name myVnet \
    --subnet mySubnet \
    --network-security-group myNetworkSecurityGroup \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool
  
  az network nic create \
    --resource-group myResourceGroupSLB \
    --name myNicVM3 \
    --vnet-name myVnet \
    --subnet mySubnet \
    --network-security-group myNetworkSecurityGroup \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool

创建后端服务器Create backend servers

本示例将创建三个要用作负载均衡器后端服务器的虚拟机。In this example, you create three virtual machines to be used as backend servers for the load balancer. 若要验证负载均衡器是否已成功创建,还需要在虚拟机上安装 NGINX。To verify that the load balancer was successfully created, you also install NGINX on the virtual machines.

如果创建具有基本公共 IP 的基本负载均衡器,则需使用 az vm availabilityset create 创建可用性集,以便将虚拟机添加到其中。If you're creating a Basic Load Balancer with a Basic Public IP, you will need to create an Availability Set using (az vm availabilityset create to add your virtual machines into. 标准负载均衡器不需要此额外步骤。Standard Load Balancers do not require this additional step. Azure 建议使用标准负载均衡器。Azure recommends using Standard.

创建三个虚拟机Create three virtual machines

可使用 cloud-init 配置文件在 Linux 虚拟机上安装 NGINX 并运行“Hello World”Node.js 应用。You can 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”的文件,并将以下配置复制粘贴到 shell。In your current shell, create a file named cloud-init.txt and copy and paste the following configuration into the shell. 请确保正确复制整个 cloud-init 文件,尤其是第一行:Make sure 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

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


 az vm create \
   --resource-group myResourceGroupSLB \
   --name myVM1 \
   --availability-set myAvailabilitySet \
   --nics myNicVM1 \
   --image UbuntuLTS \
   --generate-ssh-keys \
   --custom-data cloud-init.txt \
   --no-wait
  
 az vm create \
   --resource-group myResourceGroupSLB \
   --name myVM2 \
   --availability-set myAvailabilitySet \
   --nics myNicVM2 \
   --image UbuntuLTS \
   --generate-ssh-keys \
   --custom-data cloud-init.txt \
   --no-wait

  az vm create \
   --resource-group myResourceGroupSLB \
   --name myVM3 \
   --availability-set myAvailabilitySet \
   --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 get deployed.

测试负载均衡器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 myResourceGroupSLB \
    --name myPublicIP \
    --query [ipAddress] \
    --output tsv

测试负载均衡器

清理资源Clean up resources

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

  az group delete --name myResourceGroupSLB