Creating a Load Balancer in Azure Cloud

Load balancers allow for more efficient distribution of incoming network traffic across a group of servers or resources. They are useful for high throughput traffic environments. In this blog I will show how to setup a load balancer in Azure Cloud. In Azure’s Load Balancer environment it’s recommended for regional non-HTTP(s) traffic. If you would like to setup a load balancer for a website, try using Application Gateway or Azure Front Door instead.

Here are few terminologies for Load Balancing:

  1. Backend Pool: A group of VMs that will handle traffic requests
  2. Public Load Balancer: Uses a static public IP
  3. Internal Load Balancer: Uses a private IP
  4. Health Probes: Monitoring backend pool health
  5. Load Balancing Rules: Traffic distribution to bakcend pool
  6. Inbound NAT rules: binding of specific IP/port to specific instance
  7. Outbound: Controls outbound rules
  8. Disaster Recovery: Regaining access to a server or infrastructure after a disruption

Today we will create a Load Balancer using three Windows VMs, the topology will look something like this:

Credits: Adam Marczak

First, we will have to create three Windows VMs in the same resource groups. A resource group is used to hold multiple virtual machines. I named my resource group Test1

VM1, VM2, VM3 are all Windows Server VMs located in Canada Central.

Based on the topology above, we will not be assigning public IPs because we are assigning them to the load balancer. Make sure we do not assign a Public IP to the VMs. Also, we will have to put the VMs in the same virtual network so they can communicate with each other

Creating the Load Balancer

To create a load balancer, press on “create a resource” in the Azure home screen portal and search load balancer.

  1. Put your loadbalancer in the same resource group as your VMs. The region I picked for the VMs is Canada Central, therefore the load balancer should be in the same region as well. We are also assigning a public IP to the load balancer, so we will have to make it public

2. Press next, and now we will now have to create a front-end IP for the Load Balancer. Click on Add a frontend IP.

3. I named my Frontend IP to Harvey1FrontEnd and created a new public IP address and named it Test1-IP

4. We will now have to add a backend pool. Remember that the backend pool are the three VMs I created earlier. So name the backend pool, choose the Virtual Network the three VMs were using, and then select the three VMs as the backend pool

This is what it looks like after I selected those three VMs as my backend pool:

5. Hit next and for Inbound rules, we will have to create a load balancer rule to test the load balancer. Click on Add a load balancer rule. I named it Harvey1-Rule. I also assigned the Frontend IP address as the one I just created. The backend is also the backend pool I just created as well. We will also have to create a health probe used to monitor the load balancer in the next step. I will leave everything else as default

6. We will also create a health probe as well for the load balancer, I named it harvey1-health-probe and used the TCP protocol over port 80. It will check the load balancer every 5 seconds and will consider it unhealthy after two consecutive failures

7. After you created the load balancing rule, you can click on next and create the load balancer. Azure will go through a validation test before you can create it.

8. It could take up to 10 minutes to deploy. Now we will have to test the load balancer to see if it works. To do this we will have to run a script in CLI bash to install a Microsoft web server on all three VMs

  • Open Azure CLI on the top right hand corner of the Azure portal and enter this code in and hit enter:
for NUM in 1 2 3
do
  az vm extension set \
    --name CustomScriptExtension \
    --vm-name VM$NUM \
    -g Test1 \
    --publisher Microsoft.Compute \
    --version 1.8 \
    --settings '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
done

#Your group will be the resource group, MINE is Test1
#This uses a for loop so make sure to standardize your vm-name so the script can run

9. Once the script is done running, go to your Load Balancer, and find the public IP of your load balancer. Mine is 20.63.96.197

10. Open a web browser tab and go enter the IP address in the address bar. It should bring you to a website listing one of your VM names:

http://20.63.96.197/

11. We can also simulate a disaster recovery as well. To do this, go into VM2 and stop services. It should redirect you to VM3 or VM1 instead. In my case, it redirected me to VM3

AWESOME, it looks like your load balancer is now working!!!

Let me know if anyone has any questions, please comment below

– Harvey

Add a Comment

Your email address will not be published. Required fields are marked *