在 Azure Load Balancer 中開啟應用程式連接埠

在 Azure 中執行的 Service Fabric 應用程式位於 Azure Load Balancer 後方。 這個範例指令碼會在 Azure Load Balancer 中開啟連接埠,以便 Service Fabric 應用程式可以與外部用戶端通訊。 視需要自訂參數。 如果您的叢集位於網路安全性群組中,也要新增輸入網路安全性群組規則,以允許輸入流量。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

如有需要,可隨同 Service Fabric SDK 一起安裝 Service Fabric PowerShell 模組。

範例指令碼

# Variables
$probename = "AppPortProbe6"
$rulename="AppPortLBRule6"
$RGname="mysftestclustergroup"
$port=8303
$subscriptionID = 'subscription ID'

# Login and select your subscription
Connect-AzAccount
Get-AzSubscription -SubscriptionId $subscriptionID | Select-AzSubscription 

# Get the load balancer resource
$resource = Get-AzResource | Where {$_.ResourceGroupName –eq $RGname -and $_.ResourceType -eq "Microsoft.Network/loadBalancers"} 
$slb = Get-AzLoadBalancer -Name $resource.Name -ResourceGroupName $RGname

# Add a new probe configuration to the load balancer
$slb | Add-AzLoadBalancerProbeConfig -Name $probename -Protocol Tcp -Port $port -IntervalInSeconds 15 -ProbeCount 2

# Add rule configuration to the load balancer
$probe = Get-AzLoadBalancerProbeConfig -Name $probename -LoadBalancer $slb
$slb | Add-AzLoadBalancerRuleConfig -Name $rulename -BackendAddressPool $slb.BackendAddressPools[0] -FrontendIpConfiguration $slb.FrontendIpConfigurations[0] -Probe $probe -Protocol Tcp -FrontendPort $port -BackendPort $port

# Set the goal state for the load balancer
$slb | Set-AzLoadBalancer

指令碼說明

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
Get-AzResource 取得 Azure 資源。
Get-AzLoadBalancer 取得 Azure Load Balancer。
Add-AzLoadBalancerProbeConfig 將探查組態新增至負載平衡器。
Get-AzLoadBalancerProbeConfig 取得負載平衡器的探查組態。
Add-AzLoadBalancerRuleConfig 將規則組態新增至負載平衡器。
Set-AzLoadBalancer 設定負載平衡器的目標狀態。

下一步

如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件

您可以在 Azure PowerShell 範例中找到適用於 Azure Service Fabric 的其他 PowerShell 範例。