πŸ”„ AWS Load Balancing and Traffic Management

πŸ“‹ Table of Contents

1. πŸ—οΈ Overview

AWS Load Balancing distributes incoming application traffic across multiple targets to ensure high availability, fault tolerance, and optimal performance. This guide covers all AWS load balancer types with practical implementations.

graph TB subgraph "Internet" Client1[πŸ‘€ Client 1] Client2[πŸ‘€ Client 2] Client3[πŸ‘€ Client 3] end subgraph "AWS Region" subgraph "Availability Zone A" ALB_A[πŸ”„ ALB Node A] Target_A1[πŸ–₯️ EC2-A1] Target_A2[πŸ–₯️ EC2-A2] end subgraph "Availability Zone B" ALB_B[πŸ”„ ALB Node B] Target_B1[πŸ–₯️ EC2-B1] Target_B2[πŸ–₯️ EC2-B2] end subgraph "Availability Zone C" ALB_C[πŸ”„ ALB Node C] Target_C1[πŸ–₯️ EC2-C1] Target_C2[πŸ–₯️ EC2-C2] end end Client1 --> ALB_A Client2 --> ALB_B Client3 --> ALB_C ALB_A --> Target_A1 ALB_A --> Target_A2 ALB_A -.-> Target_B1 ALB_A -.-> Target_C1 ALB_B --> Target_B1 ALB_B --> Target_B2 ALB_B -.-> Target_A1 ALB_B -.-> Target_C1 ALB_C --> Target_C1 ALB_C --> Target_C2 ALB_C -.-> Target_A1 ALB_C -.-> Target_B1
AWS Load Balancer Architecture: This diagram illustrates how AWS load balancers operate across multiple Availability Zones. Each AZ has its own load balancer nodes that can route traffic to targets in any AZ (cross-zone load balancing). Solid lines show primary routing, dotted lines show cross-zone capabilities for high availability.

2. 🌐 Load Balancing at OSI Layers

Layer 3 (Network Layer) - Network Load Balancer (NLB)

graph LR subgraph "Layer 3 Processing" Client_IP[πŸ“± Client
IP: 203.0.113.100] NLB_IP[⚑ NLB
IP: 203.0.113.200
Port: 80] subgraph "5-Tuple Hashing" Hash[πŸ”’ Hash Algorithm
Source IP + Port
Dest IP + Port
Protocol] end Target1[πŸ–₯️ Target 1
10.0.1.10:80] Target2[πŸ–₯️ Target 2
10.0.1.20:80] Target3[πŸ–₯️ Target 3
10.0.1.30:80] end Client_IP --> NLB_IP NLB_IP --> Hash Hash --> Target1 Hash --> Target2 Hash --> Target3 subgraph "NLB Features" F1[⚑ Ultra-low latency
< 100ΞΌs] F2[🎯 Static IP per AZ] F3[πŸ” Source IP preservation] F4[πŸ“Š Millions RPS] F5[πŸ”„ Connection tracking] end
Layer 3 Network Load Balancer: NLB operates at the network layer, making routing decisions based on IP addresses and ports using a 5-tuple hash algorithm. It provides ultra-low latency (sub-100 microseconds), preserves source IP addresses, and can handle millions of requests per second with static IP addresses for each availability zone.

1Create Network Load Balancer

aws elbv2 create-load-balancer \
    --name my-network-load-balancer \
    --scheme internet-facing \
    --type network \
    --subnets subnet-12345678 subnet-87654321 \
    --tags Key=Environment,Value=Production Key=Application,Value=WebApp
NLB Creation Parameters:

Setup Order: This is the first command to create the foundational NLB infrastructure. The load balancer will get automatic static IP addresses in each specified subnet.

Layer 4 (Transport Layer) - Connection Flow

sequenceDiagram participant C as πŸ‘€ Client
203.0.113.50 participant NLB as ⚑ Network LB
203.0.113.200 participant T1 as πŸ–₯️ Target 1
10.0.1.10 participant T2 as πŸ–₯️ Target 2
10.0.1.20 Note over C,T2: TCP Connection Establishment C->>NLB: TCP SYN (Port 80)
Seq: 1000 Note over NLB: Flow Hash Calculation
(203.0.113.50:45678 + 203.0.113.200:80 + TCP) NLB->>T1: Forward TCP SYN
Source IP preserved T1->>NLB: TCP SYN-ACK
Seq: 2000, Ack: 1001 NLB->>C: TCP SYN-ACK C->>NLB: TCP ACK + HTTP Request NLB->>T1: Forward to same target
(connection tracking) Note over C,T2: New Connection (Different Flow) C->>NLB: New TCP SYN (Port 443) Note over NLB: New 5-tuple hash
Routes to different target NLB->>T2: Forward to Target 2
Layer 4 Connection Tracking: NLB maintains connection state using flow hash tables. Each unique 5-tuple (source IP, source port, destination IP, destination port, protocol) creates a flow entry that ensures all packets for that connection go to the same target. This provides session persistence while preserving the original client IP address.

2Create Target Group for NLB

aws elbv2 create-target-group \
    --name my-tcp-targets \
    --protocol TCP \
    --port 80 \
    --vpc-id vpc-12345678 \
    --target-type instance \
    --health-check-protocol TCP \
    --health-check-port 80 \
    --health-check-interval-seconds 30 \
    --healthy-threshold-count 2 \
    --unhealthy-threshold-count 2
TCP Target Group Parameters:

Purpose: This creates the target group that will contain your backend instances. The TCP health check verifies basic connectivity without HTTP overhead.

Layer 7 (Application Layer) - Application Load Balancer

graph TD subgraph "Client Requests" Req1[🌐 GET /api/users
Host: api.example.com] Req2[🌐 GET /admin/dashboard
Host: admin.example.com
Cookie: session=premium] Req3[🌐 POST /checkout
Header: X-User-Type: premium] end ALB[πŸ”„ Application Load Balancer
Layer 7 Processing] subgraph "Routing Rules" Rule1{πŸ” Path Pattern
/api/*} Rule2{🏠 Host Header
admin.example.com} Rule3{πŸ‘‘ HTTP Header
X-User-Type: premium} Rule4{πŸͺ Cookie
session=premium} end subgraph "Target Groups" TG_API[🎯 API Microservices
Health: HTTP GET /health] TG_Admin[🎯 Admin Services
Health: HTTP GET /admin/health] TG_Premium[🎯 Premium Services
Health: HTTP GET /status] TG_Standard[🎯 Standard Services
Health: HTTP GET /ping] end Req1 --> ALB Req2 --> ALB Req3 --> ALB ALB --> Rule1 ALB --> Rule2 ALB --> Rule3 ALB --> Rule4 Rule1 -->|Match /api/*| TG_API Rule2 -->|Match Host| TG_Admin Rule3 -->|Premium Header| TG_Premium Rule4 -->|Premium Cookie| TG_Premium ALB -->|Default| TG_Standard TG_API --> API1[πŸ–₯️ API-1
10.0.1.10] TG_API --> API2[πŸ–₯️ API-2
10.0.1.20] TG_Admin --> Admin1[πŸ–₯️ Admin-1
10.0.2.10] TG_Premium --> Prem1[πŸ–₯️ Premium-1
10.0.3.10] TG_Standard --> Std1[πŸ–₯️ Standard-1
10.0.4.10]
Layer 7 Application Load Balancer: ALB inspects HTTP/HTTPS content to make intelligent routing decisions. It can route based on URL paths, host headers, HTTP headers, query parameters, and cookies. This enables sophisticated architectures like microservices, canary deployments, and user-based routing with detailed HTTP health checks.

1Create Application Load Balancer

aws elbv2 create-load-balancer \
    --name my-application-load-balancer \
    --scheme internet-facing \
    --type application \
    --subnets subnet-12345678 subnet-87654321 \
    --security-groups sg-12345678 \
    --ip-address-type ipv4 \
    --tags Key=Environment,Value=Production
ALB Creation Parameters:

Setup Order: This is the first command for ALB setup. Unlike NLB, ALB requires security groups and operates in public subnets for internet-facing deployments.

2Create Target Group with HTTP Health Checks

aws elbv2 create-target-group \
    --name my-web-app-targets \
    --protocol HTTP \
    --port 80 \
    --vpc-id vpc-12345678 \
    --target-type instance \
    --health-check-protocol HTTP \
    --health-check-path /health \
    --health-check-interval-seconds 30 \
    --health-check-timeout-seconds 10 \
    --healthy-threshold-count 2 \
    --unhealthy-threshold-count 3 \
    --matcher HttpCode=200
HTTP Target Group Parameters:

Purpose: Creates HTTP-aware target group with application-level health checks. The health check endpoint should return application status, not just connectivity.

3. πŸ”§ Load Balancer Types and Selection Criteria

graph TD Decision{πŸ€” Choose Load Balancer Type} Decision -->|Ultra-low latency
Static IP needed
TCP/UDP traffic| NLB[⚑ Network Load Balancer
Layer 3/4] Decision -->|HTTP/HTTPS
Content-based routing
SSL termination| ALB[πŸ”„ Application Load Balancer
Layer 7] Decision -->|Legacy applications
Basic load balancing
Cost-sensitive| CLB[πŸ”„ Classic Load Balancer
Layer 4/7] Decision -->|Global traffic
DNS-based routing
Multi-region| GLB[🌍 Global Load Balancer
Route 53] subgraph "NLB Use Cases" NLB_UC1[πŸ“‘ Real-time gaming] NLB_UC2[πŸ’± Financial trading] NLB_UC3[🎬 Media streaming] NLB_UC4[πŸ”Œ IoT/TCP connections] end subgraph "ALB Use Cases" ALB_UC1[🌐 Web applications] ALB_UC2[πŸ”— Microservices] ALB_UC3[πŸ“± Mobile APIs] ALB_UC4[πŸ§ͺ A/B testing] end subgraph "Selection Criteria" SC1[πŸ“Š Performance Requirements] SC2[πŸ’° Cost Considerations] SC3[πŸ”§ Feature Requirements] SC4[πŸ—οΈ Architecture Complexity] end NLB --> NLB_UC1 NLB --> NLB_UC2 NLB --> NLB_UC3 NLB --> NLB_UC4 ALB --> ALB_UC1 ALB --> ALB_UC2 ALB --> ALB_UC3 ALB --> ALB_UC4
Load Balancer Selection Guide: This decision tree helps choose the appropriate load balancer type based on specific requirements. NLB for maximum performance and static IPs, ALB for HTTP/HTTPS applications with advanced routing, CLB for legacy compatibility, and Global Load Balancer for multi-region deployments.
Feature Network LB (NLB) Application LB (ALB) Classic LB (CLB)
OSI Layer Layer 3/4 Layer 7 Layer 4/7
Protocols TCP, UDP, TLS HTTP, HTTPS TCP, HTTP, HTTPS
Latency Ultra-low (~100ΞΌs) Low (~400ΞΌs) Medium
Static IP βœ… Yes ❌ No ❌ No
SSL Termination βœ… Yes βœ… Yes βœ… Yes
Content Routing ❌ No βœ… Yes ❌ No
WebSockets βœ… Yes βœ… Yes βœ… Yes
Source IP Preservation βœ… Always ⚠️ Via headers ⚠️ Via headers

4. βš™οΈ Configuration Options

Cross-Zone Load Balancing

graph TB subgraph "Without Cross-Zone LB" Client1[πŸ‘€ Client 1] --> AZ1_LB[πŸ”„ AZ-1 LB Node
50% traffic] Client2[πŸ‘€ Client 2] --> AZ2_LB[πŸ”„ AZ-2 LB Node
50% traffic] AZ1_LB --> AZ1_T1[πŸ–₯️ Target 1] AZ1_LB --> AZ1_T2[πŸ–₯️ Target 2] AZ1_LB --> AZ1_T3[πŸ–₯️ Target 3] AZ2_LB --> AZ2_T1[πŸ–₯️ Target 4] subgraph "AZ-1: 3 targets" AZ1_T1 AZ1_T2 AZ1_T3 end subgraph "AZ-2: 1 target" AZ2_T1 end end subgraph "With Cross-Zone LB" Client3[πŸ‘€ Client 3] --> AZ1_LB_CZ[πŸ”„ AZ-1 LB Node
50% traffic] Client4[πŸ‘€ Client 4] --> AZ2_LB_CZ[πŸ”„ AZ-2 LB Node
50% traffic] AZ1_LB_CZ --> CZ_T1[πŸ–₯️ Target 1
25%] AZ1_LB_CZ --> CZ_T2[πŸ–₯️ Target 2
25%] AZ1_LB_CZ --> CZ_T3[πŸ–₯️ Target 3
25%] AZ1_LB_CZ --> CZ_T4[πŸ–₯️ Target 4
25%] AZ2_LB_CZ --> CZ_T1 AZ2_LB_CZ --> CZ_T2 AZ2_LB_CZ --> CZ_T3 AZ2_LB_CZ --> CZ_T4 subgraph "All targets get equal traffic" CZ_T1 CZ_T2 CZ_T3 CZ_T4 end end
Cross-Zone Load Balancing Impact: Without cross-zone LB, traffic is distributed equally among AZ nodes, but unevenly among targets (66.7% to AZ-1 targets, 33.3% to AZ-2). With cross-zone LB enabled, all targets receive equal traffic (25% each) regardless of AZ distribution, but this incurs cross-AZ data transfer charges.

3Enable Cross-Zone Load Balancing

aws elbv2 modify-load-balancer-attributes \
    --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/my-nlb/1234567890abcdef \
    --attributes Key=load_balancing.cross_zone.enabled,Value=true
Cross-Zone Configuration:

When to Use: Enable when target distribution is uneven across AZs and consistent load distribution is more important than cost optimization.

Session Affinity (Stickiness)

sequenceDiagram participant C as πŸ‘€ Client participant ALB as πŸ”„ ALB participant T1 as πŸ–₯️ Target 1 participant T2 as πŸ–₯️ Target 2 Note over C,T2: First Request - No Session C->>ALB: GET /login
No session cookie ALB->>T1: Route to Target 1
(Round-robin selection) T1->>ALB: HTTP 200 + Session Data
Set user session ALB->>C: HTTP 200 + Sticky Cookie
AWSALB=target1hash Note over C,T2: Subsequent Requests - Session Affinity C->>ALB: GET /dashboard
Cookie: AWSALB=target1hash Note over ALB: Cookie indicates Target 1
Route to same target ALB->>T1: Route to Target 1
(Session affinity) T1->>ALB: HTTP 200 + User Data
Session maintained ALB->>C: HTTP 200
Session preserved Note over C,T2: Target Failure Scenario C->>ALB: GET /profile
Cookie: AWSALB=target1hash ALB->>T1: Health check failed Note over ALB: Target 1 unhealthy
Route to healthy target ALB->>T2: Route to Target 2
New session required T2->>ALB: HTTP 200
Session reset ALB->>C: HTTP 200 + New Cookie
AWSALB=target2hash
Session Affinity Behavior: When enabled, ALB uses application-controlled cookies or duration-based stickiness to route requests from the same client to the same target. This preserves session state but can create uneven load distribution. If the target becomes unhealthy, the session is redirected to a healthy target.

4Configure Session Stickiness

aws elbv2 modify-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890abcdef \
    --attributes Key=stickiness.enabled,Value=true \
                 Key=stickiness.type,Value=lb_cookie \
                 Key=stickiness.lb_cookie.duration_seconds,Value=86400
Stickiness Parameters:

Trade-offs: Improves user experience for stateful applications but can cause uneven load distribution and reduced fault tolerance.

Proxy Protocol Configuration

graph LR subgraph "Without Proxy Protocol" Client1[πŸ‘€ Client
203.0.113.50] --> NLB1[⚑ NLB
203.0.113.200] NLB1 --> Target1[πŸ–₯️ Backend
Sees: 203.0.113.200
❌ Lost original IP] end subgraph "With Proxy Protocol v1" Client2[πŸ‘€ Client
203.0.113.50] --> NLB2[⚑ NLB
203.0.113.200] NLB2 --> Target2[πŸ–₯️ Backend
Receives: PROXY TCP4
203.0.113.50 203.0.113.200
45678 80
βœ… Original IP preserved] end subgraph "With Proxy Protocol v2" Client3[πŸ‘€ Client
203.0.113.50] --> NLB3[⚑ NLB
203.0.113.200] NLB3 --> Target3[πŸ–₯️ Backend
Receives: Binary header
+ TLV extensions
βœ… Rich metadata] end
Proxy Protocol Functionality: Proxy Protocol preserves original client connection information when load balancers proxy connections. Version 1 uses human-readable text format, while version 2 uses efficient binary format with additional metadata. Backend applications must be configured to parse the proxy protocol header.

5Enable Proxy Protocol

aws elbv2 modify-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890abcdef \
    --attributes Key=proxy_protocol_v2.enabled,Value=true
Proxy Protocol Options:

Important: Ensure your backend applications support Proxy Protocol before enabling, or connections will fail.

Routing Algorithms

graph TD subgraph "Round Robin (Default ALB)" RR_Req1[πŸ“¨ Request 1] --> RR_T1[πŸ–₯️ Target 1] RR_Req2[πŸ“¨ Request 2] --> RR_T2[πŸ–₯️ Target 2] RR_Req3[πŸ“¨ Request 3] --> RR_T3[πŸ–₯️ Target 3] RR_Req4[πŸ“¨ Request 4] --> RR_T1 end subgraph "Least Outstanding Requests (ALB)" LOR_Req1[πŸ“¨ Request 1
Target 1: 5 active] --> LOR_T2[πŸ–₯️ Target 2
3 active βœ…] LOR_Req2[πŸ“¨ Request 2
Target 2: 4 active] --> LOR_T3[πŸ–₯️ Target 3
2 active βœ…] LOR_Req3[πŸ“¨ Request 3
All equal: 3 active] --> LOR_T1[πŸ–₯️ Target 1
Round-robin βœ…] end subgraph "Flow Hash (NLB Default)" FH_Flow1[πŸ“¨ Flow 1
203.0.113.50:45678] --> FH_Hash{πŸ”’ 5-tuple Hash} FH_Flow2[πŸ“¨ Flow 2
203.0.113.51:45679] --> FH_Hash FH_Hash --> FH_T1[πŸ–₯️ Target 1
Consistent routing] FH_Hash --> FH_T2[πŸ–₯️ Target 2
Consistent routing] end
Load Balancing Algorithms: ALB uses Round Robin by default (equal distribution) or Least Outstanding Requests (routes to target with fewest active connections). NLB uses Flow Hash based on 5-tuple (source IP, source port, destination IP, destination port, protocol) ensuring consistent routing for the same connection flow.

6Configure Routing Algorithm

aws elbv2 modify-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890abcdef \
    --attributes Key=load_balancing.algorithm.type,Value=least_outstanding_requests
Algorithm Options:

Best Practice: Use least outstanding requests for applications with variable response times or when targets have different processing capabilities.

5. 🎯 Target Group Configurations

Target Types Comparison

graph TB subgraph "Instance Target Type" TG_Instance[🎯 Target Group
Type: instance] TG_Instance --> EC2_1[πŸ–₯️ EC2 Instance
i-1234567890abcdef0
Auto-discovered IP] TG_Instance --> EC2_2[πŸ–₯️ EC2 Instance
i-0987654321fedcba0
Auto-discovered IP] TG_Instance --> EC2_3[πŸ–₯️ EC2 Instance
i-abcdef1234567890
Auto-discovered IP] end subgraph "IP Target Type" TG_IP[🎯 Target Group
Type: ip] TG_IP --> IP_1[🌐 IP Target
10.0.1.100:80
Manual IP specification] TG_IP --> IP_2[🌐 IP Target
192.168.1.50:8080
On-premises server] TG_IP --> Lambda[⚑ Lambda Function
Auto-assigned IP
Serverless target] end subgraph "ALB Target Type" TG_ALB[🎯 Target Group
Type: alb] TG_ALB --> ALB_Target[πŸ”„ Another ALB
Multi-tier architecture
ALB chaining] end subgraph "Protocol Support" Proto_HTTP[πŸ“„ HTTP/HTTPS
Layer 7 protocols] Proto_TCP[πŸ”Œ TCP/UDP/TLS
Layer 4 protocols] Proto_GENEVE[🌐 GENEVE
Gateway Load Balancer] end
Target Group Types: Instance targets automatically resolve EC2 instance IPs and integrate with Auto Scaling. IP targets allow manual IP specification for containers, on-premises servers, or Lambda functions. ALB targets enable chaining load balancers for complex architectures. Each type supports different protocols and use cases.

7Create IP-based Target Group

aws elbv2 create-target-group \
    --name my-ip-targets \
    --protocol HTTP \
    --port 8080 \
    --vpc-id vpc-12345678 \
    --target-type ip \
    --ip-address-type ipv4 \
    --health-check-protocol HTTP \
    --health-check-path /api/health \
    --health-check-port 8080 \
    --health-check-interval-seconds 30 \
    --matcher HttpCode=200,201
IP Target Group Parameters:

Use Cases: Containers with dynamic IPs, microservices on Fargate, Lambda functions, or hybrid architectures with on-premises servers.

8Register IP Targets

aws elbv2 register-targets \
    --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-ip-targets/1234567890abcdef \
    --targets Id=10.0.1.100,Port=8080 \
             Id=10.0.2.150,Port=8080 \
             Id=192.168.1.50,Port=9090,AvailabilityZone=us-east-1a
Target Registration Parameters:

Dynamic Registration: Use this command in deployment scripts or auto-scaling events to dynamically add/remove targets.

GENEVE Protocol for Gateway Load Balancer

graph TB subgraph "GENEVE Encapsulation Flow" Client[πŸ‘€ Client
Original Packet] --> IGW[🌐 Internet Gateway] IGW --> GWLB[βš–οΈ Gateway Load Balancer
GENEVE Encapsulation] GWLB --> Appliance1[πŸ›‘οΈ Security Appliance 1
Firewall/IDS] GWLB --> Appliance2[πŸ›‘οΈ Security Appliance 2
DPI/WAF] GWLB --> Appliance3[πŸ›‘οΈ Security Appliance 3
Threat Detection] Appliance1 --> GWLB_Return[βš–οΈ GWLB Return Path
GENEVE Decapsulation] Appliance2 --> GWLB_Return Appliance3 --> GWLB_Return GWLB_Return --> Target_App[🎯 Target Application
Original packet restored] end subgraph "GENEVE Packet Structure" Outer_IP[πŸ“¦ Outer IP Header
GWLB ↔ Appliance] UDP_Header[πŸ“¦ UDP Header
Port 6081] GENEVE_Header[πŸ“¦ GENEVE Header
VNI + Metadata] Original_Packet[πŸ“¦ Original Packet
Client ↔ Application] end Outer_IP --> UDP_Header UDP_Header --> GENEVE_Header GENEVE_Header --> Original_Packet
GENEVE Protocol in Gateway Load Balancer: GWLB uses GENEVE encapsulation to send original packets to security appliances while preserving source and destination information. The appliances process the encapsulated packets and return them for decapsulation, enabling transparent security inspection without changing the original traffic flow.

9Create Gateway Load Balancer with GENEVE

aws elbv2 create-load-balancer \
    --name my-gateway-load-balancer \
    --type gateway \
    --subnets subnet-12345678 subnet-87654321 \
    --scheme internal \
    --tags Key=Purpose,Value=SecurityInspection
Gateway Load Balancer Parameters:

Architecture: GWLB sits transparently in the traffic path, sending packets to security appliances for inspection without application awareness.

10Create GENEVE Target Group

aws elbv2 create-target-group \
    --name security-appliances \
    --protocol GENEVE \
    --port 6081 \
    --vpc-id vpc-12345678 \
    --target-type instance \
    --health-check-protocol HTTP \
    --health-check-path /health \
    --health-check-port 80
GENEVE Target Group Parameters:

Security Appliances: Targets must be security appliances capable of processing GENEVE-encapsulated traffic and returning processed packets.

6. ☸️ AWS Load Balancer Controller for Kubernetes

graph TB subgraph "Kubernetes Cluster" subgraph "AWS Load Balancer Controller" Controller[πŸŽ›οΈ AWS LB Controller\nWatches K8s resources\nManages AWS resources] end subgraph "Kubernetes Resources" Ingress[πŸ“‹ Ingress Resource\nalb.ingress.kubernetes.io] Service_NLB[πŸ“‹ Service - NLB\nservice.beta.kubernetes.io] Service_ALB[πŸ“‹ Service - ALB\nTargetGroupBinding] end subgraph "Application Pods" Pod1[πŸ™ Pod 1\n10.0.1.10] Pod2[πŸ™ Pod 2\n10.0.1.20] Pod3[πŸ™ Pod 3\n10.0.1.30] end end subgraph "AWS Resources" ALB_AWS[πŸ”„ Application LB\nCreated by Controller] NLB_AWS[⚑ Network LB\nCreated by Controller] TG_AWS[🎯 Target Groups\nAuto-managed] end Controller --> ALB_AWS Controller --> NLB_AWS Controller --> TG_AWS Ingress -.-> Controller Service_NLB -.-> Controller Service_ALB -.-> Controller ALB_AWS --> Pod1 ALB_AWS --> Pod2 NLB_AWS --> Pod3 TG_AWS --> Pod1 TG_AWS --> Pod2 TG_AWS --> Pod3
AWS Load Balancer Controller Architecture: The controller watches Kubernetes Ingress and Service resources, automatically creating and managing AWS load balancers and target groups. It registers Pod IPs as targets and handles lifecycle management, scaling, and health checks, providing seamless integration between Kubernetes and AWS load balancing services.

11Install AWS Load Balancer Controller

helm repo add eks https://aws.github.io/eks-charts
helm repo update

helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
    --set clusterName=my-eks-cluster \
    --set serviceAccount.create=false \
    --set serviceAccount.name=aws-load-balancer-controller \
    --set region=us-east-1 \
    --set vpcId=vpc-12345678 \
    -n kube-system
Controller Installation Parameters:

Prerequisites: Service account must exist with appropriate IAM permissions to manage ELB resources.

12Create Ingress for Application Load Balancer

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
    alb.ingress.kubernetes.io/load-balancer-name: my-k8s-alb
    alb.ingress.kubernetes.io/group.name: my-app-group
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS": 443}]'
    alb.ingress.kubernetes.io/ssl-redirect: '443'
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012
spec:
  rules:
  - host: api.example.com
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api-service
            port:
              number: 80
  - host: admin.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: admin-service
            port:
              number: 80
Ingress Annotations Explained:

Behavior: Controller creates ALB with listeners on ports 80/443, configures SSL termination, and sets up host-based routing rules.

13Create Service for Network Load Balancer

apiVersion: v1
kind: Service
metadata:
  name: my-nlb-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
    service.beta.kubernetes.io/aws-load-balancer-scheme: "internet-facing"
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "tcp"
    service.beta.kubernetes.io/aws-load-balancer-healthcheck-protocol: "HTTP"
    service.beta.kubernetes.io/aws-load-balancer-healthcheck-path: "/health"
    service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: "*"
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: my-app
NLB Service Annotations:

Use Case: High-performance TCP/UDP load balancing with ultra-low latency requirements.

7. πŸ”’ TLS Termination and Passthrough

graph TB subgraph "TLS Termination at Load Balancer" Client_Term[πŸ”’ Client\nHTTPS Request\nTLS Encrypted] --> ALB_Term[πŸ”„ ALB\nSSL Certificate\nTLS Termination] ALB_Term --> Backend_Term[πŸ–₯️ Backend\nHTTP Unencrypted\nPort 80] subgraph "Certificate Management" ACM[πŸ“œ AWS Certificate Manager\nAuto-renewal\nIntegration] IAM_Cert[πŸ“œ IAM Certificate Store\nManual management] end ALB_Term -.-> ACM ALB_Term -.-> IAM_Cert end subgraph "TLS Passthrough" Client_Pass[πŸ”’ Client\nHTTPS Request\nTLS Encrypted] --> NLB_Pass[⚑ NLB\nLayer 4 Proxy\nNo TLS inspection] NLB_Pass --> Backend_Pass[πŸ”’ Backend\nHTTPS Encrypted\nPort 443\nBackend handles TLS] subgraph "Backend Certificate" Backend_Cert[πŸ“œ Backend Certificate\nApplication managed\nEnd-to-end encryption] end Backend_Pass -.-> Backend_Cert end subgraph "Re-encryption" Client_Re[πŸ”’ Client\nHTTPS Request] --> ALB_Re[πŸ”„ ALB\nTLS Termination\n+ Re-encryption] ALB_Re --> Backend_Re[πŸ”’ Backend\nHTTPS New TLS\nBackend certificate] end
TLS Handling Options: TLS Termination decrypts traffic at the load balancer for inspection and processing. TLS Passthrough maintains end-to-end encryption by forwarding encrypted traffic directly to backends. Re-encryption provides both load balancer processing and backend encryption for maximum security with performance benefits.

14Create HTTPS Listener with SSL Termination

aws elbv2 create-listener \
    --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-alb/1234567890abcdef \
    --protocol HTTPS \
    --port 443 \
    --certificates CertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012 \
    --ssl-policy ELBSecurityPolicy-TLS-1-2-2017-01 \
    --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890abcdef
HTTPS Listener Parameters: