快速入门:使用 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.

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

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.
    • 如果使用的是本地安装,请通过 Azure CLI 使用 az login 命令登录。If you're using a local install, sign in with Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅使用 Azure CLI 登录See Sign in with Azure CLI for additional sign-in options.
    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with Azure CLI.
    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.
  • 本快速入门需要 Azure CLI 2.0.28 或更高版本。This quickstart requires version 2.0.28 or later of 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:

  • 命名为“CreatePubLBQS-rg”。Named CreatePubLBQS-rg.
  • 在“chinaeast2”位置。In the chinaeast2 location.
  az group create \
    --name CreatePubLBQS-rg \
    --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 - Standard

需要先创建支持的虚拟网络资源,然后才能部署 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.
  • 地址前缀为 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.
  • 在“CreatePubLBQS-rg”资源组中。In the CreatePubLBQS-rg resource group.
  • “chinaeast2”的位置。Location of chinaeast2.
  az network vnet create \
    --resource-group CreatePubLBQS-rg \
    --location chinaeast2 \
    --name myVNet \
    --address-prefixes 10.1.0.0/16 \
    --subnet-name myBackendSubnet \
    --subnet-prefixes 10.1.0.0/24

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

使用 az network public-ip create 为堡垒主机创建公共 IP:Use az network public-ip create to create a public ip address for the bastion host:

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

创建堡垒子网Create a bastion subnet

使用 az network vnet subnet create 创建堡垒子网:Use az network vnet subnet create to create a bastion subnet:

  • 命名为 AzureBastionSubnet。Named AzureBastionSubnet.
  • 地址前缀为 10.1.1.0/24。Address prefix of 10.1.1.0/24.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
az network vnet subnet create \
    --resource-group CreatePubLBQS-rg \
    --name AzureBastionSubnet \
    --vnet-name myVNet \
    --address-prefixes 10.1.1.0/24

创建堡垒主机Create bastion host

使用 az network bastion create 创建堡垒主机:Use az network bastion create to create a bastion host:

  • 命名为 myBastionHost。Named myBastionHost.
  • 在“CreatePubLBQS-rg”中。In CreatePubLBQS-rg.
  • 与公共 IP myBastionIP 相关联。Associated with public IP myBastionIP.
  • 与虚拟网络 myVNet 相关联。Associated with virtual network myVNet.
  • 在“chinaeast2”位置。In chinaeast2 location.
az network bastion create \
    --resource-group CreatePubLBQS-rg \
    --name myBastionHost \
    --public-ip-address myBastionIP \
    --vnet-name myVNet \
    --location chinaeast2

部署 Azure Bastion 主机需要几分钟时间。It can take a few minutes for the Azure Bastion host to deploy.

创建网络安全组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.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  az network nsg create \
    --resource-group CreatePubLBQS-rg \
    --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.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 协议为“(*)”。Protocol (*).
  • 方向为“入站”。Direction Inbound.
  • 源为“(*)”。Source (*).
  • 目标为“(*)”。Destination (*).
  • 目标端口为“端口 80”。Destination port Port 80.
  • 访问为“允许”。Access Allow.
  • 优先级为“200”。Priority 200.
  az network nsg rule create \
    --resource-group CreatePubLBQS-rg \
    --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 backend servers - Standard

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

  • 虚拟机的三个网络接口。Three network interfaces for the virtual machines.
  • 三个要用作负载均衡器后端服务器的虚拟机。Three virtual machines to be used as backend servers for the load balancer.

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

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

  • 名为 myNicVM1、myNicVM2 和 myNicVM3 。Named myNicVM1, myNicVM2, and myNicVM3.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 在虚拟网络“myVNet”中。In virtual network myVNet.
  • 在子网“myBackendSubnet”中。In subnet myBackendSubnet.
  • 在网络安全组“myNSG”中。In network security group myNSG.
  array=(myNicVM1 myNicVM2 myNicVM3)
  for vmnic in "${array[@]}"
  do
    az network nic create \
        --resource-group CreatePubLBQS-rg \
        --name $vmnic \
        --vnet-name myVNet \
        --subnet myBackEndSubnet \
        --network-security-group myNSG
  done

创建虚拟机Create virtual machines

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

VM1VM1

  • 命名为“myVM1”。Named myVM1.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 附加到网络接口“myNicVM1”。Attached to network interface myNicVM1.
  • 虚拟机映像 win2019datacenter。Virtual machine image win2019datacenter.
  az vm create \
    --resource-group CreatePubLBQS-rg \
    --name myVM1 \
    --nics myNicVM1 \
    --image win2019datacenter \
    --admin-username azureuser \
    --no-wait

VM2VM2

  • 命名为“myVM2”。Named myVM2.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 附加到网络接口“myNicVM2”。Attached to network interface myNicVM2.
  • 虚拟机映像 win2019datacenter。Virtual machine image win2019datacenter.
  az vm create \
    --resource-group CreatePubLBQS-rg \
    --name myVM2 \
    --nics myNicVM2 \
    --image win2019datacenter \
    --admin-username azureuser \
    --no-wait

VM3VM3

  • 命名为“myVM3”。Named myVM3.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 附加到网络接口“myNicVM3”。Attached to network interface myNicVM3.
  • 虚拟机映像 win2019datacenter。Virtual machine image win2019datacenter.
   az vm create \
    --resource-group CreatePubLBQS-rg \
    --name myVM3 \
    --nics myNicVM3 \
    --image win2019datacenter \
    --admin-username azureuser \
    --no-wait

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

创建公共 IP 地址 - 标准Create a public IP address - Standard

若要通过 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.
  • 在“CreatePubLBQS-rg”中。In CreatePubLBQS-rg.
  az network public-ip create \
    --resource-group CreatePubLBQS-rg \
    --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 CreatePubLBQS-rg \
    --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 CreatePubLBQS-rg \
    --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 CreatePubLBQS-rg \
    --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

将虚拟机添加到负载均衡器后端池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:

  • 在后端地址池“myBackEndPool”中。In backend address pool myBackEndPool.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  array=(myNicVM1 myNicVM2 myNicVM3)
  for vmnic in "${array[@]}"
  do
    az network nic ip-config address-pool add \
     --address-pool myBackendPool \
     --ip-config-name ipconfig1 \
     --nic-name $vmnic \
     --resource-group CreatePubLBQS-rg \
     --lb-name myLoadBalancer
  done

创建出站规则配置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 或前缀可用于出站配置。A public IP or prefix can be used for the outbound configuration.

公共 IPPublic IP

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

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

公共 IP 前缀Public IP Prefix

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

  • 命名为 myPublicIPPrefixOutbound。Named myPublicIPPrefixOutbound.
  • 在“CreatePubLBQS-rg”中。In CreatePubLBQS-rg.
  • 前缀长度为 28。Prefix length of 28.
  az network public-ip prefix create \
    --resource-group CreatePubLBQS-rg \
    --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.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 与公共 IP 地址 myPublicIPOutbound 关联。Associated with public IP address myPublicIPOutbound.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network lb frontend-ip create \
    --resource-group CreatePubLBQS-rg \
    --name myFrontEndOutbound \
    --lb-name myLoadBalancer \
    --public-ip-address myPublicIPOutbound 

公共 IP 前缀Public IP prefix

  • 命名为“myFrontEndOutbound”。Named myFrontEndOutbound.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 与公共 IP 前缀 myPublicIPPrefixOutbound 关联。Associated with public IP prefix myPublicIPPrefixOutbound.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network lb frontend-ip create \
    --resource-group CreatePubLBQS-rg \
    --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.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  az network lb address-pool create \
    --resource-group CreatePubLBQS-rg \
    --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.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 与负载均衡器 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 CreatePubLBQS-rg \
    --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:

  • 在后端地址池“myBackEndPoolOutbound”中。In backend address pool myBackEndPoolOutbound.
  • 在“CreatePubLBQS-rg”资源组中。In resource group CreatePubLBQS-rg.
  • 与负载均衡器 myLoadBalancer 关联。Associated with load balancer myLoadBalancer.
  array=(myNicVM1 myNicVM2 myNicVM3)
  for vmnic in "${array[@]}"
  do
    az network nic ip-config address-pool add \
     --address-pool myBackendPoolOutbound \
     --ip-config-name ipconfig1 \
     --nic-name $vmnic \
     --resource-group CreatePubLBQS-rg \
     --lb-name myLoadBalancer
  done

安装 IISInstall IIS

使用 az vm extension set 将 IIS 安装在虚拟机上,并将默认网站设置为计算机名。Use az vm extension set to install IIS on the virtual machines and set the default website to the computer name.

  array=(myVM1 myVM2 myVM3)
    for vm in "${array[@]}"
    do
     az vm extension set \
       --publisher Microsoft.Compute \
       --version 1.8 \
       --name CustomScriptExtension \
       --vm-name $vm \
       --resource-group CreatePubLBQS-rg \
       --settings '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
  done

测试负载均衡器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 CreatePubLBQS-rg \
    --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 CreatePubLBQS-rg

后续步骤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: