使用PowerShell将可用性集添加到Azure负载均衡器的后端池

时间:2018-05-03 10:00:35

标签: powershell azure

尽管使用了官方文档,我仍然很难找到合适的PowerShell,将可用性集(2个虚拟机)与其第一个IP关联到Azure负载均衡器的后端池配置。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您可以使用Get-AzureRmAvailabilitySet来获取然后枚举可用性集中的VM。然后使用Set-AzureRmNetworkInterface在每个VM的网卡上设置LoadBalancerBackendAddressPool

此链接围绕此https://docs.microsoft.com/en-us/azure/networking/scripts/load-balancer-windows-powershell-sample-nlb

有一些很好的代码

答案 1 :(得分:0)

我遇到了同样的问题,我的目标是根据可用性集中的计算机更新负载均衡器backendPool。

我创建了一个脚本:

<#
.SYNOPSIS
    Updates the Azure Load Balancer backend Pool
.DESCRIPTION
    Add's vm's to the backend pool of the specified Azure Load Balancer.
.OUTPUTS
    Progress messages
#>
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $True)]
    [string]$loadBalancerName,
    [Parameter(Mandatory = $True)]
    [string]$resourceGroupName,
    [Parameter(Mandatory = $True)]
    [string]$debugDeploymentDebugLevel,
    [Parameter(Mandatory = $True)]
    [string]$availabilitySetName,
    [Parameter(Mandatory = $True)]
    [string]$backendPoolName
)

$ErrorActionPreference = "Stop"

Try {
    $loadBalancer = Get-AzureRmLoadBalancer `
        -Name $loadBalancerName `
        -ResourceGroupName $resourceGroupName `
        -ErrorAction Stop
}
Catch {
    Write-Warning "No Load Balancer found with name $loadBalancerName in resource group $resourceGroupName"
    Return
}

try {
    $backendPool = Get-AzureRmLoadBalancerBackendAddressPoolConfig `
        -Name $backendPoolName `
        -LoadBalancer $loadBalancer
}
catch {
    #Write-Warning "no Backend Pool found with the name $backendPoolName in the load balancer with the name $loadBalancerName"
    Return
}

try {
    $AvSet = Get-AzureRmAvailabilitySet `
        -Name $availabilitySetName `
        -ResourceGroupName (Get-AzureRmResource | Where-Object {
            ($_.Name -eq $availabilitySetName) -and `
            ($_.ResourceType -eq "Microsoft.Compute/AvailabilitySets")}).ResourceGroupName
}
catch {
    Write-Warning "no AvailabilitySet found with the name $availabilitySetName in resource group $availabilitySetResourceGroup"
    Return
}

ForEach ($id in $avSet.VirtualMachinesReferences.id) {

    $nic = Get-AzureRmNetworkInterface | Where-Object {($_.VirtualMachine.id).ToLower() -eq ($id).ToLower()}
    $nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $backendPool

    Set-AzureRmNetworkInterface -NetworkInterface $nic -AsJob    
}    

If ($ErrorMessages) {
    Write-Error "Deployment returned the following errors: $ErrorMessages";
    Return
}

您也可以在我的github上找到:https://github.com/azurekid/blog/blob/master/Update-BackendPool.ps1

希望这会有所帮助;-)