如何在Azure负载平衡器入站NAT规则上替换目标VM和NIC



我有一个现有的Azure前端负载平衡器,该平衡器具有入站NAT规则。我想将质量中这些NAT规则的目标/目的地更改为新的目标服务器。

我目前有一个脚本,该脚本从LB中获取所有NAT规则,然后通过它们迭代,并尝试将它们添加到新服务器的NIC中。我已经尝试过,也没有从旧服务器的NIC中删除NAT规则。无论哪种方式,该方法返回false,并且不应用任何更改。

#Set Variables
#subscription ID
$subscription = "value"
#the name of the old NIC that has the LB rules
$OldNicName = "old_nic"
#the name of the NIC to be attached to the LB rules
$NewNicName = "new_nic"
#name of the loadbalancer
$lbname = "my_lb"
#Set Active Subscription
Set-AzContext -SubscriptionId $subscription
#Get the loadbalancer
$lb = Get-AzLoadBalancer -Name $lbname
#Get the old firewall interface/NIC
$OldNic = Get-AzNetworkInterface -Name $OldNicName
#Get the target firewall interface/NIC
$NewNic = Get-AzNetworkInterface -Name $NewNicName
#Attach NAT rules to the NIC
$lb.InboundNatRules | ForEach-Object -Process {$OldNic.IpConfigurations[0].LoadBalancerInboundNatRules.Remove($_); $NewNic.IpConfigurations[0].LoadBalancerInboundNatRules.Add($_)}
#Apply the configuration and reload the NIC
$OldNic | Set-AzNetworkInterface
$NewNic | Set-AzNetworkInterface

我想要现在的每个入站NAT规则与新的NIC/VM关联,但目前remove((和add((函数都返回false。

对于您的问题,您想将质量中这些NAT规则的目标/目标更改为新的目标服务器。与VM网络接口关联的NAT规则并在接口IP配置中设置。因此,您需要使用两个PowerShell命令Set-AzNetworkInterfaceIpConfigSet-AzNetworkInterface实现您的目的。这样的脚本:

# Set Variables
# subscription ID
$subscription = "value"
# the name of the old NIC that has the LB rules
$OldNicName = "old_nic"
# the name of the NIC to be attached to the LB rules
$NewNicName = "new_nic"
# name of the loadbalancer
$lbname = "my_lb"
# assume all the resources in the same group
$groupname = "group_name"
Set-AzContext -SubscriptionId $subscription
# remove the NAT rules from the old NIC
$oldNic = Get-AzNetworkInterface -ResourceGroupName $groupname -Name $OldNicName
$list = @()       # this is a empty array
Set-AzNetworkInterfaceIpConfig -Name ipconfig1 -NetworkInterface $oldNic -LoadBalancerInboundNatRule $list 
$oldNic | Set-AzNetworkInterface
# associate the NAT rules to the new NIC
$newNic = Get-AzNetworkInterface -ResourceGroupName $groupname -Name $NewNicName
$lb = Get-AzLoadBalancer -ResourceGroupName $groupname -Name $lbname
$NatRules = Get-AzLoadBalancerInboundNatRuleConfig -LoadBalancer $lb
Set-AzNetworkInterfaceIpConfig -Name ipconfig1 -NetworkInterface $newNic -LoadBalancerInboundNatRule $NatRules
$newNic | Set-AzNetworkInterface

最新更新