用于创建现有负载均衡器的新入站 NAT 池的 powershell 命令

powershell command for creating new Inbound NAT Pool of Existing Load Balancer

我想将现有的 Azure 负载均衡器附加到虚拟机规模集,为此我需要创建入站 nat 池,以便我可以获取入站 nat 池的资源 ID 并将其传递给 ARM 模板VMSS。 提前致谢。

根据我的研究,我们可以使用 PowerShell 命令 New-AzLoadBalancerInboundNatPoolConfig 创建入站 nat 池。详情请参考https://docs.microsoft.com/en-us/powershell/module/az.network/New-AzLoadBalancerInboundNatPoolConfig?view=azps-2.8.0.

例如:

Connect-AzAccount

$slb = Get-AzLoadBalancer -Name "testload" -ResourceGroupName "testfun07"
$feIpConfig = Get-AzLoadBalancerFrontendIpConfig -Name "LoadBalancerFrontEnd" -Loadbalancer $slb

New-AzLoadBalancerInboundNatPoolConfig -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001


更新

如果我们想使用 PowerShell 为现有的负载均衡器创建入站 NAT 池,我们需要执行以下步骤

  1. 创建入站 NAT 池
$slb = Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07"

$feIpConfig = Get-AzureRmLoadBalancerFrontendIpConfig -Name "LoadBalancerFrontEnd" -Loadbalancer $slb

Add-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
  1. 为 laad 平衡器配置 ppol
Set-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001

  1. 更新负载平衡器
Set-AzureRmLoadBalancer -LoadBalancer $slb

例如

$name = "you account"
$password = "password"
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($name, $secpasswd)
Connect-AzureRmAccount -Credential $mycreds 

$slb = Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07"

$feIpConfig = Get-AzureRmLoadBalancerFrontendIpConfig -Name "LoadBalancerFrontEnd" -Loadbalancer $slb

Add-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001
 "1" | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8

Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07" | Select-Object InboundNatPools | fl | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8



Set-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $slb -Name "myInboundNatPool" -FrontendIpConfigurationId $feIpConfig.Id -Protocol TCP -FrontendPortRangeStart 1001 -FrontendPortRangeEnd 2000 -BackendPort 1001

 "2" | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8

Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07" | Select-Object InboundNatPools | fl | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8



Set-AzureRmLoadBalancer -LoadBalancer $slb

"3" | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8

Get-AzureRmLoadBalancer -Name "testload" -ResourceGroupName "testfun07" | Select-Object InboundNatPools | fl | Out-File -FilePath E:\mytest.txt -Append -Encoding utf8

感谢您的回复,但经过多次尝试后我终于找到了答案。

New-AzureRmLoadBalancerInboundNatPoolConfig -Name "natpool3" -Protocol Tcp -FrontendPortRangeStart 50000 -FrontendPortRangeEnd 50119 -BackendPort 3389
$lb= Get-AzureRmLoadBalancer -ResourceGroupName VMSS-RG -Name nbslb
$feip=(Get-AzureRmLoadBalancer -ResourceGroupName VMSS-RG -Name nbslb).FrontendIpConfigurations[0].Id
Add-AzureRmLoadBalancerInboundNatPoolConfig -LoadBalancer $lb -FrontendIpConfigurationId $feip -Name "natpool3" -Protocol Tcp -FrontendPortRangeStart 50000 -FrontendPortRangeEnd 50119 -BackendPort 3389 
Set-AzureRmLoadBalancer -LoadBalancer $lb