0
0
Azurecloud~5 mins

Load balancing rules in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Load balancing rules in Azure help distribute incoming network traffic evenly across multiple servers. This prevents any single server from getting overwhelmed and keeps your app running smoothly.
When you want to share user requests between several web servers to avoid overload.
When you have multiple virtual machines running the same service and want to balance traffic.
When you need to ensure high availability by redirecting traffic if one server fails.
When you want to manage traffic on specific ports like HTTP or HTTPS across servers.
When you want to improve app performance by spreading workload evenly.
Config File - loadbalancer-rule.json
loadbalancer-rule.json
{
  "type": "Microsoft.Network/loadBalancers/loadBalancingRules",
  "apiVersion": "2023-05-01",
  "name": "myLoadBalancingRule",
  "properties": {
    "frontendIPConfiguration": {
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Network/loadBalancers/myLoadBalancer/frontendIPConfigurations/myFrontEnd"
    },
    "backendAddressPool": {
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Network/loadBalancers/myLoadBalancer/backendAddressPools/myBackEndPool"
    },
    "probe": {
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Network/loadBalancers/myLoadBalancer/probes/myHealthProbe"
    },
    "protocol": "Tcp",
    "loadDistribution": "Default",
    "frontendPort": 80,
    "backendPort": 80,
    "enableFloatingIP": false,
    "idleTimeoutInMinutes": 4,
    "enableTcpReset": true
  }
}

This JSON defines a load balancing rule for an Azure Load Balancer.

  • frontendIPConfiguration: The public IP or frontend IP where traffic arrives.
  • backendAddressPool: The group of virtual machines receiving the traffic.
  • probe: Health check to ensure backend VMs are healthy.
  • protocol: Network protocol used (TCP here).
  • frontendPort and backendPort: Ports on frontend and backend.
  • loadDistribution: How traffic is distributed (Default means even).
  • enableFloatingIP: Whether to allow direct server return traffic.
  • idleTimeoutInMinutes: Timeout for idle connections.
  • enableTcpReset: Whether to reset TCP connections on timeout.
Commands
This command creates a load balancing rule on the Azure Load Balancer named 'myLoadBalancer' in the resource group 'example-rg'. It sets TCP traffic on port 80 to be balanced between backend VMs, using a health probe to check VM status.
Terminal
az network lb rule create --resource-group example-rg --lb-name myLoadBalancer --name myLoadBalancingRule --protocol Tcp --frontend-port 80 --backend-port 80 --frontend-ip-name myFrontEnd --backend-pool-name myBackEndPool --probe-name myHealthProbe --idle-timeout 4 --enable-tcp-reset true
Expected OutputExpected
{ "frontendPort": 80, "backendPort": 80, "enableFloatingIP": false, "enableTcpReset": true, "idleTimeoutInMinutes": 4, "loadDistribution": "Default", "name": "myLoadBalancingRule", "protocol": "Tcp", "provisioningState": "Succeeded" }
--frontend-port - Port on the load balancer to listen on
--backend-port - Port on backend VMs to forward traffic to
--probe-name - Health probe to check backend VM health
This command lists all load balancing rules configured on the load balancer to verify the rule was created successfully.
Terminal
az network lb rule list --resource-group example-rg --lb-name myLoadBalancer
Expected OutputExpected
[{ "name": "myLoadBalancingRule", "protocol": "Tcp", "frontendPort": 80, "backendPort": 80, "provisioningState": "Succeeded" }]
Key Concept

If you remember nothing else from this pattern, remember: load balancing rules connect incoming traffic on a frontend IP and port to backend servers, using health probes to keep traffic flowing only to healthy servers.

Common Mistakes
Not specifying the correct frontend IP configuration name when creating the rule.
The load balancer won't know where to listen for incoming traffic, so the rule won't work.
Always use the exact frontend IP configuration name that exists on your load balancer.
Using different ports for frontend and backend without a clear reason.
This can cause confusion and traffic may not reach the backend service correctly.
Keep frontend and backend ports the same unless you have a specific need to translate ports.
Skipping the health probe configuration.
Without health probes, traffic may be sent to unhealthy or offline backend servers.
Always configure a health probe and link it to your load balancing rule.
Summary
Create a load balancing rule to direct traffic from a frontend IP and port to backend VMs.
Use health probes to ensure traffic only goes to healthy backend servers.
Verify the rule creation by listing load balancing rules on the load balancer.