How to create an Azure Load Balancer with PowerShell
This post is part of a series in which I will show how to create different resources in Microsoft Azure. Today I will show you how to create an essential component: an Azure Load Balancer.
Azure Load Balancer allows you to scale your applications and create high availability for your services. Load Balancer can be used for inbound as well as outbound scenarios and provides low latency, high throughput, and scales up to millions of flows for all TCP and UDP applications.
This tutorial assumes that you already have a Microsoft Azure account set up. Important: Before deploying and testing your Load balancer, you must create network resources: virtual network and virtual NICs.
Azure PowerShell Workaround #
If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.
Login-AzAccount
This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task we will use the following commands:
Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"
Once you set your default subscription, you’re ready to start.
Set the variables #
Here, we define the characteristics of our environment.
$resourcegroup = "RG-AZWE"
$location = "westeurope"
ResourceGroup
With the following command in PowerShell, we obtain the list of existing resource groups in your subscription.
Get-AzResourceGroup `
| Select-Object ResourceGroupName, Location
If you need to create a new resource group, check out this link.
Location
With the following cmdlet in PowerShell, we obtain the list of existing locations in Azure.
Get-AzLocation `
| Select-Object DisplayName, Location
Creates a front-end IP configuration for a load balancer #
Since we are going to create a public load balancer, we will need to have a public IP. The following command creates a new public IP address resource, which we will use in the load balancer. In the case of an internal load balancer, this step is not necessary.
New-AzPublicIpAddress `
-Name <String> `
-ResourceGroupName <String> `
-Location <String> `
-AllocationMethod <String>
The New-AzLoadBalancerFrontendIpConfig cmdlet creates an IP front-end configuration using the public IP address created in the previous step.
New-AzLoadBalancerFrontendIpConfig `
-Name <String> `
-PublicIpAddressId <String>
#
Creates a backend address pool configuration for a load balancer #
The New-AzLoadBalancerBackendAddressPoolConfig cmdlet creates a backend address pool configuration for a load balancer.
New-AzLoadBalancerBackendAddressPoolConfig -Name <String>
#
Creates a probe configuration for a load balancer #
The New-AzLoadBalancerProbeConfig cmdlet creates a probe configuration using the TCP protocol.
New-AzLoadBalancerProbeConfig `
-Name <String> `
-Protocol <String> `
-Port <Int32> `
-IntervalInSeconds <Int32> `
-ProbeCount <Int32>
#
Creates a rule configuration for a load balancer #
The New-AzLoadBalancerRuleConfig cmdlet creates a rule configuration using the variables set above.
New-AzLoadBalancerRuleConfig `
-Name <String> `
-Protocol <String> `
-FrontendPort <Int32> `
-FrontendIpConfiguration <PSFrontendIPConfiguration> `
-BackendPort <Int32> `
-BackendAddressPool <PSBackendAddressPool> `
-Probe <PSProbe>
Once all necessary configurations have been established, you are ready to create a Load Balancer with the New-AzLoadBalancer cmdlet. To create a Virtual Network with PowerShell, use the New-AzLoadBalancer cmdlet with the following syntax:
New-AzLoadBalancer `
-Name <String> `
-ResourceGroupName <String> `
-Location <String> `
-FrontendIpConfiguration <PSFrontendIPConfiguration[]> `
-BackendAddressPool <PSBackendAddressPool[]> `
-LoadBalancingRule <PSLoadBalancingRule[]> `
-Probe <PSProbe[]>
#
We already have a load balancer, it only remains to associate the load balancer with an availability set, a virtual machine or a Virtual machine scale set.
Thanks for reading my post. I hope you find it useful.
If you want to know more about Load Balancer, check out this link.