使用 Azure CLI 在多个 IP 配置上进行负载均衡Load balancing on multiple IP configurations using Azure CLI

本文介绍如何将 Azure 负载均衡器用于辅助网络接口 (NIC) 的多个 IP 地址。This article describes how to use Azure Load Balancer with multiple IP addresses on a secondary network interface (NIC). 在此方案中,有两个运行 Windows 的 VM,每个 VM 有一个主 NIC 和一个辅助 NIC。For this scenario, we have two VMs running Windows, each with a primary and a secondary NIC. 每个辅助 NIC 都有两个 IP 配置。Each of the secondary NICs has two IP configurations. 每个 VM 托管网站 contoso.com 和 fabrikam.com。Each VM hosts both websites contoso.com and fabrikam.com. 每个网站都绑定到辅助 NIC 的一个 IP 配置。Each website is bound to one of the IP configurations on the secondary NIC. 我们使用 Azure 负载均衡器公开两个前端 IP 地址,每个地址分别对应于一个网站,从而将流量分发到网站的各个 IP 配置。We use Azure Load Balancer to expose two frontend IP addresses, one for each website, to distribute traffic to the respective IP configuration for the website. 此场景中两个前端以及两个后端池 IP 地址都使用相同的端口号。This scenario uses the same port number across both frontends, as well as both backend pool IP addresses.

负载均衡应用场景图像

在多个 IP 配置上进行负载均衡的步骤Steps to load balance on multiple IP configurations

若要实现本文中所述的方案,请完成以下步骤:To achieve the scenario outlined in this article complete the following steps:

  1. 按照所链接的文章中的步骤安装和配置 Azure CLI,然后登录到 Azure 帐户。Install and Configure the Azure CLI by following the steps in the linked article and log into your Azure account.

  2. 如下所述创建一个资源组并将其命名为 contosofabrikam,如下所示:Create a resource group called contosofabrikam as follows:

    az group create contosofabrikam chinaeast
    
  3. 为两个 VM 创建可用性集Create an availability set to for the two VMs. 对于此场景,请使用以下命令:For this scenario, use the following command:

    az vm availability-set create --resource-group contosofabrikam --location chinaeast --name myAvailabilitySet
    
  4. 创建一个虚拟网络并将其命名为 myVNet,并创建一个名为 mySubnet 的子网:Create a virtual network called myVNet and a subnet called mySubnet:

    az network vnet create --resource-group contosofabrikam --name myVnet --address-prefixes 10.0.0.0/16  --location chinaeast --subnet-name MySubnet --subnet-prefix 10.0.0.0/24
    
  5. 创建负载均衡器并将其命名为 mylbCreate the load balancer called mylb:

    az network lb create --resource-group contosofabrikam --location chinaeast --name mylb
    
  6. 为负载均衡器的前端 IP 配置创建两个动态公共 IP 地址:Create two dynamic public IP addresses for the frontend IP configurations of your load balancer:

    az network public-ip create --resource-group contosofabrikam --location chinaeast --name PublicIp1 --domain-name-label contoso --allocation-method Dynamic
    
    az network public-ip create --resource-group contosofabrikam --location chinaeast --name PublicIp2 --domain-name-label fabrikam --allocation-method Dynamic
    
  7. 创建两个分别名为 contosofefabrikamfe 的前端 IP 配置:Create the two frontend IP configurations, contosofe and fabrikamfe respectively:

    az network lb frontend-ip create --resource-group contosofabrikam --lb-name mylb --public-ip-name PublicIp1 --name contosofe
    az network lb frontend-ip create --resource-group contosofabrikam --lb-name mylb --public-ip-name PublicIp2 --name fabrkamfe
    
  8. 创建后端地址池 - contosopoolfabrikampool探测器 - HTTP 以及负载均衡规则 - HTTPcHTTPfCreate your backend address pools - contosopool and fabrikampool, a probe - HTTP, and your load balancing rules - HTTPc and HTTPf:

    az network lb address-pool create --resource-group contosofabrikam --lb-name mylb --name contosopool
    azure network lb address-pool create --resource-group contosofabrikam --lb-name mylb --name fabrikampool
    
    az network lb probe create --resource-group contosofabrikam --lb-name mylb --name HTTP --protocol "http" --interval 15 --count 2 --path index.html
    
    az network lb rule create --resource-group contosofabrikam --lb-name mylb --name HTTPc --protocol tcp --probe-name http--frontend-port 5000 --backend-port 5000 --frontend-ip-name contosofe --backend-address-pool-name contosopool
    az network lb rule create --resource-group contosofabrikam --lb-name mylb --name HTTPf --protocol tcp --probe-name http --frontend-port 5000 --backend-port 5000 --frontend-ip-name fabrkamfe --backend-address-pool-name fabrikampool
    
  9. 运行以下命令,检查输入以验证是否正确创建了负载均衡器Check the output to verify your load balancer was created correctly by running the following command:

    az network lb show --resource-group contosofabrikam --name mylb
    
  10. 为第一个虚拟机 VM1 创建公共 IPmyPublicIp存储帐户mystorageaccont1,如下所示:Create a public IP, myPublicIp, and storage account, mystorageaccont1 for your first virtual machine VM1 as follows:

    az network public-ip create --resource-group contosofabrikam --location chinaeast --name myPublicIP --domain-name-label mypublicdns345 --allocation-method Dynamic
    
    az storage account create --location chinaeast --resource-group contosofabrikam --kind Storage --sku-name GRS mystorageaccount1
    
  11. 为 VM1 创建网络接口,添加另一个 IP 配置 VM1-ipconfig2,并创建 VM,如下所示:Create the network interfaces for VM1 and add a second IP configuration, VM1-ipconfig2, and create the VM as follows:

    az network nic create --resource-group contosofabrikam --location chinaeast --subnet-vnet-name myVnet --subnet-name mySubnet --name VM1Nic1 --ip-config-name NIC1-ipconfig1
    az network nic create --resource-group contosofabrikam --location chinaeast --subnet-vnet-name myVnet --subnet-name mySubnet --name VM1Nic2 --ip-config-name VM1-ipconfig1 --public-ip-name myPublicIP --lb-address-pool-ids "/subscriptions/<your subscription ID>/resourceGroups/contosofabrikam/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/contosopool"
    az network nic ip-config create --resource-group contosofabrikam --nic-name VM1Nic2 --name VM1-ipconfig2 --lb-address-pool-ids "/subscriptions/<your subscription ID>/resourceGroups/contosofabrikam/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/fabrikampool"
    az vm create --resource-group contosofabrikam --name VM1 --location chinaeast --os-type linux --nic-names VM1Nic1,VM1Nic2  --vnet-name VNet1 --vnet-subnet-name Subnet1 --availability-set myAvailabilitySet --vm-size Standard_DS3_v2 --storage-account-name mystorageaccount1 --image-urn canonical:UbuntuServer:16.04.0-LTS:latest --admin-username <your username>  --admin-password <your password>
    
  12. 为第二个 VM 重复步骤 10-11:Repeat steps 10-11 for your second VM:

    az network public-ip create --resource-group contosofabrikam --location chinaeast --name myPublicIP2 --domain-name-label mypublicdns785 --allocation-method Dynamic
    az storage account create --location chinaeast --resource-group contosofabrikam --kind Storage --sku-name GRS mystorageaccount2
    az network nic create --resource-group contosofabrikam --location chinaeast --subnet-vnet-name myVnet --subnet-name mySubnet --name VM2Nic1
    az network nic create --resource-group contosofabrikam --location chinaeast --subnet-vnet-name myVnet --subnet-name mySubnet --name VM2Nic2 --ip-config-name VM2-ipconfig1 --public-ip-name myPublicIP2 --lb-address-pool-ids "/subscriptions/<your subscription ID>/resourceGroups/contosofabrikam/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/contosopool"
    az network nic ip-config create --resource-group contosofabrikam --nic-name VM2Nic2 --name VM2-ipconfig2 --lb-address-pool-ids "/subscriptions/<your subscription ID>/resourceGroups/contosofabrikam/providers/Microsoft.Network/loadBalancers/mylb/backendAddressPools/fabrikampool"
    az vm create --resource-group contosofabrikam --name VM2 --location chinaeast --os-type linux --nic-names VM2Nic1,VM2Nic2 --vnet-name VNet1 --vnet-subnet-name Subnet1 --availability-set myAvailabilitySet --vm-size Standard_DS3_v2 --storage-account-name mystorageaccount2 --image-urn canonical:UbuntuServer:16.04.0-LTS:latest --admin-username <your username>  --admin-password <your password>
    
  13. 最后,必须将 DNS 资源记录配置为指向各自的负载均衡器的前端 IP 地址。Finally, you must configure DNS resource records to point to the respective frontend IP address of the Load Balancer. 可以在 Azure DNS 中托管域。You may host your domains in Azure DNS. 有关将 Azure DNS 与负载均衡器配合使用的详细信息,请参阅将 Azure DNS 与其他 Azure 服务配合使用For more information about using Azure DNS with Load Balancer, see Using Azure DNS with other Azure services.

后续步骤Next steps