Azure Load Balancer is a Layer-4 (TCP, UDP) load balancer that distributes incoming traffic among healthy instances of services defined in a backend pool. It is an essential component of any highly available and scalable solution in Azure. Azure Load Balancer provides high availability by distributing incoming traffic among multiple backend instances, which ensures that if one instance goes down, the traffic will be redirected to another available instance.

Azure Load Balancer supports cross-regional deployment, which means you can deploy your services in multiple regions and distribute traffic among them using a single Azure Load Balancer.

In this tutorial, we’ll cover the following topics:

  1. Create an Azure Load Balancer
  2. Add backend instances to the load balancer
  3. Configure a health probe for the backend instances
  4. Create a load balancer rule
  5. Test the load balancer

We’ll be using Azure CLI commands to perform these tasks.

Prerequisites

Before you begin, make sure you have the following prerequisites:

  1. An Azure account with permissions to create resources
  2. Azure CLI installed on your computer
  3. Basic knowledge of Azure Load Balancer and its components

1. Create an Azure Load Balancer

To create an Azure Load Balancer, follow these steps:

  1. Open a terminal window or command prompt.
  2. Log in to your Azure account using the following command:
az login
  1. Create a resource group in which you will create the Azure Load Balancer. Use the following command to create a resource group:
az group create --name MyResourceGroup --location eastus

Replace “MyResourceGroup” with a unique name for your resource group, and “eastus” with the location where you want to create the resource group.

  1. Create an Azure Load Balancer using the following command:
az network lb create --name MyLoadBalancer --resource-group MyResourceGroup --location eastus --sku Standard

Replace “MyLoadBalancer” with a unique name for your load balancer. The “sku Standard” parameter specifies that we want to use the standard SKU for the load balancer.

2. Add backend instances to the load balancer

To add backend instances to the load balancer, follow these steps:

  1. Create a virtual network in which you will create the backend instances. Use the following command to create a virtual network:
az network vnet create --name MyVNet --resource-group MyResourceGroup --location eastus --address-prefixes 10.0.0.0/16 --subnet-name MySubnet --subnet-prefixes 10.0.0.0/24

Replace “MyVNet” with a unique name for your virtual network, and “MySubnet” with a unique name for your subnet.

  1. Create a network security group for the backend instances. Use the following command to create a network security group:
az network nsg create --name MyNSG --resource-group MyResourceGroup --location eastus

Replace “MyNSG” with a unique name for your network security group.

  1. Create a virtual machine that will serve as a backend instance for the load balancer. Use the following command to create a virtual machine:
az vm create --resource-group MyResourceGroup --name MyVM1 --image UbuntuLTS --admin-username azureuser --generate-ssh-keys --vnet-name MyVNet --subnet MySubnet --nsg MyNSG

Replace “MyVM1” with a unique name for your virtual machine.

  1. Add the backend instance to the backend pool of the Azure Load Balancer using the following command:
az network lb address-pool create --name MyBackendPool --lb-name MyLoadBalancer --resource-group MyResourceGroup
  1. Add the virtual machine to the backend pool using the following command:
az network nic ip-config address-pool add --address-pool MyBackendPool --name ipconfig1 --nic-name MyVM1VMNic --lb-name MyLoadBalancer --resource-group MyResourceGroup

Replace “ipconfig1” with a unique name for your IP configuration.

  1. Create additional virtual machines that will serve as backend instances for the load balancer, following the same steps as above. Add each virtual machine to the backend pool using the “az network nic ip-config address-pool add” command.

3. Configure a health probe for the backend instances

To configure a health probe for the backend instances, follow these steps:

  1. Create a health probe using the following command:
az network lb probe create --name MyHealthProbe --resource-group MyResourceGroup --lb-name MyLoadBalancer --protocol tcp --port 80

Replace “MyHealthProbe” with a unique name for your health probe.

  1. Associate the health probe with the backend pool using the following command:
az network lb rule create --name MyLoadBalancerRule --resource-group MyResourceGroup --lb-name MyLoadBalancer --protocol tcp --frontend-port 80 --backend-port 80 --probe-name MyHealthProbe --backend-pool-name MyBackendPool

Replace “MyLoadBalancerRule” with a unique name for your load balancer rule.

4. Create a load balancer rule

To create a load balancer rule, follow these steps:

  1. Create a frontend IP configuration for the load balancer using the following command:
az network public-ip create --name MyPublicIP --resource-group MyResourceGroup --location eastus --allocation-method Static

Replace “MyPublicIP” with a unique name for your public IP address.

  1. Create a frontend IP configuration for the load balancer using the following command:
az network lb frontend-ip create --name MyFrontendIP --lb-name MyLoadBalancer --resource-group MyResourceGroup --public-ip-address MyPublicIP

Replace “MyFrontendIP” with a unique name for your frontend IP configuration.

  1. Create a load balancer rule using the following command:
az network lb rule create --name MyLoadBalancerRule --resource-group MyResourceGroup --lb-name MyLoadBalancer --protocol tcp --frontend-port 80 --backend-port 80 --frontend-ip-name MyFrontendIP --backend-pool-name MyBackendPool --probe-name MyHealthProbe

Replace “MyLoadBalancerRule” with a unique name for your load balancer rule.

5. Test the load balancer

To test the load balancer, follow these steps:

  1. Connect to one of the virtual machines using SSH or RDP.
  2. Start a web server on the virtual machine using the following command:
sudo apt-get update
sudo apt-get install apache2 -y
sudo systemctl start apache2
  1. Open a web browser and enter the public IP address of the load balancer. You should see the default Apache web page served by one of the backend instances.
  2. Refresh the web page multiple times to see the load balancer distributing traffic among the backend instances.

Congratulations! You have successfully created an Azure cross regional load balancer using Azure CLI commands.