Introduction
In modern cloud architectures, effective load balancing is crucial for ensuring high availability and optimal performance. Azure provides a suite of load balancing solutions that address different layers of the OSI model:
- Azure Load Balancer operates at layer 4 (TCP/UDP), ideal for fast, high-throughput distribution of network traffic among virtual machines.
- Azure Application Gateway, on the other hand, operates at layer 7 (HTTP/HTTPS) and is specifically designed for web applications, offering advanced features like SSL termination, cookie-based session affinity, and URL-based routing.
- Azure Traffic Manager is a DNS-based traffic distribution solution that directs users to the most appropriate endpoint globally—it’s particularly useful for multi-region deployments and optimizing user experience via geographic routing.
In this post, we’ll walk you through the practical aspects of deploying load balancing solutions for both layer 4 and layer 7 traffic. You’ll see how to set these up using the Azure Portal and using Infrastructure-as-Code (IaC) via Bicep.
Comparing Azure Load Balancing Solutions
Azure Load Balancer (Layer 4)
- What It Does:
Operates at the transport layer to efficiently distribute incoming TCP/UDP traffic among healthy backend instances. - Use Cases:
Ideal for scalable, high-performance workloads where low latency is required. - Implementation:
Configure frontend IPs, backend pools, health probes, and load balancing rules.
Azure Application Gateway (Layer 7)
- What It Does:
Inspects traffic at the HTTP/HTTPS level and provides features like URL-based routing, SSL offloading, and session affinity. - Use Cases:
Best suited for web application scenarios where you need to load balance and secure HTTP traffic. - Implementation:
Define configurations for frontend IPs, listeners, HTTP settings, backend pools, and request routing rules.
Azure Traffic Manager
- What It Does:
Uses DNS-based routing to direct clients to optimal endpoints based on performance, geographic location, or failover policies. - Use Cases:
Excellent for global distribution scenarios, disaster recovery, and performance optimization across regions. - Implementation:
Managed at the DNS level, with health checks and endpoint monitoring.
Setting Up Load Balancing for Layer 4 Traffic
Using the Azure Portal
-
Create a Public Load Balancer:
- Sign in to the Azure Portal.
- Click Create a resource and search for Load Balancer.
- Choose Public as the SKU.
- In the Basics tab, provide a unique name (e.g.,
MyPublicLB
), select your subscription and resource group, and specify your region. - Configure the Frontend IP Configuration by selecting or creating a new Public IP.
- Click Review + create and then Create once validation passes.
-
Configure the Backend Pool and Health Probe:
- After deployment, navigate to your Load Balancer resource.
- Under Settings, select Backend pools; add your virtual machines or scale set instances.
- Under Health Probes, define a probe (e.g., TCP on port 80) to monitor the health of your backend resources.
-
Set Up Load Balancing Rules:
- Under Load balancing rules, create a rule that associates the frontend configuration and the backend pool, specifying ports and protocols (e.g., forwarding TCP port 80).
Using Bicep
Below is a sample Bicep template that provisions an Azure Load Balancer for layer 4 traffic along with a virtual network and backend linux server.
|
|
Deployment Steps:
-
Save the template as
loadBalancerDeployment.bicep
. -
Open your terminal and run:
1 2 3 4
az login az deployment group create \ --resource-group MyResourceGroup \ --template-file loadBalancerDeployment.bicep
Setting Up Load Balancing for Layer 7 Traffic
Using the Azure Portal
-
Create an Application Gateway:
- In the Azure Portal, click Create a resource and search for Application Gateway.
- In the Basics tab, enter details like the name (e.g.,
MyAppGateway
), resource group, region, and select the appropriate SKU (e.g., Standard_v2). - Under Settings, configure the Virtual Network and subnet dedicated for the gateway.
- In Frontend IP configurations, assign a Public IP (create one if necessary).
- Click Review + create, and then Create.
-
Configure HTTP Settings and Listeners:
- After deployment, navigate to your Application Gateway resource.
- Under Backend pools, define the pool of servers (or VMs) where your application resides.
- Configure an HTTP setting (including port, protocol, and session affinity settings).
- Create an HTTP Listener that binds the frontend IP, port, and protocol.
- Under Rules, create a request routing rule to map the listener to the backend pool and HTTP settings.
Using Bicep
Below is a sample Bicep template that deploys a basic Application Gateway for layer 7 load balancing. (Note: Replace the <subnetResourceId>
placeholder with your subnet’s resource ID.)
|
|
Deployment Steps:
-
Save the template as
appGatewayDeployment.bicep
. -
Deploy the template with:
1 2 3 4
az login az deployment group create \ --resource-group MyResourceGroup \ --template-file appGatewayDeployment.bicep
Setting Up Global Load Balancing Traffic
Using the Azure Portal
-
Create an Traffic Manager:
-
In the Azure Portal, click Create a resource and search for Traffic Manager.
-
In the Basics tab, enter details like the name (e.g.,
MyExternalApp
) which needs to be globally unique, resource group, region (This is for the resource element only, the configuration is global), and select the routing method (choose from the below options)- Performance: Distributes traffic requests to the origin with the lowest network latency, rather than the closest geographic location, ensuring optimal performance
- Weighted: Distributes traffic based on assigned weights to each origin. This allows you to control the proportion of traffic sent to different origins, which can be useful for load balancing and testing new deployments.
- Priority: Routes traffic to the primary origin by default. If the primary origin becomes unavailable, traffic is routed to the secondary origin. This ensures high availability and failover capabilities.
- Geographic: Routes traffic based on the geographic location of the user. This method ensures that users are directed to the nearest origin, reducing latency and improving performance.
- Multivalue: Returns multiple IP addresses for a single DNS query. This method allows clients to choose the best IP address to connect to, enhancing redundancy and load distribution.
- Subnet: Routes traffic based on the user’s IP address subnet. This method is useful for directing traffic from specific subnets to designated origins, providing more granular control over traffic routing.
-
Under Settings, configure the Virtual Network and subnet dedicated for the gateway.
-
In Frontend IP configurations, assign a Public IP (create one if necessary).
-
Click Review + create, and then Create.
-
-
Configure HTTP Settings and Listeners:
- After deployment, navigate to your Application Gateway resource.
- Under Backend pools, define the pool of servers (or VMs) where your application resides.
- Configure an HTTP setting (including port, protocol, and session affinity settings).
- Create an HTTP Listener that binds the frontend IP, port, and protocol.
- Under Rules, create a request routing rule to map the listener to the backend pool and HTTP settings.
Using Bicep
Below is a sample Bicep template that deploys a basic Application Gateway for layer 7 load balancing. (Note: Replace the <subnetResourceId>
placeholder with your subnet’s resource ID.)
|
|
Deployment Steps:
-
Save the template as
appGatewayDeployment.bicep
. -
Deploy the template with:
1 2 3 4 5
az login az deployment group create \ --resource-group MyResourceGroup \ --template-file appGatewayDeployment.bicep \ --parameters subnetId="/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name>"
Conclusion
In this post, we explored a range of load balancing solutions available in Azure—from the low-level, high-performance Azure Load Balancer to the feature-rich, application-aware Azure Application Gateway, and the globally distributed Azure Traffic Manager. By examining their differences and deploying examples for both layer 4 and layer 7 traffic, you now have a clearer understanding of how to tailor Azure’s load balancing solutions to meet your application’s needs.
Whether you prefer the hands-on approach using the Azure Portal or a repeatable deployment via Bicep, these tools enhance your network’s resilience and scalability.
Learn More
- Azure Load Balancer:
Load Balancer Overview | Load Balancer Pricing - Azure Application Gateway:
Application Gateway Overview | Application Gateway Pricing - Azure Traffic Manager:
Traffic Manager Overview | Traffic Manager Pricing - Bicep Documentation:
Bicep Language Overview