Tutorial: Create a cross-region Azure Load Balancer using Azure CLI

A cross-region load balancer ensures a service is available globally across multiple Azure regions. If one region fails, the traffic is routed to the next closest healthy regional load balancer.

In this tutorial, you learn how to:

  • Create cross-region load balancer.
  • Create a load balancer rule.
  • Create a backend pool containing two regional load balancers.
  • Test the load balancer.

If you don’t have an Azure subscription, create a free account before you begin.

Prerequisites

  • An Azure subscription.
  • Two standard sku Azure Load Balancers with backend pools deployed in two different Azure regions.
  • Azure CLI installed locally or Azure Cloud Shell.

If you choose to install and use the CLI locally, this quickstart requires Azure CLI version 2.0.28 or later. To find the version, run az --version. If you need to install or upgrade, see Install the Azure CLI.

Sign in to Azure CLI

Sign in to Azure CLI:

az login

Create cross-region load balancer

In this section, you'll create a cross-region load balancer, public IP address, and load balancing rule.

Create a resource group

An Azure resource group is a logical container into which Azure resources are deployed and managed.

Create a resource group with az group create:

  • Named myResourceGroupLB-CR.
  • In the westus location.
  az group create \
    --name myResourceGroupLB-CR \
    --location westus

Create the load balancer resource

Create a cross-region load balancer with az network cross-region-lb create:

  • Named myLoadBalancer-CR.
  • A frontend pool named myFrontEnd-CR.
  • A backend pool named myBackEndPool-CR.
  az network cross-region-lb create \
    --name myLoadBalancer-CR \
    --resource-group myResourceGroupLB-CR \
    --frontend-ip-name myFrontEnd-CR \
    --backend-pool-name myBackEndPool-CR     

Create the load balancer rule

A load balancer rule defines:

  • Frontend IP configuration for the incoming traffic.
  • The backend IP pool to receive the traffic.
  • The required source and destination port.

Create a load balancer rule with az network cross-region-lb rule create:

  • Named myHTTPRule-CR
  • Listening on Port 80 in the frontend pool myFrontEnd-CR.
  • Sending load-balanced network traffic to the backend address pool myBackEndPool-CR using Port 80.
  • Protocol TCP.
  az network cross-region-lb rule create \
    --backend-port 80 \
    --frontend-port 80 \
    --lb-name myLoadBalancer-CR \
    --name myHTTPRule-CR \
    --protocol tcp \
    --resource-group myResourceGroupLB-CR \
    --backend-pool-name myBackEndPool-CR \
    --frontend-ip-name myFrontEnd-CR

Create backend pool

In this section, you'll add two regional standard load balancers to the backend pool of the cross-region load balancer.

Important

To complete these steps, ensure that two regional load balancers with backend pools have been deployed in your subscription. For more information, see, Quickstart: Create a public load balancer to load balance VMs using Azure CLI.

Add the regional frontends to load balancer

In this section, you'll place the resource IDs of two regional load balancers frontends into variables. You'll then use the variables to add the frontends to the backend address pool of the cross-region load balancer.

Retrieve the resource IDs with az network lb frontend-ip show.

Use az network cross-region-lb address-pool address add to add the frontends you placed in variables in the backend pool of the cross-region load balancer:

  region1id=$(az network lb frontend-ip show \
    --lb-name myLoadBalancer-R1 \
    --name myFrontEnd-R1 \
    --resource-group CreatePubLBQS-rg-r1 \
    --query id \
    --output tsv)

  az network cross-region-lb address-pool address add \
    --frontend-ip-address $region1id \
    --lb-name myLoadBalancer-CR \
    --name myFrontEnd-R1 \
    --pool-name myBackEndPool-CR \
    --resource-group myResourceGroupLB-CR

  region2id=$(az network lb frontend-ip show \
    --lb-name myLoadBalancer-R2 \
    --name myFrontEnd-R2 \
    --resource-group CreatePubLBQS-rg-r2 \
    --query id \
    --output tsv)
  
  az network cross-region-lb address-pool address add \
    --frontend-ip-address $region2id \
    --lb-name myLoadBalancer-CR \
    --name myFrontEnd-R2 \
    --pool-name myBackEndPool-CR \
    --resource-group myResourceGroupLB-CR

Test the load balancer

In this section, you'll test the cross-region load balancer. You'll connect to the public IP address in a web browser. You'll stop the virtual machines in one of the regional load balancer backend pools and observe the failover.

  1. To get the public IP address of the load balancer, use az network public-ip show:

      az network public-ip show \
        --resource-group myResourceGroupLB-CR \
        --name PublicIPmyLoadBalancer-CR \
        --query ipAddress \
        --output tsv
    
  2. Copy the public IP address, and then paste it into the address bar of your browser. The default page of IIS Web server is displayed on the browser.

  3. Stop the virtual machines in the backend pool of one of the regional load balancers.

  4. Refresh the web browser and observe the failover of the connection to the other regional load balancer.

Clean up resources

When no longer needed, use the az group delete command to remove the resource group, load balancer, and all related resources.

  az group delete \
    --name myResourceGroupLB-CR

Next steps

In this tutorial, you:

  • Created a cross-region load balancer.
  • Created a load-balancing rule.
  • Added regional load balancers to the backend pool of the cross-region load balancer.
  • Tested the load balancer.

Advance to the next article to learn how to...