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

本文介绍如何创建内部负载均衡器,以便对 VM 进行负载均衡。This article shows you how to create an internal load balancer to load balance VMs. 为了测试负载均衡器,请部署两个运行 Ubuntu 服务器的虚拟机 (VM),以便对 Web 应用进行负载均衡。To test the load balancer, you deploy two virtual machines (VMs) running Ubuntu server to load balance a web app.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

本教程要求运行 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 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.

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

  az group create \
    --name myResourceGroupILB \
    --location chinaeast

创建虚拟网络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 \
    --name myVnet \
    --resource-group myResourceGroupILB \
    --location chinaeast \
    --subnet-name mySubnet

创建标准负载均衡器Create Standard Load Balancer

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

  • 前端 IP 配置,用于在负载均衡器上接收传入网络流量。a frontend IP configuration 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”的内部负载均衡器,该负载均衡器包括名为“myFrontEnd”的前端 IP 配置,以及名为“myBackEndPool”的后端池(与专用 IP 地址 10.0.0.7 相关联)。Create an internal Load Balancer with az network lb create named myLoadBalancer that includes a frontend IP configuration named myFrontEnd, a back-end pool named myBackEndPool that is associated with a private IP address 10.0.0.7.

使用 --sku basic 创建基本负载均衡器。Use --sku basic to create a Basic Load Balancer. Azure 建议将标准 SKU 用于生产工作负荷。Azure recommends Standard SKU for production workloads.

  az network lb create \
    --resource-group myResourceGroupILB \
    --name myLoadBalancer \
    --sku standard \
    --frontend-ip-name myFrontEnd \
    --private-ip-address 10.0.0.7 \
    --backend-pool-name myBackEndPool \
    --vnet-name myVnet \
    --subnet mySubnet      

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

运行状况探测会检查所有虚拟机实例,以确保它们可以接收网络流量。A health probe checks all virtual machine instances to make sure they can receive 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 myResourceGroupILB \
    --lb-name myLoadBalancer \
    --name myHealthProbe \
    --protocol tcp \
    --port 80

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

负载均衡器规则定义传入流量的前端 IP 配置和用于接收流量的后端 IP 池,以及所需源和目标端口。A load balancer rule defines the front-end IP configuration for the incoming traffic and the back-end IP pool to receive the traffic, along with the required source and destination port. 使用 az network lb rule create 创建负载均衡器规则 myHTTPRule,以便侦听前端池 myFrontEnd 中的端口 80,并且将经过负载均衡的网络流量发送到也使用端口 80 的后端地址池 myBackEndPoolCreate a load balancer rule myHTTPRule 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 myResourceGroupILB \
    --lb-name myLoadBalancer \
    --name myHTTPRule \
    --protocol tcp \
    --frontend-port 80 \
    --backend-port 80 \
    --frontend-ip-name myFrontEnd \
    --backend-pool-name myBackEndPool \
    --probe-name myHealthProbe  

还可以使用下面的配置通过标准负载均衡器创建 HA 端口负载均衡器规则。You can also create an HA ports load balancer rule using configuration below with Standard Load Balancer.

az network lb rule create --resource-group myResourceGroupILB --lb-name myLoadBalancer --name haportsrule --protocol all --frontend-port 0 --backend-port 0 --frontend-ip-name myFrontEnd --backend-address-pool-name myBackEndPool

为后端地址池创建服务器Create servers for the backend address pool

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

创建 NICCreate NICs

使用 az network nic create 创建两个网络接口,并将它们与专用 IP 地址关联。Create two network interfaces with az network nic create and associate them with the private IP address.

for i in `seq 1 2`; do
  az network nic create \
    --resource-group myResourceGroupILB \
    --name myNic$i \
    --vnet-name myVnet \
    --subnet mySubnet \
    --lb-name myLoadBalancer \
    --lb-address-pools myBackEndPool
done

创建后端服务器Create backend servers

本示例将创建两个要用作负载均衡器后端服务器的虚拟机。In this example, you create two 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.

创建可用性集Create an Availability set

使用 az vm availabilityset create 创建可用性集Create an availability set with az vm availabilityset create

 az vm availability-set create \
   --resource-group myResourceGroupILB \
   --name myAvailabilitySet

创建两个虚拟机Create two 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.

for i in `seq 1 2`; do
 az vm create \
   --resource-group myResourceGroupILB \
   --name myVM$i \
   --availability-set myAvailabilitySet \
   --nics myNic$i \
   --image UbuntuLTS \
   --generate-ssh-keys \
   --custom-data cloud-init.txt
   done

VM 可能需要几分钟才能部署好。It may take a few minutes for the VMs to get deployed.

创建用于测试负载均衡器的 VMCreate a VM for testing the load balancer

若要测试负载均衡器,请创建虚拟机 myVMTest 并将其关联到 myNic3To test the load balancer, create a virtual machine, myVMTest, and associate it to myNic3.

 az vm create \
    --resource-group myResourceGroupILB \
    --name myVMTest \
    --image win2016datacenter \
    --admin-username azureuser \
    --admin-password myPassword123456!

测试内部负载均衡器Test the internal load balancer

若要测试负载均衡器,必须先获得负载均衡器的专用 IP 地址。To test the load balancer, you must first obtain the private IP address of the load balancer. 接下来,登录到虚拟机 myVMTest,然后在 Web 浏览器的地址栏中键入专用 IP 地址。Next, sign in to virtual machine myVMTest, and type the private IP address into the address bar of its web browser.

若要获取负载均衡器的专用 IP 地址,请使用 az network lb showTo get the private IP address of the load balancer, use az network lb show. 复制该专用 IP 地址,然后将其粘贴到虚拟机 myVMTest 的 Web 浏览器的地址栏中。Copy the private IP address, and then paste it into the address bar of a web browser of your virtual machine - myVMTest.

  az network lb show \
    --name myLoadBalancer \
    --resource-group myResourceGroupILB

测试负载均衡器

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

后续步骤Next steps

本文介绍了如何创建内部基本负载平衡器,向其附加 VM,配置负载均衡器流量规则、运行状况探测,然后测试负载均衡器。In this article, you created an internal Basic Load Balancer, attached VMs to it, configured the load balancer traffic rule, health probe, and then tested the load balancer. 若要详细了解负载均衡器及其关联的资源,请继续阅读操作方法文章。To learn more about load balancers and their associated resources, continue to the how-to articles.