Open an application port in the Azure load balancer

A Service Fabric application running in Azure sits behind the Azure load balancer. 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.

Note

This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure 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
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.

Command Notes
Get-AzResource Gets an Azure resource.
Get-AzLoadBalancer Gets the Azure load balancer.
Add-AzLoadBalancerProbeConfig Adds a probe configuration to a load balancer.
Get-AzLoadBalancerProbeConfig Gets a probe configuration for a load balancer.
Add-AzLoadBalancerRuleConfig Adds a rule configuration to a load balancer.
Set-AzLoadBalancer Sets the goal state for a load balancer.

Next steps

For more information on the Azure PowerShell module, see Azure PowerShell documentation.

Additional Powershell samples for Azure Service Fabric can be found in the Azure PowerShell samples.