打开 Azure 负载均衡器中的应用程序端口Open an application port in the Azure load balancer

正在 Azure 中运行的 Service Fabric 应用程序位于 Azure 负载均衡器后。A Service Fabric application running in Azure sits behind the Azure load balancer. 此示例脚本将打开 Azure 负载均衡器中的端口,以便 Service Fabric 应用程序可与外部客户端通信。This sample script opens a port in an Azure load balancer so that a Service Fabric application can communicate with external clients. 根据需要自定义参数。Customize the parameters as needed. 如果群集位于网络安全组中,请同时添加入站网络安全组规则,以允许入站通信流流入。If your cluster is in a network security group, also add an inbound network security group rule to allow inbound traffic.

备注

本文进行了更新,以便使用新的 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.

必要时,使用 Service Fabric SDK 安装 Service Fabric PowerShell 模块。If needed, install the Service Fabric PowerShell module with the Service Fabric SDK.

示例脚本Sample script

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

# Login and select your subscription
Connect-AzAccount -Environment AzureChinaCloud
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

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command-specific documentation.

CommandCommand 说明Notes
Get-AzResourceGet-AzResource 获取 Azure 资源。Gets an Azure resource.
Get-AzLoadBalancerGet-AzLoadBalancer 获取 Azure 负载均衡器。Gets the Azure load balancer.
Add-AzLoadBalancerProbeConfigAdd-AzLoadBalancerProbeConfig 向负载均衡器添加探测配置。Adds a probe configuration to a load balancer.
Get-AzLoadBalancerProbeConfigGet-AzLoadBalancerProbeConfig 获取负载均衡器的探测配置。Gets a probe configuration for a load balancer.
Add-AzLoadBalancerRuleConfigAdd-AzLoadBalancerRuleConfig 向负载均衡器添加规则配置。Adds a rule configuration to a load balancer.
Set-AzLoadBalancerSet-AzLoadBalancer 设置负载均衡器的目标状态。Sets the goal state for a load balancer.

后续步骤Next steps

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure PowerShell 示例中找到 Azure Service Fabric 的其他 Powershell 示例。Additional Powershell samples for Azure Service Fabric can be found in the Azure PowerShell samples.