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

本文介绍如何将 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.

负载均衡应用场景图像

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

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

按照以下步骤来实现本文所概述的场景:Follow the steps below to achieve the scenario outlined in this article:

  1. 安装 Azure PowerShell 中的说明进行操作。Install Azure PowerShell. 有关安装最新版本的 Azure PowerShell、选择订阅和登录帐户的信息,请参阅 如何安装和配置 Azure PowerShellSee How to install and configure Azure PowerShell for information about installing the latest version of Azure PowerShell, selecting your subscription, and signing in to your account.

  2. 使用以下设置创建资源组:Create a resource group using the following settings:

    $location = "chinaeast".
    $myResourceGroup = "contosofabrikam"
    

    有关详细信息,请参阅创建资源组中的第 2 步。For more information, see Step 2 of Create a Resource Group.

  3. 创建可用性集来包含 VM。Create an Availability Set to contain your VMs. 对于此场景,请使用以下命令:For this scenario, use the following command:

    New-AzAvailabilitySet -ResourceGroupName "contosofabrikam" -Name "myAvailset" -Location "China North"
    
  4. 按照创建 Windows VM 中步骤 3 至 5 的说明准备创建具有单个 NIC 的 VM。Follow instructions steps 3 through 5 in Create a Windows VM article to prepare the creation of a VM with a single NIC. 执行步骤 6.1,使用以下命令而不是步骤 6.2:Execute step 6.1, and use the following instead of step 6.2:

    $availset = Get-AzAvailabilitySet -ResourceGroupName "contosofabrikam" -Name "myAvailset"
    New-AzVMConfig -VMName "VM1" -VMSize "Standard_DS1_v2" -AvailabilitySetId $availset.Id
    

    然后完成创建 Windows VM 的步骤 6.3 至 6.8。Then complete Create a Windows VM steps 6.3 through 6.8.

  5. 向每个 VM 中添加另一个 IP 配置。Add a second IP configuration to each of the VMs. 按照将多个 IP 地址分配给虚拟机文章中的说明执行操作。Follow the instructions in Assign multiple IP addresses to virtual machines article. 请使用以下配置设置:Use the following configuration settings:

    $NicName = "VM1-NIC2"
    $RgName = "contosofabrikam"
    $NicLocation = "China North"
    $IPConfigName4 = "VM1-ipconfig2"
    $Subnet1 = Get-AzVirtualNetworkSubnetConfig -Name "mySubnet" -VirtualNetwork $myVnet
    

    在本文中无需将辅助 IP 配置与公共 IP 关联。You do not need to associate the secondary IP configurations with public IPs for the purpose of this tutorial. 请编辑此命令以删除公共 IP 关联部分。Edit the command to remove the public IP association part.

  6. 针对 VM2 再次完成本文的步骤 4 到 6。Complete steps 4 through 6 of this article again for VM2. 执行此操作时务必将 VM 名称替换为 VM2。Be sure to replace the VM name to VM2 when doing this. 请注意,无需为第二个 VM 创建虚拟网络。Note that you do not need to create a virtual network for the second VM. 用户可以根据自己的用例创建或不创建新的子网。You may or may not create a new subnet based on your use case.

  7. 创建两个公共 IP 地址并将它们存储在相应的变量中,如下所示:Create two public IP addresses and store them in the appropriate variables as shown:

    $publicIP1 = New-AzPublicIpAddress -Name PublicIp1 -ResourceGroupName contosofabrikam -Location 'China North' -AllocationMethod Dynamic -DomainNameLabel contoso
    $publicIP2 = New-AzPublicIpAddress -Name PublicIp2 -ResourceGroupName contosofabrikam -Location 'China North' -AllocationMethod Dynamic -DomainNameLabel fabrikam
    
    $publicIP1 = Get-AzPublicIpAddress -Name PublicIp1 -ResourceGroupName contosofabrikam
    $publicIP2 = Get-AzPublicIpAddress -Name PublicIp2 -ResourceGroupName contosofabrikam
    
  8. 创建两个前端 IP 配置:Create two frontend IP configurations:

    $frontendIP1 = New-AzLoadBalancerFrontendIpConfig -Name contosofe -PublicIpAddress $publicIP1
    $frontendIP2 = New-AzLoadBalancerFrontendIpConfig -Name fabrikamfe -PublicIpAddress $publicIP2
    
  9. 创建后端地址池、探测程序和负载均衡规则:Create your backend address pools, a probe, and your load balancing rules:

    $beaddresspool1 = New-AzLoadBalancerBackendAddressPoolConfig -Name contosopool
    $beaddresspool2 = New-AzLoadBalancerBackendAddressPoolConfig -Name fabrikampool
    
    $healthProbe = New-AzLoadBalancerProbeConfig -Name HTTP -RequestPath 'index.html' -Protocol http -Port 80 -IntervalInSeconds 15 -ProbeCount 2
    
    $lbrule1 = New-AzLoadBalancerRuleConfig -Name HTTPc -FrontendIpConfiguration $frontendIP1 -BackendAddressPool $beaddresspool1 -Probe $healthprobe -Protocol Tcp -FrontendPort 80 -BackendPort 80
    $lbrule2 = New-AzLoadBalancerRuleConfig -Name HTTPf -FrontendIpConfiguration $frontendIP2 -BackendAddressPool $beaddresspool2 -Probe $healthprobe -Protocol Tcp -FrontendPort 80 -BackendPort 80
    
  10. 一旦创建这些资源后,即可创建负载均衡器:Once you have these resources created, create your load balancer:

    $mylb = New-AzLoadBalancer -ResourceGroupName contosofabrikam -Name mylb -Location 'China North' -FrontendIpConfiguration $frontendIP1 -LoadBalancingRule $lbrule -BackendAddressPool $beAddressPool -Probe $healthProbe
    
  11. 将第二个后端地址池和前端 IP 配置添加到新创建的负载均衡器:Add the second backend address pool and frontend IP configuration to your newly created load balancer:

    $mylb = Get-AzLoadBalancer -Name "mylb" -ResourceGroupName $myResourceGroup | Add-AzLoadBalancerBackendAddressPoolConfig -Name fabrikampool | Set-AzLoadBalancer
    
    $mylb | Add-AzLoadBalancerFrontendIpConfig -Name fabrikamfe -PublicIpAddress $publicIP2 | Set-AzLoadBalancer
    
    Add-AzLoadBalancerRuleConfig -Name HTTP -LoadBalancer $mylb -FrontendIpConfiguration $frontendIP2 -BackendAddressPool $beaddresspool2 -Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 80 | Set-AzLoadBalancer
    
  12. 下面的命令获取 NIC,并将每个辅助 NIC 的两个 IP 配置添加到负载均衡器的后端地址池:The commands below get the NICs and then add both IP configurations of each secondary NIC to the backend address pool of the load balancer:

    $nic1 = Get-AzNetworkInterface -Name "VM1-NIC2" -ResourceGroupName "MyResourcegroup";
    $nic2 = Get-AzNetworkInterface -Name "VM2-NIC2" -ResourceGroupName "MyResourcegroup";
    
    $nic1.IpConfigurations[0].LoadBalancerBackendAddressPools.Add($mylb.BackendAddressPools[0]);
    $nic1.IpConfigurations[1].LoadBalancerBackendAddressPools.Add($mylb.BackendAddressPools[1]);
    $nic2.IpConfigurations[0].LoadBalancerBackendAddressPools.Add($mylb.BackendAddressPools[0]);
    $nic2.IpConfigurations[1].LoadBalancerBackendAddressPools.Add($mylb.BackendAddressPools[1]);
    
    $mylb = $mylb | Set-AzLoadBalancer
    
    $nic1 | Set-AzNetworkInterface
    $nic2 | Set-AzNetworkInterface
    
  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