✓Azure subscription with contributor access, Azure CLI 2.50+, basic understanding of networking (TCP/IP, subnets, NSGs), familiarity with Azure portal and resource groups.
✦ Definition~90s read
What is Azure Load Balancer?
Microsoft Azure — Azure Load Balancer is a core Azure service that handles load balancer in the Microsoft cloud ecosystem.
★
Azure Load Balancer is like having a specialized tool that handles load balancer in the Microsoft cloud — you manage the configuration, Azure handles the infrastructure.
Plain-English First
Azure Load Balancer is like having a specialized tool that handles load balancer in the Microsoft cloud — you manage the configuration, Azure handles the infrastructure.
Azure is Microsoft's cloud computing platform offering over 200 services. This article covers azure load balancer with production-ready configurations, best practices, and hands-on examples.
Azure Load Balancer: The Stateless Workhorse
Azure Load Balancer (ALB) operates at Layer 4 of the OSI model, distributing TCP/UDP traffic across healthy backend instances. Unlike Application Gateway (Layer 7), ALB is stateless—it doesn't inspect payloads. This makes it ideal for high-throughput, low-latency scenarios like database replicas, DNS servers, or any workload where you need raw packet forwarding. ALB supports both public (inbound internet) and internal (private VNet) load balancing. It uses a five-tuple hash (source IP, source port, destination IP, destination port, protocol) to maintain session affinity when configured with 'Client IP' persistence. Crucially, ALB does not terminate TLS; that's a job for a reverse proxy or Application Gateway. In production, you'll often pair ALB with a TLS terminator behind it. ALB's health probes are configurable: HTTP, HTTPS, or TCP. For TCP, a simple SYN-ACK check suffices; for HTTP, you can probe a specific endpoint like /health. Always set a short probe interval (5s) and low unhealthy threshold (2) to detect failures fast. ALB's backend pools can be VMs, VMSS, or IP addresses (including on-prem via VPN). Standard SKU is mandatory for production—Basic SKU lacks availability zones and SLA.
Always use Standard SKU in production. Basic SKU lacks availability zones, has no SLA, and doesn't support NAT gateway or cross-region load balancing. Standard SKU also enables outbound rules and connection draining.
📊 Production Insight
In production, we once saw a 5-minute outage because the health probe interval was set to 30s with threshold 3. A backend VM failed, but traffic kept flowing to it for 90 seconds. Set probe interval to 5s and unhealthy threshold to 2 for sub-10 second failover.
🎯 Key Takeaway
Azure Load Balancer is a Layer 4, stateless load balancer ideal for TCP/UDP traffic with low latency requirements.
thecodeforge.io
Azure Load Balancer
Backend Pools: Choosing the Right Membership Type
Azure Load Balancer supports three backend pool membership types: Virtual Machines, Virtual MachineScale Sets (VMSS), and IP addresses (including on-premises). For production, VMSS is the recommended approach because it integrates with autoscaling and rolling upgrades. When using VMs, you must manually add/remove them from the backend pool—error-prone during scaling events. IP-based pools allow you to target any IP, including on-prem servers via VPN or ExpressRoute, but you lose automatic health probe integration with Azure VMs. For VMSS, ALB automatically adds new instances and removes terminated ones. However, note that ALB does not wait for connection draining before removing an instance—it stops sending new connections immediately. To avoid dropped connections, configure connection draining (idle timeout) on the load balancing rule. The default idle timeout is 4 minutes; set it to match your application's longest request. When using VMSS with a custom image, ensure the health probe endpoint is responsive before the instance is marked healthy. A common pitfall: the application starts listening on port 80 before it's ready to serve traffic, causing 502 errors. Use a startup script that delays health probe response until the app is fully initialized.
ALB does not support true connection draining. When a backend is removed, existing connections are terminated immediately. Use application-level retry logic or set a low idle timeout to minimize impact.
📊 Production Insight
We had a production incident where a VMSS rolling upgrade caused all instances to be replaced simultaneously, dropping all active connections. Solution: use a rolling upgrade with max surge=1 and configure health probe with a grace period.
🎯 Key Takeaway
Use VMSS backend pools for autoscaling and automatic instance management; IP-based pools for hybrid scenarios.
Health Probes: The Canary in the Coal Mine
Health probes are ALB's mechanism to determine backend instance health. Three protocols: TCP, HTTP, HTTPS. TCP probe checks if the port is listening—fast but shallow. HTTP/HTTPS probes check a specific endpoint (e.g., /health) and expect a 200 OK. Always prefer HTTP probes for application-level health. Design your /health endpoint to verify dependencies: database connectivity, cache, disk space. A common mistake is a health endpoint that returns 200 even when the app is broken (e.g., stuck in a deadlock). Implement a liveness check that fails fast. Probe configuration: interval (default 15s), unhealthy threshold (default 2), healthy threshold (default 2). For production, set interval to 5s, unhealthy threshold to 2, healthy threshold to 1. This gives ~10s detection of failure. Be careful with probe port—it must match the backend port unless you use a separate health port. ALB probes originate from Azure's infrastructure IP range (168.63.129.16). Ensure your NSG allows inbound traffic from this IP on the probe port. If you block it, all instances will be marked unhealthy. Also, probes are sent from all Azure regions where the LB is deployed; for cross-region, you need to allow broader ranges.
Azure health probes originate from 168.63.129.16. Add an NSG rule to allow inbound TCP traffic from this IP on the probe port. Without it, all backends will be marked unhealthy.
📊 Production Insight
We once had a cascading failure where a database slowdown caused all health endpoints to timeout, marking all instances unhealthy. The LB stopped sending traffic, and the app went down. Solution: implement circuit breakers in the health endpoint to fail fast, not wait for timeout.
🎯 Key Takeaway
Health probes determine backend availability; use HTTP probes with dependency checks for accurate health signals.
thecodeforge.io
Azure Load Balancer
Load Balancing Rules and Session Persistence
Load balancing rules define how traffic is distributed. Each rule binds a frontend IP:port to a backend pool:port. You can have multiple rules for different protocols (e.g., TCP 80, TCP 443). Session persistence (affinity) is optional: None, Client IP, or Client IP and Protocol. For stateless apps, use None. For stateful apps (e.g., shopping cart), use Client IP. However, Client IP persistence can cause uneven distribution if many users share the same IP (NAT). For better distribution, use Client IP and Protocol, which adds the protocol to the hash. ALB uses a five-tuple hash by default; session persistence modifies the hash to include only the specified fields. Important: session persistence is not sticky sessions in the traditional sense—it's based on the hash, not a cookie. If a backend goes down, the hash is recalculated, and traffic may go to a different backend. For true sticky sessions, use Application Gateway with cookie-based affinity. Also, note that session persistence is per rule, not per backend. If you have multiple rules, each can have its own persistence setting. In production, avoid session persistence for high-availability; design your app to be stateless or use a distributed cache (Redis) for session state.
Enable TCP reset on idle timeout to immediately close connections that exceed the idle timeout. This prevents half-open connections and improves resource cleanup.
📊 Production Insight
We debugged a case where session persistence caused a single backend to receive 80% of traffic because many users were behind the same corporate NAT. Switched to None and used Redis for session state, solving the imbalance.
🎯 Key Takeaway
Load balancing rules map frontend ports to backend pools; session persistence uses hash-based affinity, not cookies.
Outbound Rules: SNAT and Source NAT
Azure Load Balancer provides outbound connectivity for backend instances via Source Network Address Translation (SNAT). When a VM in the backend pool initiates outbound traffic, ALB translates the source IP to the frontend public IP. This is critical for VMs that need to reach the internet (e.g., for updates, external APIs). Outbound rules are configured on the load balancer. You define which frontend IP to use, which backend pool, and the port allocation strategy. The default SNAT port allocation is 1024 ports per VM, but you can adjust with 'Manual' or 'Use Default'. For production, use 'Manual' and allocate based on expected concurrent connections. A common failure: SNAT port exhaustion. Each outbound connection consumes a port; when all ports are used, new connections fail. Symptoms: intermittent connectivity, timeouts. To mitigate, use multiple frontend IPs (up to 16) to increase port pool. Also, consider using Azure NAT Gateway for larger scale—it provides 64,512 ports per IP and scales better. Outbound rules are only available on Standard SKU. Basic SKU uses default outbound access (no control). Always configure explicit outbound rules to avoid relying on default behavior, which can change.
Each outbound connection consumes a SNAT port. Monitor SNAT port usage with Azure Monitor metrics. If usage exceeds 80%, add more frontend IPs or switch to NAT Gateway.
📊 Production Insight
We hit SNAT exhaustion during a Black Friday sale. The fix: added 4 additional frontend IPs and reduced idle timeout to 2 minutes. Also implemented connection pooling in the app to reuse connections.
🎯 Key Takeaway
Outbound rules control SNAT for backend instances; monitor port usage to avoid exhaustion.
High Availability: Availability Zones and Cross-Region
Standard SKU ALB supports availability zones. You can deploy a zone-redundant frontend (spans all zones) or zonal (pinned to one zone). Zone-redundant is recommended for production—it survives a zone outage. Backend VMs should also be distributed across zones. For cross-region load balancing, Azure offers Cross-Region Load Balancer (preview). It distributes traffic across regional ALBs using a global frontend IP. This is useful for active-passive or active-active multi-region deployments. Cross-region LB uses a health probe on the regional LB's frontend. If a region fails, traffic is redirected to the next healthy region. However, cross-region LB does not support session persistence—each request may go to a different region. For stateful apps, use Traffic Manager or Front Door instead. In production, always deploy ALB in a zone-redundant configuration. Also, ensure backend VMs are in the same zones as the frontend to avoid cross-zone latency. For critical workloads, combine ALB with Azure Front Door for global load balancing and DDoS protection.
zone-redundant-alb.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Create zone-redundant publicIP
az network public-ip create \
--resource-group prod-rg \
--name prod-pip \
--sku Standard \
--zone 123
# Create zone-redundant frontend
az network lb frontend-ip create \
--resource-group prod-rg \
--lb-name prod-lb \
--name frontend \
--public-ip-address prod-pip \
--zone 123
# Create backend VMs in each zone
for zone in 123; do
az vm create \
--resource-group prod-rg \
--name vm-zone-$zone \
--zone $zone \
--vnet-name prod-vnet \
--subnet backend-subnet \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
done
Output
{
"publicIP": {
"name": "prod-pip",
"sku": {"name": "Standard"},
"zones": ["1", "2", "3"]
},
"frontendIPConfiguration": {
"name": "frontend",
"zones": ["1", "2", "3"]
}
}
💡Zone-Redundant vs Zonal
Zone-redundant frontend survives a zone outage. Zonal frontend is pinned to one zone—if that zone fails, the LB goes down. Always choose zone-redundant for production.
📊 Production Insight
During a regional outage, our zone-redundant ALB kept serving traffic because backends were in two remaining zones. We had a playbook to manually scale up the healthy zones. Test zone failure scenarios in a non-prod environment.
🎯 Key Takeaway
Use zone-redundant frontend and distribute backends across zones for high availability within a region.
Monitoring and Diagnostics: Logs, Metrics, Alerts
Azure Load Balancer integrates with Azure Monitor for metrics and logs. Key metrics: Packet Count, Byte Count, SNAT Connection Count, Health Probe Status, Data Path Availability. Set alerts on Health Probe Status (if backend count drops below threshold) and SNAT Connection Count (if >80% utilized). For deep diagnostics, enable NSG flow logs and LB logs. LB logs include load balancer rule events and health probe events. However, LB logs are verbose; enable only for troubleshooting. Use Azure Monitor Workbooks to create dashboards. A common issue: data path availability metric shows 0 when the LB frontend is unreachable (e.g., NSG blocking). Always monitor this metric. For proactive monitoring, set up action groups to notify on alert triggers. Also, use Azure Resource Health to check LB health. In production, we recommend a minimum of 90% data path availability alert. For SNAT, set a warning at 70% and critical at 90%. Additionally, log backend instance health changes to detect flapping instances (rapidly toggling healthy/unhealthy). Flapping can indicate misconfigured health probes or application instability.
This metric indicates if the LB frontend is reachable. If it drops to 0, no traffic can flow. Common causes: NSG blocking, public IP deleted, or backend pool empty.
📊 Production Insight
We missed a slow SNAT exhaustion because we only alerted on >90%. By the time alert fired, connections were dropping. Now we alert at 70% and have automated scale-up of frontend IPs.
🎯 Key Takeaway
Monitor health probe status and SNAT connection count; set alerts for proactive incident response.
Troubleshooting Common Failures
Even with proper configuration, things go wrong. Here are common failure modes and how to diagnose them. 1) Backend marked unhealthy: Check NSG rules—ensure 168.63.129.16 is allowed on probe port. Verify the health endpoint returns 200. Use az network lb probe show to see probe status. 2) No traffic to backend: Check load balancing rule mapping. Verify backend pool has instances. Use az network lb list-backend-address-pool to list addresses. 3) Intermittent timeouts: Likely SNAT port exhaustion. Check SnatConnectionCount metric. Increase frontend IPs or reduce idle timeout. 4) Connection resets: TCP reset enabled? If idle timeout is too low, connections may be reset prematurely. Increase idle timeout or disable TCP reset if not needed. 5) Uneven traffic distribution: Session persistence causing imbalance. Check load distribution setting. Use SourceIPProtocol for better distribution. 6) Cross-region issues: Ensure regional LBs are healthy. Cross-region LB uses health probes on regional frontends. If a regional LB is down, traffic fails over. Test failover by stopping a regional LB. Always have a troubleshooting runbook with these steps. Use Azure Network Watcher's IP flow verify to test connectivity. Also, enable diagnostic logs for the LB to capture rule hits.
troubleshoot.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Check health probe status
az network lb probe show \
--resource-group prod-rg \
--lb-name prod-lb \
--name http-probe \
--query "loadBalancingRules[].{name:name, provisioningState:provisioningState}"
# List backend pool addresses
az network lb address-pool list \
--resource-group prod-rg \
--lb-name prod-lb \
--name backend-pool \
--query "loadBalancerBackendAddresses[].ipAddress"
# VerifyNSG allows probe
az network nsg rule list \
--resource-group prod-rg \
--nsg-name backend-nsg \
--query "[?destinationPortRange=='80']"
# CheckSNAT metrics
az monitor metrics list \
--resource /subscriptions/.../loadBalancers/prod-lb \
--metric SnatConnectionCount \
--interval PT1H \
--output table
Output
Backend IPs: 10.0.1.4, 10.0.1.5
NSG Rule: Allow TCP 80 from 168.63.129.16
SNAT Connection Count: 4500 (avg)
⚠ Common Pitfall: Probe Port Mismatch
If your health probe port differs from the backend port, ensure the backend is listening on the probe port. Otherwise, the probe will fail and mark the instance unhealthy.
📊 Production Insight
We once spent hours debugging 'no traffic' only to find the backend pool was empty because a deployment script removed all VMs. Always have a minimum instance count guardrail in your IaC.
Manual creation of ALB is error-prone. Use Infrastructure as Code (IaC) with Terraform or ARM/Bicep. Below is a Terraform example for a production-grade ALB with zone redundancy, health probes, and outbound rules. Key practices: parameterize everything, use remote state, and implement CI/CD pipelines. In the Terraform config, note the use of for_each to create multiple frontend IPs for SNAT. Also, the health probe depends on the backend pool. Always set depends_on explicitly to avoid race conditions. For production, use modules to encapsulate LB configuration. Test changes in a staging environment first. Use Terraform workspaces for environment separation. Also, enable prevent_destroy on critical resources like the LB itself. For secrets (e.g., health endpoint passwords), use Azure Key Vault. Finally, integrate with Azure Policy to enforce SKU Standard and zone-redundancy. This ensures compliance across all subscriptions.
Store Terraform state in Azure Storage with encryption and locking. Never use local state for production. Use azurerm_backend_container for remote state.
📊 Production Insight
We had a deployment that accidentally deleted the LB because Terraform state was stale. Now we use prevent_destroy on critical resources and run terraform plan in CI with manual approval.
🎯 Key Takeaway
Use IaC (Terraform/Bicep) for repeatable, auditable ALB deployments; parameterize and use remote state.
Security: NSGs, DDoS, and Private Endpoints
Azure Load Balancer itself is not a security appliance—it's a traffic distributor. Security must be implemented at the network and application layers. First, Network Security Groups (NSGs) on backend subnets: allow only traffic from the LB frontend IP (or VNet) and health probe source IP (168.63.129.16). Deny all other inbound. For public ALB, enable Azure DDoS Protection Standard on the VNet. This protects against volumetric attacks. ALB does not have built-in WAF; use Azure Application Gateway or Front Door for Layer 7 filtering. For internal ALB, use Private Endpoints to expose services privately. ALB can be placed behind a firewall (e.g., Azure Firewall) for traffic inspection. However, this adds latency. For compliance, enable diagnostic logs and send to Log Analytics. Use Azure Policy to enforce that ALBs are Standard SKU and have NSGs attached. Also, restrict outbound traffic from backend VMs using NSGs or Azure Firewall. Never allow unrestricted outbound. Finally, use Managed Identities for VMs to access Azure resources securely, avoiding connection strings in code.
Basic DDoS protection is free but limited. Enable DDoS Protection Standard on the VNet for advanced mitigation. It costs ~$3k/month but is essential for public-facing production workloads.
📊 Production Insight
We mitigated a DDoS attack by enabling DDoS Standard and rate-limiting at the application layer. The LB itself was fine, but backend VMs were overwhelmed. We added auto-scaling and a WAF in front.
🎯 Key Takeaway
Secure ALB with NSGs, DDoS Protection, and private endpoints; use Azure Firewall for traffic inspection if needed.
Cost Optimization and Performance Tuning
Azure Load Balancer pricing is based on rules and data processed. Standard SKU has a fixed hourly cost plus per-GB data processing. To optimize: consolidate rules—each rule adds cost. Use a single rule for multiple ports if possible (e.g., TCP 80-443). For outbound, use NAT Gateway instead of LB outbound rules for better port scalability and lower cost at high volumes. NAT Gateway costs ~$0.045/hour plus data processing, but provides 64,512 ports per IP. For performance, ALB can handle millions of flows. However, ensure backend VMs are sized appropriately. Use accelerated networking on VMs for higher throughput. Also, enable TCP segmentation offload (TSO) on the OS. Monitor latency metrics: ALB adds <1ms latency. If you see higher, check backend response times. For global performance, use cross-region LB or Front Door. In production, right-size your backend pool: too few instances cause overload; too many waste cost. Use autoscaling based on CPU or request count. Finally, consider reserved instances for predictable workloads to save up to 72% on VM costs.
For high outbound traffic, use NAT Gateway instead of LB outbound rules. NAT Gateway provides more SNAT ports and better performance at similar cost.
📊 Production Insight
We reduced costs by 30% by moving from LB outbound rules to NAT Gateway for a microservices cluster. The SNAT port exhaustion issues also disappeared.
🎯 Key Takeaway
Optimize cost by consolidating rules, using NAT Gateway for outbound, and autoscaling backends.
Migration from Basic to Standard SKU
If you're still on Basic SKU, migrate to Standard. Basic SKU is being deprecated and lacks features. Migration steps: 1) Create a new Standard SKU ALB in the same region. 2) Configure frontend IP, backend pool, probes, and rules matching the Basic LB. 3) Update DNS to point to the new LB's public IP. 4) Gradually shift traffic by updating application configurations. 5) Delete the old Basic LB. Important: Standard SKU requires explicit outbound rules; Basic had default outbound. Ensure you configure outbound rules before cutting over. Also, Standard SKU has different NSG requirements—it doesn't allow inbound traffic from the internet unless explicitly allowed. Test in a staging environment first. Use Azure Traffic Manager to perform a blue-green migration. During migration, monitor metrics to ensure no impact. Rollback plan: keep the Basic LB running until traffic is fully migrated. Note that Basic LB does not support availability zones, so if you need HA, you must redeploy backends across zones.
migrate.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# CreateStandardLB
az network lb create --resource-group prod-rg --name prod-lb-std --sku Standard --public-ip-address prod-pip-std
# Copy configuration from BasicLB
az network lb rule list --resource-group prod-rg --lb-name prod-lb-basic --query "[].{name:name, protocol:protocol, frontendPort:frontendPort, backendPort:backendPort}" -o json > rules.json
# Apply rules to StandardLBfor rule in $(cat rules.json | jq -c '.[]'); do
name=$(echo $rule | jq -r '.name')
protocol=$(echo $rule | jq -r '.protocol')
frontendPort=$(echo $rule | jq -r '.frontendPort')
backendPort=$(echo $rule | jq -r '.backendPort')
az network lb rule create --resource-group prod-rg --lb-name prod-lb-std --name $name --protocol $protocol --frontend-port $frontendPort --backend-port $backendPort --frontend-ip-name frontend --backend-pool-name backend-pool --probe-name http-probe
done
# UpdateDNS
az network dns record-set a update --resource-group dns-rg --zone-name example.com --name www --set targetResource.id=/subscriptions/.../publicIPAddresses/prod-pip-std
Output
Migration completed. DNS updated to new Standard LB.
⚠ Outbound Connectivity
After migration, VMs may lose outbound internet access because Standard LB doesn't have default outbound. Create an outbound rule or use NAT Gateway before cutting over.
📊 Production Insight
We migrated a production Basic LB to Standard and forgot to configure outbound rules. VMs couldn't reach external APIs for 10 minutes. Now we have a pre-migration checklist that includes outbound rules.
🎯 Key Takeaway
Migrate from Basic to Standard SKU for features, SLA, and availability zones; plan outbound connectivity.
Gateway Load Balancer: Transparent NVA Insertion
Gateway Load Balancer (GWLB) is a specialized SKU of Azure Load Balancer designed for transparent insertion of Network Virtual Appliances (NVAs) into the traffic path. Unlike Standard LB which distributes traffic to backends, GWLB sits between the client and the NVA, forwarding traffic using Geneve tunneling. This enables scenarios like firewall inspection, IDS/IPS, and packet inspection — without complex UDR configurations. GWLB maintains flow stickiness and flow symmetry automatically, eliminating the asymmetric routing issues common with traditional NVA deployments. In production, chain a Gateway Load Balancer to a Standard Public Load Balancer: the public LB receives client traffic and forwards it to the GWLB, which distributes it across NVAs. This provides HA at both the application and NVA layers. Key benefits: no UDRs needed, automatic flow symmetry, and easy NVA scaling (add/remove NVAs without changing routes). GWLB supports up to 100 Gbps throughput. Use it for north-south traffic scenarios with partner NVAs like Palo Alto, Check Point, or Fortinet. Separate trusted and untrusted traffic using different tunnel interfaces (external vs internal). Ensure NVAs have MTU at least 1550 (up to 4000 for jumbo frames) to accommodate VXLAN headers.
Before GWLB, you needed two LBs (inbound and outbound) plus complex UDRs for NVA insertion. GWLB handles flow symmetry automatically — no more asymmetric routing headaches.
📊 Production Insight
We replaced a complex dual-LB NVA setup with GWLB. Configuration went from 50 lines of UDRs and two LBs to a single GWLB config. NVA scaling became as simple as adding a new backend address.
NAT Gateway vs Load Balancer Outbound: Choosing the Right Strategy
Azure provides multiple methods for outbound connectivity, and selecting the right one is critical for production workloads. NAT Gateway is the recommended option: it provides up to 64,512 SNAT ports per IP (vs. 64,000 shared across all backends for LB outbound rules), scales to 100 Gbps, and is zone-redundant. NAT Gateway also takes precedence over all other outbound methods when associated with a subnet. Load Balancer outbound rules are still viable for scenarios where you need both inbound and outbound through the same public IP, but they require manual port allocation to avoid SNAT exhaustion — the default allocation is conservative and insufficient for production. Instance-level public IPs (direct IP on the NIC) are simple but don't scale and add management overhead. Default outbound access retired September 2025 and should never be used. In production, use NAT Gateway for most outbound scenarios, especially when: high SNAT port count needed (>64K), multiple VMs share the same subnet, or you need flow logs (StandardV2). Use LB outbound rules when you must share the same public IP for inbound and outbound. Combine both: NAT Gateway for outbound, LB for inbound. The hierarchy is: NAT Gateway > instance-level PIP > LB outbound rules > default outbound access. Monitor SNAT port utilization with Azure Monitor and set alerts at 70% utilization.
choose-outbound.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# Method1: NATGateway (recommended)
az network nat gateway create --resource-group prod-rg --name prod-natgw --sku Standard
az network vnet subnet update --resource-group prod-rg --vnet-name prod-vnet --name web-subnet --nat-gateway prod-natgw
# Method2: LB outbound rules (when sharing IP with inbound)
az network lb outbound-rule create \
--resource-group prod-rg \
--lb-name prod-lb \
--name outbound-rule \
--frontend-ip-configs frontend \
--backend-pool-name backend-pool \
--protocol All \
--outbound-ports 10000 # Manual allocation
# MonitorSNAT usage
az monitor metrics list \
--resource /subscriptions/.../natGateways/prod-natgw \
--metric SNATConnectionCount \
--interval PT1H
az monitor metrics list \
--resource /subscriptions/.../loadBalancers/prod-lb \
--metric SnatConnectionCount \
--interval PT1H
⚠ Default Outbound Access Retired
Since September 30, 2025, default outbound access no longer exists for new deployments. All VNets now use private subnets by default. You must explicitly configure NAT Gateway, LB outbound rules, or instance-level PIPs.
📊 Production Insight
A customer was using LB outbound rules with default port allocation and hitting SNAT exhaustion daily. Switching to NAT Gateway increased available SNAT ports from 1,024 per VM to 64,512 per IP. Exhaustion problems vanished.
🎯 Key Takeaway
NAT Gateway is the preferred outbound method for most scenarios; use LB outbound rules only when sharing IP with inbound traffic.
thecodeforge.io
Azure Load Balancer
Inbound NAT Rules: Direct VM Access Management
Inbound NAT rules in Azure Load Balancer allow you to map a specific frontend port to a specific backend VM and port. This is commonly used for RDP/SSH access to individual VMs without exposing them directly to the internet — traffic goes through the LB's public IP and is forwarded to the VM. Inbound NAT rules are defined per rule (one frontend port maps to one backend:port). You can also use inbound NAT pools for VMSS — each VM in the scale set gets a unique port mapping automatically. In production, inbound NAT rules are ideal for management access: instead of assigning public IPs to each VM (insecure), you create NAT rules on the LB. For example, map port 50001 to VM-1:22, port 50002 to VM-2:22, etc. Use Azure Bastion as a more secure alternative for management access — it provides RDP/SSH through the browser without any public IP exposure. Inbound NAT rules have limitations: each rule consumes 8 SNAT ports from the frontend IP's available pool, reducing capacity for outbound connections. For large fleets, use a bastion host or Azure Bastion instead of hundreds of NAT rules. Combine inbound NAT rules with NSGs to restrict source IPs (e.g., only allow your corporate VPN IP range).
inbound-nat-rules.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
# Create inbound NAT rule forSSH access to a specific VM
az network lb inbound-nat-rule create \
--resource-group prod-rg \
--lb-name prod-lb \
--name ssh-web-vm \
--protocol Tcp \
--frontend-port 50001 \
--backend-port 22 \
--frontend-ip-name frontend
# Associate with VM's NIC
az network nic ip-config inbound-nat-rule add \
--resource-group prod-rg \
--nic-name web-vm-nic \
--ip-config-name ipconfig1 \
--inbound-nat-rule ssh-web-vm
# Create inbound NAT pool forVMSS
az network lb inbound-nat-pool create \
--resource-group prod-rg \
--lb-name prod-lb \
--name ssh-pool \
--protocol Tcp \
--frontend-port-range-start 50000 \
--frontend-port-range-end 50100 \
--backend-port 22
# Connect to VM via NAT rule
ssh -p 50001 azureuser@20.185.0.1
⚠ NAT Rules Consume SNAT Ports
Each inbound NAT rule consumes 8 SNAT ports from the frontend IP's 64,000 available ports. If you have many rules, you reduce outbound capacity. For fleets >50 VMs, use Azure Bastion instead.
📊 Production Insight
We used inbound NAT rules for SSH access to 200 VMs. Each VM got a unique port. When we hit the SNAT port limit, we consolidated to a bastion host and used Azure Bastion for management, freeing up the SNAT ports for application traffic.
🎯 Key Takeaway
Inbound NAT rules provide secure per-VM access through the LB without exposing public IPs directly, but each rule reduces SNAT port availability.
⚙ Quick Reference
15 commands from this guide
File
Command / Code
Purpose
create-alb.sh
az network lb create \
Azure Load Balancer
configure-backend-pool.sh
az network lb address-pool create \
Backend Pools
health-endpoint.py
from flask import Flask, jsonify
Health Probes
create-lb-rule.sh
az network lb rule create \
Load Balancing Rules and Session Persistence
create-outbound-rule.sh
az network lb outbound-rule create \
Outbound Rules
zone-redundant-alb.sh
az network public-ip create \
High Availability
setup-alerts.sh
az monitor metrics alert create \
Monitoring and Diagnostics
troubleshoot.sh
az network lb probe show \
Troubleshooting Common Failures
main.tf
resource "azurerm_lb" "prod" {
Production Deployment with Infrastructure as Code
nsg-rules.sh
az network nsg rule create \
Security
cost-estimate.sh
az consumption prices list \
Cost Optimization and Performance Tuning
migrate.sh
az network lb create --resource-group prod-rg --name prod-lb-std --sku Standard ...
Azure Load Balancer distributes TCP/UDP traffic without inspecting payloads, ideal for high-throughput, low-latency workloads.
2
Health Probes Are Critical
Use HTTP probes with dependency checks and short intervals (5s) for fast failure detection; ensure NSG allows probe source IP 168.63.129.16.
3
SNAT Port Management
Monitor outbound connections; use multiple frontend IPs or NAT Gateway to avoid port exhaustion; set alerts at 70% utilization.
4
IaC and Security
Deploy with Terraform/Bicep for repeatability; secure with NSGs, DDoS Protection, and private endpoints; always use Standard SKU.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01JUNIOR
Explain Azure Load Balancer and its use cases.
Q02JUNIOR
How does Azure Load Balancer handle high availability?
Q03JUNIOR
What are the security best practices for load balancer?
Q04JUNIOR
How do you optimize costs for load balancer?
Q05JUNIOR
Compare Azure load balancer with self-hosted alternatives.
Q01 of 05JUNIOR
Explain Azure Load Balancer and its use cases.
ANSWER
Microsoft Azure — Azure Load Balancer is an Azure service for managing load balancer in the cloud. Use it when you need reliable, scalable load balancer without managing underlying infrastructure.
Q02 of 05JUNIOR
How does Azure Load Balancer handle high availability?
ANSWER
Azure provides region pairs, availability zones, and SLA-backed guarantees. Configure redundancy at the application and data tier for 99.95%+ availability.
Q03 of 05JUNIOR
What are the security best practices for load balancer?
ANSWER
Use managed identities, RBAC with least privilege, encrypt data at rest and in transit, enable diagnostic logging, and regularly audit access with Azure Monitor.
Q04 of 05JUNIOR
How do you optimize costs for load balancer?
ANSWER
Right-size resources based on metrics, use reserved instances or savings plans, implement auto-scaling, and review Azure Advisor cost recommendations.
Q05 of 05JUNIOR
Compare Azure load balancer with self-hosted alternatives.
ANSWER
Azure managed services reduce operational overhead (patching, backups, scaling). Trade-offs include less control and potential cost at extreme scale. Best for teams wanting to focus on applications over infrastructure.
01
Explain Azure Load Balancer and its use cases.
JUNIOR
02
How does Azure Load Balancer handle high availability?
JUNIOR
03
What are the security best practices for load balancer?
JUNIOR
04
How do you optimize costs for load balancer?
JUNIOR
05
Compare Azure load balancer with self-hosted alternatives.
JUNIOR
FAQ · 6 QUESTIONS
Frequently Asked Questions
01
What is the difference between Azure Load Balancer and Application Gateway?
Azure Load Balancer operates at Layer 4 (TCP/UDP) and is stateless, ideal for high-throughput traffic. Application Gateway operates at Layer 7 (HTTP/HTTPS) with features like SSL termination, URL-based routing, and WAF. Use ALB for non-HTTP workloads or when you need raw packet forwarding; use App Gateway for web applications requiring advanced routing and security.
Was this helpful?
02
How do I troubleshoot unhealthy backend instances in Azure Load Balancer?
First, verify the health probe configuration: ensure the probe port matches the backend port (or a separate health port) and the endpoint returns 200. Check NSG rules to allow traffic from Azure's health probe source IP (168.63.129.16) on the probe port. Use az network lb probe show to see probe status. Also, check the backend instance's application logs to ensure it's running and responsive.
Was this helpful?
03
Can Azure Load Balancer handle WebSocket connections?
Yes, Azure Load Balancer can handle WebSocket connections because it operates at Layer 4 and forwards TCP traffic. However, it does not understand WebSocket protocol; it simply forwards packets. For WebSocket-aware load balancing (e.g., sticky sessions based on WebSocket key), use Application Gateway.
Was this helpful?
04
What is SNAT port exhaustion and how do I prevent it?
SNAT port exhaustion occurs when a backend VM runs out of ephemeral ports for outbound connections. Each outbound connection consumes a port. To prevent it, monitor SNAT connection count metrics, use multiple frontend IPs to increase port pool, reduce idle timeout, or use Azure NAT Gateway which provides 64,512 ports per IP. Also, implement connection pooling in your application.
Was this helpful?
05
How do I achieve cross-region load balancing with Azure Load Balancer?
Use Azure Cross-Region Load Balancer (preview) which distributes traffic across regional Standard Load Balancers. It uses a global frontend IP and health probes on regional frontends. For production, consider Azure Traffic Manager or Azure Front Door for more advanced global routing policies like performance-based or geographic routing.
Was this helpful?
06
What are the key differences between Basic and Standard SKU of Azure Load Balancer?
Standard SKU offers availability zones, SLA (99.99%), outbound rules, connection draining, TCP reset, and integration with NAT Gateway. Basic SKU lacks these features, has no SLA, and is limited to a single availability zone. Always use Standard SKU for production workloads.