Skip to content

Instantly share code, notes, and snippets.

@devopsinsiders
Created September 10, 2023 08:27
Show Gist options
  • Save devopsinsiders/41336a719fe6a04297827e8d5935a18d to your computer and use it in GitHub Desktop.
Save devopsinsiders/41336a719fe6a04297827e8d5935a18d to your computer and use it in GitHub Desktop.
Load Balancing In Azure 🏒

Azure Load Balancer πŸ”„

Azure Load Balancer is a Layer-4 (Transport Layer) load balancing service that distributes incoming network traffic (such as HTTP, HTTPS, TCP, and UDP) across multiple virtual machines (VMs) or instances to ensure proper resource utilization and fault tolerance. It helps you build highly available applications by providing high availability, scalability, and performance.

🌐 Traffic Distribution: It uses various load-balancing algorithms like round-robin, least-connections, and more to distribute traffic evenly among backend resources.

πŸ” Frontend and Backend Configuration: The frontend defines the entry point (IP address and port) for incoming traffic. The backend defines the target resources (VMs, VMSS, or IP addresses) that will receive the traffic.

🏒 Availability Sets and Availability Zones: You can use Azure Availability Sets or Availability Zones in conjunction with Azure Load Balancer to distribute traffic across multiple fault domains for improved high availability.

❀️ Health Probing: Azure Load Balancer constantly monitors the health of backend resources by sending health probes (e.g., HTTP requests) and directs traffic only to healthy resources.

πŸ”„ Session Persistence: It supports session affinity (sticky sessions) to maintain client-to-backend affinity based on client IP or a session cookie.

πŸ“₯ Inbound and Outbound Load Balancing: Azure Load Balancer can be used for both incoming (ingress) and outgoing (egress) traffic.

🌐 Public and Private Load Balancers: You can configure public IP addresses for internet-facing applications or private IP addresses for internal applications.

Azure Application Gateway πŸš€

Azure Application Gateway is a Layer-7 (Application Layer) load balancing service that is specifically designed for handling HTTP/HTTPS traffic. It offers advanced routing, SSL termination, web application firewall (WAF) capabilities, and more, making it suitable for web applications with complex traffic management requirements.

🌐 HTTP Load Balancing: It intelligently routes traffic based on URL path, host headers, and other HTTP attributes.

πŸ” SSL Offloading: Application Gateway can handle SSL/TLS encryption and decryption, reducing the workload on backend servers.

πŸ›‘οΈ Web Application Firewall (WAF): Provides protection against common web application vulnerabilities and attacks.

πŸͺ Cookie-Based Affinity: Supports session affinity based on HTTP cookies.

πŸ”„ Rewrite and Redirection: You can perform URL rewrite and redirection for incoming requests.

πŸš€ Auto-Scaling: Application Gateway can automatically scale based on demand to handle traffic spikes.

Azure Front Door 🌍

Azure Front Door is a global content delivery network (CDN) and Layer-7 load balancing service that focuses on accelerating the delivery of web applications and content to users worldwide. It is designed for high-performance, low-latency, and global scale.

🌐 Global Load Balancing: Front Door uses anycast routing to direct user requests to the nearest and healthiest backend based on factors like latency, health, and routing rules.

πŸ›‘οΈ Web Application Firewall (WAF): Offers WAF capabilities for security against web threats.

πŸ” SSL Acceleration: Handles SSL/TLS termination and acceleration.

🌍 Traffic Routing and Splitting: Provides sophisticated traffic routing based on URL path, geographic location, and other criteria.

πŸ“¦ Caching and Acceleration: Front Door accelerates content delivery through caching.

πŸ“Š Monitoring and Analytics: Offers detailed analytics and monitoring to gain insights into traffic patterns.

In summary, Azure Load Balancer, Azure Application Gateway, and Azure Front Door are Azure's load balancing services, each tailored to specific use cases and traffic management needs. Azure Load Balancer operates at the network layer, Application Gateway at the application layer, and Front Door at the content delivery and global traffic management layer. Depending on your application's requirements, you can choose the most appropriate load balancing service or use them in combination to achieve optimal performance, availability, and security. πŸš€πŸŒπŸŒ

Terraform Azure Load Balancer Configuration πŸš€

This Terraform code deploys an Azure Load Balancer (LB) with associated resources, such as a Public IP, backend address pools, probes, and rules. This configuration is designed to set up a basic load balancing environment for your applications.

Prerequisites βœ…

Before you can use this Terraform configuration, make sure you have the following prerequisites in place:

  1. Azure Account: You need an active Azure account. πŸ‘©β€πŸ’»

  2. Terraform Installed: Ensure Terraform is installed on your local machine or the environment where you plan to run this code. You can download Terraform from terraform.io. 🌍

  3. Azure CLI: Make sure you have the Azure CLI installed and configured with your Azure subscription. You can install it from here. πŸ“¦

Configuration βš™οΈ

The provided Terraform configuration deploys the following Azure resources:

  • Public IP Address 🌐
  • Azure Load Balancer 🏒
  • Backend Address Pools πŸ“š
  • Load Balancer Probes πŸ”
  • Load Balancer Rules πŸ“

Variables

Before running Terraform, you must customize the configuration by setting values for the following variables:

  • var.location: The Azure region where you want to deploy the resources. πŸ—ΊοΈ

  • var.rg_name: The name of the Azure resource group where the resources will be created. 🏒

Please make sure to set these variables to match your desired configuration. πŸ› οΈ

Resources 🧰

Public IP Address 🌐

resource "azurerm_public_ip" "PublicIPForLB" {
  name                = "PublicIPForLB"
  location            = var.location
  resource_group_name = var.rg_name
  allocation_method   = "Static"
  sku                 = "Standard"
}
  • This resource block defines a static Public IP address that will be associated with the Load Balancer. 🌟

Azure Load Balancer 🏒

resource "azurerm_lb" "NginxLoadBalancer" {
  name                = "NginxLoadBalancer"
  location            = var.location
  resource_group_name = var.rg_name
  sku                 = "Standard"
  frontend_ip_configuration {
    name                 = "PublicIPAddress"
    public_ip_address_id = azurerm_public_ip.PublicIPForLB.id
  }
}
  • This resource block defines the Azure Load Balancer. πŸ”„

  • It associates the previously created Public IP address with the Load Balancer. 🌍

Backend Address Pools πŸ“š

resource "azurerm_lb_backend_address_pool" "BackEndAddressPool" {
  loadbalancer_id = azurerm_lb.NginxLoadBalancer.id
  name            = "BackEndAddressPool"
}
  • This resource block defines a backend address pool where backend virtual machines are added. The Load Balancer will distribute traffic to the virtual machines in this pool. 🎯

Backend Address Pool Addresses πŸ™οΈ

resource "azurerm_lb_backend_address_pool_address" "backendnginx01" {
  name                    = "backendnginx01"
  backend_address_pool_id = azurerm_lb_backend_address_pool.BackEndAddressPool.id
  virtual_network_id      = data.azurerm_virtual_network.lb-demo.id
  ip_address              = data.azurerm_virtual_machine.nginxvm01.private_ip_address
}

resource "azurerm_lb_backend_address_pool_address" "backendnginx02" {
  name                    = "backendnginx02"
  backend_address_pool_id = azurerm_lb_backend_address_pool.BackEndAddressPool.id
  virtual_network_id      = data.azurerm_virtual_network.lb-demo.id
  ip_address              = data.azurerm_virtual_machine.nginxvm02.private_ip_address
}
  • These resource blocks define the backend virtual machines that will receive traffic from the Load Balancer. βš™οΈ

  • Replace the ip_address values with the private IP addresses of your backend virtual machines. πŸ”

Load Balancer Probe πŸ”

resource "azurerm_lb_probe" "nginxprobe" {
  loadbalancer_id = azurerm_lb.NginxLoadBalancer.id
  name            = "http-port"
  port            = 80
}
  • This resource block defines a probe to monitor the health of the backend resources. It checks the availability of port 80 on the backend virtual machines. πŸ”„

Load Balancer Rule πŸ“

resource "azurerm_lb_rule" "example" {
  loadbalancer_id                = azurerm_lb.NginxLoadBalancer.id
  name                           = "NginxRule"
  protocol                       = "Tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = "PublicIPAddress"
  backend_address_pool_ids       = [azurerm_lb_backend_address_pool.BackEndAddressPool.id]
  probe_id                       = azurerm_lb_probe.nginxprobe.id
}
  • This resource block defines a Load Balancer rule that directs incoming traffic on port 80 to the backend address pool. πŸ“œ

  • It also associates the previously defined probe to monitor the health of backend resources. 🩺

Usage πŸš€

Follow these steps to deploy the resources using Terraform:

  1. Clone the Repository: Clone this repository to your local machine or the environment where you plan to run Terraform. πŸ“‚

  2. Initialize Terraform: Run the following command to initialize Terraform and download the required providers:

    terraform init
  3. Review the Plan: Run the following command to see what Terraform plans to do:

    terraform plan

    This step will provide you with a summary of the changes that will be made to your Azure environment. πŸ“‹

  4. Apply the Configuration: If the plan looks correct, apply the configuration to create the Azure resources:

    terraform apply
  5. Cleanup (Optional): If you want to remove the resources created by Terraform, you can run the following command:

    terraform destroy

Additional Information ℹ️

  • The Load Balancer is configured to route traffic on port 80 to the backend address pools defined in the configuration. 🌐

  • Probes are used to monitor the health of backend resources. πŸ”

  • Rules define how traffic is distributed to the backend pools based on the probe status. πŸ“Š

resource "azurerm_public_ip" "PublicIPForLB" {
name = "PublicIPForLB"
location = var.location
resource_group_name = var.rg_name
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_lb" "NginxLoadBalancer" {
name = "NginxLoadBalancer"
location = var.location
resource_group_name = var.rg_name
sku = "Standard"
frontend_ip_configuration {
name = "PublicIPAddress"
public_ip_address_id = azurerm_public_ip.PublicIPForLB.id
}
}
resource "azurerm_lb_backend_address_pool" "BackEndAddressPool" {
loadbalancer_id = azurerm_lb.NginxLoadBalancer.id
name = "BackEndAddressPool"
}
resource "azurerm_lb_backend_address_pool_address" "backendnginx01" {
name = "backendnginx01"
backend_address_pool_id = azurerm_lb_backend_address_pool.BackEndAddressPool.id
virtual_network_id = data.azurerm_virtual_network.lb-demo.id
ip_address = data.azurerm_virtual_machine.nginxvm01.private_ip_address
}
resource "azurerm_lb_backend_address_pool_address" "backendnginx02" {
name = "backendnginx02"
backend_address_pool_id = azurerm_lb_backend_address_pool.BackEndAddressPool.id
virtual_network_id = data.azurerm_virtual_network.lb-demo.id
ip_address = data.azurerm_virtual_machine.nginxvm02.private_ip_address
}
resource "azurerm_lb_probe" "nginxprobe" {
loadbalancer_id = azurerm_lb.NginxLoadBalancer.id
name = "http-port"
port = 80
}
resource "azurerm_lb_rule" "example" {
loadbalancer_id = azurerm_lb.NginxLoadBalancer.id
name = "NginxRule"
protocol = "Tcp"
frontend_port = 80
backend_port = 80
frontend_ip_configuration_name = "PublicIPAddress"
backend_address_pool_ids = [azurerm_lb_backend_address_pool.BackEndAddressPool.id]
probe_id = azurerm_lb_probe.nginxprobe.id
}
data "azurerm_virtual_network" "lb-demo" {
name = var.virtual_network_name
resource_group_name = var.rg_name
}
data "azurerm_virtual_machine" "nginxvm01" {
name = "nginxvm01"
resource_group_name = var.rg_name
}
data "azurerm_virtual_machine" "nginxvm02" {
name = "nginxvm02"
resource_group_name = var.rg_name
}
data "azurerm_virtual_machine" "nginxvm02" {
foreach = toset(["nginxvm01", "nginxvm02"])
name = "nginxvm02"
resource_group_name = var.rg_name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment