|
| 1 | +--- |
| 2 | +title: Configure load balancing and outbound rules using Azure PowerShell |
| 3 | +titlesuffix: Azure Load Balancer |
| 4 | +description: This article shows how to configure load balancing and outbound rules in a Standard Load Balancer using the Azure PowerShell. |
| 5 | +services: load-balancer |
| 6 | +author: asudbring |
| 7 | +ms.service: load-balancer |
| 8 | +ms.topic: article |
| 9 | +ms.date: 09/24/2019 |
| 10 | +ms.author: allensu |
| 11 | + |
| 12 | +--- |
| 13 | +# Configure load balancing and outbound rules in Standard Load Balancer using Azure PowerShell |
| 14 | + |
| 15 | +This article shows you how to configure outbound rules in Standard Load Balancer using Azure PowerShell. |
| 16 | + |
| 17 | +When you are done, the Load Balancer resource contains two frontends and rules associated with them: one for inbound and another for outbound. Each frontend has a reference to a public IP address and this scenario uses a different public IP address for inbound versus outbound. The load balancing rule provides only inbound load balancing and the outbound rule controls the outbound NAT provided for the VM. This article uses two separate backend pools, one for inbound and one for outbound, to illustrate capability and allow for flexibility for this scenario. |
| 18 | + |
| 19 | +[!INCLUDE [cloud-shell-try-it.md](../../includes/cloud-shell-try-it.md)] |
| 20 | + |
| 21 | +[!INCLUDE [updated-for-az](../../includes/updated-for-az.md)] |
| 22 | + |
| 23 | +## Connect to Azure Account |
| 24 | +Sign in to your Azure subscription with the [Connect-AzAccount](https://docs.microsoft.com/powershell/module/az.accounts/connect-azaccount?view=azps-2.5.0) command and follow the on-screen directions: |
| 25 | + |
| 26 | +```azurepowershell-interactive |
| 27 | +Connect-AzAccount |
| 28 | +``` |
| 29 | +## Create resource group |
| 30 | + |
| 31 | +Create a resource group with [New-AzResourceGroup](https://docs.microsoft.com/powershell/module/az.resources/new-azresourcegroup?view=azps-2.6.0). An Azure resource group is a logical container into which Azure resources are deployed and managed. |
| 32 | + |
| 33 | +The following example creates a resource group named *myresourcegroupoutbound* in the *eastus2* location: |
| 34 | + |
| 35 | +```azurepowershell-interactive |
| 36 | +New-AzResourceGroup -Name myresourcegroupoutbound -Location eastus |
| 37 | +``` |
| 38 | +## Create virtual network |
| 39 | +Create a virtual network named *myvnetoutbound* with a subnet named *mysubnetoutbound* in the *myresourcegroupoutbound* using [New-AzVirtualNetwork](https://docs.microsoft.com/powershell/module/az.network/new-azvirtualnetwork?view=azps-2.6.0) and [New-AzVirtualNetworkSubnetConfig](https://docs.microsoft.com/powershell/module/az.network/new-azvirtualnetworksubnetconfig?view=azps-2.6.0): |
| 40 | + |
| 41 | +```azurepowershell-interactive |
| 42 | +$subnet = New-AzVirtualNetworkSubnetConfig -Name mysubnetoutbound -AddressPrefix "192.168.0.0/24" |
| 43 | +
|
| 44 | +New-AzVirtualNetwork -Name myvnetoutbound -ResourceGroupName myresourcegroupoutbound -Location eastus -AddressPrefix "192.168.0.0/16" -Subnet $subnet |
| 45 | +``` |
| 46 | + |
| 47 | +## Create inbound Public IP address |
| 48 | + |
| 49 | +To access your web app on the Internet, you need a public IP address for the load balancer. A Standard Load Balancer only supports Standard Public IP addresses. Use [New-AzPublicIpAddress](https://docs.microsoft.com/powershell/module/az.network/new-azpublicipaddress?view=azps-2.6.0) to create a Standard Public IP address named *mypublicipinbound* in *myresourcegroupoutbound*. |
| 50 | + |
| 51 | +```azurepowershell-interactive |
| 52 | +$pubIPin = New-AzPublicIpAddress -ResourceGroupName myresourcegroupoutbound -Name mypublicipinbound -AllocationMethod Static -Sku Standard -Location eastus |
| 53 | +``` |
| 54 | + |
| 55 | +## Create outbound public IP address |
| 56 | + |
| 57 | +Create a Standard IP address for Load Balancer's frontend outbound configuration using [New-AzPublicIpAddress](https://docs.microsoft.com/powershell/module/az.network/new-azpublicipaddress?view=azps-2.6.0). |
| 58 | + |
| 59 | +```azurepowershell-interactive |
| 60 | +$pubIPout = New-AzPublicIpAddress -ResourceGroupName myresourcegroupoutbound -Name mypublicipoutbound -AllocationMethod Static -Sku Standard -Location eastus |
| 61 | +``` |
| 62 | + |
| 63 | +## Create Azure Load Balancer |
| 64 | + |
| 65 | +This section details how you can create and configure the following components of the load balancer: |
| 66 | + - A frontend IP that receives the incoming network traffic on the load balancer. |
| 67 | + - A backend pool where the frontend IP sends the load balanced network traffic. |
| 68 | + - A backend pool for outbound connectivity. |
| 69 | + - A health probe that determines health of the backend VM instances. |
| 70 | + - A load balancer inbound rule that defines how traffic is distributed to the VMs. |
| 71 | + - A load balancer outbound rule that defines how traffic is distributed from the VMs. |
| 72 | + |
| 73 | +### Create inbound frontend IP |
| 74 | +Create the outbound frontend IP configuration for the Load Balancer with [New-AzLoadBalancerFrontendIpConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancerfrontendipconfig?view=azps-2.6.0) that includes an inbound frontend IP configuration named *myfrontendinbound* that is associated to the public IP address *mypublicipinbound* |
| 75 | + |
| 76 | +```azurepowershell-interactive |
| 77 | +$frontendIPin = New-AzLoadBalancerFrontendIPConfig -Name "myfrontendinbound" -PublicIpAddress $pubIPin |
| 78 | +``` |
| 79 | +### Create outbound frontend IP |
| 80 | +Create the outbound frontend IP configuration for the Load Balancer with [New-AzLoadBalancerFrontendIpConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancerfrontendipconfig?view=azps-2.6.0) that includes an outbound frontend IP configuration named *myfrontendoutbound* that is associated to the public IP address *mypublicipoutbound*: |
| 81 | + |
| 82 | +```azurepowershell-interactive |
| 83 | +$frontendIPout = New-AzLoadBalancerFrontendIPConfig -Name "myfrontendoutbound" -PublicIpAddress $pubIPout |
| 84 | +``` |
| 85 | +### Create inbound backend pool |
| 86 | +Create the backend inbound pool for the load balancer with [New-AzLoadBalancerBackendAddressPoolConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancerbackendaddresspoolconfig?view=azps-2.6.0) named *bepoolinbound*: |
| 87 | + |
| 88 | +```azurepowershell-interactive |
| 89 | +$bepoolin = New-AzLoadBalancerBackendAddressPoolConfig -Name bepoolinbound |
| 90 | +``` |
| 91 | + |
| 92 | +### Create outbound backend pool |
| 93 | +Create an additional backend address pool to define outbound connectivity for a pool of VMs with [New-AzLoadBalancerBackendAddressPoolConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancerbackendaddresspoolconfig?view=azps-2.6.0) named *bepooloutbound*.Creating a separate outbound pool provides maximum flexibility, but you can omit this step and only use the inbound *bepoolinbound* as well. : |
| 94 | + |
| 95 | +```azurepowershell-interactive |
| 96 | +$bepoolout = New-AzLoadBalancerBackendAddressPoolConfig -Name bepooloutbound |
| 97 | +``` |
| 98 | + |
| 99 | +### Create health probe |
| 100 | + |
| 101 | +A health probe checks all virtual machine instances to make sure they can send network traffic. The virtual machine instance with failed probe checks is removed from the load balancer until it goes back online and a probe check determines that it's healthy. Create a health probe with [New-AzLoadBalancerProbeConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancerprobeconfig?view=azps-2.6.0) to monitor the health of the virtual machines. |
| 102 | + |
| 103 | +```azurepowershell-interactive |
| 104 | +$probe = New-AzLoadBalancerProbeConfig -Name http -Protocol "http" -Port 80 -IntervalInSeconds 15 -ProbeCount 2 -RequestPath / |
| 105 | +``` |
| 106 | +### Create load balancing rule |
| 107 | + |
| 108 | +A load balancer rule defines the frontend IP configuration for the incoming traffic and the backend pool to receive the traffic, along with the required source and destination port. Create a load balancer rule *myinboundlbrule* with [New-AzLoadBalancerRuleConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancerruleconfig?view=azps-2.6.0) for listening to port 80 in the frontend pool *myfrontendinbound* and sending load-balanced network traffic to the backend address pool *bepoolinbound* also using port 80. |
| 109 | + |
| 110 | +>[!NOTE] |
| 111 | +>This load balancing rule disables automatic outbound (S)NAT as a result of this rule with the **-DisableOutboundSNAT** parameter. Outbound NAT is only provided by the outbound rule. |
| 112 | +
|
| 113 | +```azurepowershell-interactive |
| 114 | +$inboundRule = New-AzLoadBalancerRuleConfig -Name inboundlbrule -FrontendIPConfiguration $frontendIPin -BackendAddressPool $bepoolin -Probe $probe -Protocol "Tcp" -FrontendPort 80 -BackendPort 80 -IdleTimeoutInMinutes 15 -EnableFloatingIP -LoadDistribution SourceIP -DisableOutboundSNAT |
| 115 | +``` |
| 116 | + |
| 117 | +### Create outbound rule |
| 118 | + |
| 119 | +An outbound rule defines the frontend public IP, represented by the frontend *myfrontendoutbound*, which will be used for all outbound NAT traffic as well as the backend pool to which this rule applies. Create an outbound rule *myoutboundrule* for outbound network translation of all virtual machines (NIC IP configurations) in *bepool* backend pool. The command below also changes the outbound idle timeout from 4 to 15 minutes and allocates 10000 SNAT ports instead of 1024. Review [New-AzLoadBalancerOutboundRuleConfig](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalanceroutboundruleconfig?view=azps-2.7.0) for more details. |
| 120 | + |
| 121 | +```azurepowershell-interactive |
| 122 | + $outboundRule = New-AzLoadBalancerOutBoundRuleConfig -Name outboundrule -FrontendIPConfiguration $frontendIPout -BackendAddressPool $bepoolout -Protocol All -IdleTimeoutInMinutes 15 -AllocatedOutboundPort 10000 |
| 123 | +``` |
| 124 | +If you do not want to use a separate outbound pool, you can change the address pool argument in the preceding command to specify *$bepoolin* instead. We recommend to use separate pools for flexibility and readability of the resulting configuration. |
| 125 | + |
| 126 | +### Create Load Balancer |
| 127 | + |
| 128 | +Create a Load Balancer with the inbound IP address using [New-AzLoadBalancer](https://docs.microsoft.com/powershell/module/az.network/new-azloadbalancer?view=azps-2.6.0) named *lb* that includes an inbound frontend IP configuration and a backend pool *bepoolinbound* that is associated with the public IP address *mypublicipinbound* that you created in the preceding step. |
| 129 | + |
| 130 | +```azurepowershell-interactive |
| 131 | +New-AzLoadBalancer -Name lb -Sku Standard -ResourceGroupName myresourcegroupoutbound -Location eastus -FrontendIpConfiguration $frontendIPin,$frontendIPout -BackendAddressPool $bepoolin,$bepoolout -Probe $probe -LoadBalancingRule $inboundrule -OutboundRule $outboundrule |
| 132 | +``` |
| 133 | + |
| 134 | +At this point, you can proceed with adding your VM's to the backend pool *bepoolinbound* __and__ *bepooloutbound* by updating the IP configuration of the respective NIC resources using [Add-AzNetworkInterfaceIpConfig](https://docs.microsoft.com/cli/azure/network/lb/rule?view=azure-cli-latest). |
| 135 | + |
| 136 | +## Clean up resources |
| 137 | + |
| 138 | +When no longer needed, you can use the [Remove-AzResourceGroup](https://docs.microsoft.com/powershell/module/az.resources/remove-azresourcegroup?view=azps-2.7.0) command to remove the resource group, load balancer, and all related resources. |
| 139 | + |
| 140 | +```azurepowershell-interactive |
| 141 | + Remove-AzResourceGroup -Name myresourcegroupoutbound |
| 142 | +``` |
| 143 | + |
| 144 | +## Next steps |
| 145 | +In this article, you created Standard Load Balancer, configured both inbound load balancer traffic rules, configured and health probe for the VMs in the backend pool. To learn more about Azure Load Balancer, continue to the tutorials for Azure Load Balancer. |
| 146 | + |
| 147 | +> [!div class="nextstepaction"] |
| 148 | +> [Azure Load Balancer tutorials](tutorial-load-balancer-standard-public-zone-redundant-portal.md) |
0 commit comments