๐ Scenario 03: The Great Auto-scaling Challenge!
"Python Auto-scaling Hero Defeats Resource Exhaustion Chaos!"
๐ SCENARIO OVERVIEW
The Final Challenge
Chaos Agent has launched their most devastating attack yet! They're attempting to overwhelm your applications with massive traffic spikes and resource exhaustion attacks. Manual scaling can't keep up with the dynamic load patterns, and your infrastructure is at risk of complete failure under the relentless assault.
The Auto-scaling Hero Solution
Deploy an intelligent auto-scaling system using Python automation that dynamically adjusts resources based on real-time demand. Watch as your applications seamlessly scale up during attacks and efficiently scale down when the threat passes. No more manual intervention, no more resource waste!
What You'll Build
- ๐ Horizontal Pod Autoscaler (HPA) with intelligent scaling policies
- โก Interactive Load Testing Platform with real-time visualization
- ๐ฎ Chaos Agent Attack Simulator for ultimate stress testing
- ๐ Real-time Scaling Monitor with comprehensive metrics
- ๐ก๏ธ Production-Grade Resource Management with smart limits
โฑ๏ธ TIME ALLOCATION
| Activity | Duration | Type |
|---|---|---|
| Live Demo (Instructor) | 10 minutes | ๐ Watch |
| Your Auto-scaling Deployment | 5 minutes | ๐ ๏ธ Hands-on |
| Interactive Load Testing | 10 minutes | ๐ฎ Interactive |
| Chaos Agent Attack | 5 minutes | ๐ฅ Challenge |
| Scaling Analysis | 5 minutes | ๐ Analysis |
| Total | 35 minutes |
๐ฏ LEARNING OBJECTIVES
By completing this scenario, you will:
โ
Master Horizontal Pod Autoscaler (HPA) configuration and behavior
โ
Understand resource requests vs limits and their scaling impact
โ
Implement intelligent scaling policies for production workloads
โ
Experience real-time load testing and performance monitoring
โ
Learn how to handle traffic spikes and resource attacks
โ
Defeat Chaos Agent's most powerful resource exhaustion attacks! โก
๐งจ THE CHAOS AGENT'S FINAL ATTACK
"Your static deployments are DOOMED! I'll launch massive traffic spikes that will overwhelm your servers! Watch as your applications crash under the weight of my resource exhaustion attacks! Your manual scaling is NO MATCH for my chaos!" ๐๐ฅ
What Chaos Agent Exploits: - โ Fixed replica counts that can't handle traffic spikes - โ Manual scaling processes that are too slow to respond - โ Resource exhaustion leading to application crashes - โ No intelligent load distribution or capacity planning - โ Inability to scale down, wasting resources continuously - โ Single points of failure under high load conditions
๐ฆธโโ๏ธ THE AUTO-SCALING HERO'S ULTIMATE RESPONSE
"Not this time, Chaos Agent! My Python-powered auto-scaling system will adapt to ANY load you throw at it. Watch as intelligent algorithms automatically provision resources and maintain perfect performance!" ๐ฆธโโ๏ธ๐
How Auto-scaling Hero Wins: - โ Horizontal Pod Autoscaler (HPA) - Intelligent scaling based on CPU metrics - โ Smart scaling policies - Aggressive scale-up, conservative scale-down - โ Resource optimization - Perfect requests/limits balance - โ Real-time monitoring - Live visibility into scaling decisions - โ Load testing platform - Interactive chaos simulation - โ Predictive scaling - Proactive resource management - โ Zero-downtime scaling - Seamless capacity adjustments
๐ FILE STRUCTURE
scenarios/03-auto-scaling/
โโโ README.md # This comprehensive guide
โโโ deploy-auto-scaling-hero.py # ๐ Main auto-scaling deployment
โโโ monitor-scaling.py # ๐ Real-time scaling monitor
โโโ load-test.py # โก Load testing utility
โโโ requirements.txt # Python dependencies
โโโ participant-guide.md # Step-by-step instructions
โโโ troubleshooting.md # Auto-scaling troubleshooting
โโโ docs/
โโโ hpa-deep-dive.md # HPA technical explanation
โโโ scaling-best-practices.md # Production scaling guide
โโโ metrics-and-monitoring.md # Monitoring setup guide
๐ QUICK START (For Participants)
Prerequisites
- โ Scenario 2 completed (secure todo app should still be running)
- โ Kubernetes cluster with metrics-server (Docker Desktop includes this)
- โ Python 3.10+ with required libraries
- โ kubectl configured and working
Step 1: Environment Setup (2 minutes)
# Navigate to auto-scaling scenario
cd scenarios/03-auto-scaling
# Install auto-scaling dependencies
pip3 install -r requirements.txt
# Verify metrics server is available
kubectl top nodes
Step 2: Deploy Auto-scaling Challenge (5 minutes)
# Run the auto-scaling hero deployment
python3 deploy-auto-scaling-hero.py
Expected Output:
๐ AUTO-SCALING HERO DEPLOYMENT STARTING
======================================================================
๐ Creating scaling challenge namespace: scaling-challenge
โ
Scaling namespace created
๐ Deploying scalable demonstration application...
โ
Scalable application deployed
โก Deploying load testing application...
โ
Load testing application deployed
๐ Creating Horizontal Pod Autoscaler (HPA)...
โ
HPA created with intelligent scaling policies
๐ Creating application services...
โ
Services created for scaling demo and load testing
โณ Waiting for auto-scaling deployments to be ready...
โ
scaling-demo-app ready! 1/1 pods
โ
load-tester ready! 1/1 pods
๐ Checking metrics server availability...
โ
Metrics server is available
======================================================================
๐ AUTO-SCALING HERO DEPLOYMENT SUCCESSFUL!
โ
Interactive auto-scaling challenge ready!
======================================================================
๐ฏ ACCESS YOUR AUTO-SCALING CHALLENGE:
๐ป Primary: http://localhost:31003
๐ง Port Forward: kubectl port-forward svc/scaling-demo-service -n scaling-challenge 31503:80
๐ Then access: http://localhost:31503
Step 3: Access Your Auto-scaling Challenge (Immediate)
The script provides environment-specific access methods:
๐ณ Docker Desktop Environment:
๐ป Primary: http://localhost:31003
๐ Fallback: Port forwarding (see universal access below)
๐ฏ Minikube Environment:
# Get Minikube IP and access
minikube service scaling-demo-service -n scaling-challenge --url
# Or use: http://$(minikube ip):31003
โ๏ธ Cloud Environment (EKS/GKE/AKS):
# Get node external IP
kubectl get nodes -o wide
# Access: http://<external-ip>:31003
๐ Universal Access (Always Works):
# Port forwarding - conflict-free with other services
kubectl port-forward svc/scaling-demo-service -n scaling-challenge 31503:80
# Then access: http://localhost:31503
Step 4: Interactive Load Testing (10 minutes)
- ๐ Open the Auto-scaling Dashboard:
- Access the application at the URL provided
- View the real-time scaling dashboard
-
Observe current pod count and resource usage
-
โก Launch Chaos Agent Attacks:
- Click "Launch Traffic Spike" button
- Watch pods scale up automatically
-
Monitor CPU usage and response times
-
๐ฎ Interactive Load Testing:
- Use the load testing controls
- Adjust traffic intensity
-
Observe scaling behavior in real-time
-
๐ Monitor Scaling Decisions:
- Watch HPA metrics
- See scaling events
- Understand scaling policies
Step 5: Real-time Scaling Monitor (5 minutes)
# Run the scaling monitor
python3 monitor-scaling.py
# Choose monitoring options:
# 1 = Current scaling status
# 2 = HPA metrics analysis
# 3 = Resource usage tracking
# 4 = Continuous monitoring
๐ฌ LIVE DEMO WALKTHROUGH (For Instructors)
Demo Script Overview
Part 1: Chaos Agent's Resource Attack (3 minutes)
# Show the resource exhaustion chaos
./demo-script.sh
What Students See: 1. Static deployment overwhelmed by traffic 2. Manual scaling too slow to respond 3. Application crashes under load 4. "This is what happens without auto-scaling!"
Part 2: Auto-scaling Hero Saves the Day (4 minutes)
# Deploy the auto-scaling solution
python3 deploy-auto-scaling-hero.py
Key Teaching Points: - ๐ HPA automatically scales based on CPU - ๐ Smart scaling policies prevent thrashing - ๐ Resource optimization for efficiency - ๐ Real-time monitoring and visibility
Part 3: Interactive Chaos Testing (3 minutes)
- Launch traffic spikes
- Watch automatic scaling
- Demonstrate load testing
- Show scaling efficiency
๐ HPA CONFIGURATION
Horizontal Pod Autoscaler Setup
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: scaling-demo-hpa
namespace: scaling-challenge
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: scaling-demo-app
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
Resource Requests and Limits
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
๐งช AUTO-SCALING TESTING
Test 1: Traffic Spike Simulation
# Generate load to trigger scaling
kubectl run load-generator --image=busybox --command -- sh -c "while true; do wget -qO- http://scaling-demo-service.scaling-challenge.svc.cluster.local; done"
# Watch pods scale up
kubectl get pods -n scaling-challenge -w
Test 2: HPA Metrics Verification
# Check HPA status
kubectl get hpa -n scaling-challenge
# View HPA details
kubectl describe hpa scaling-demo-hpa -n scaling-challenge
Test 3: Scale Down Test
# Stop load generation
kubectl delete pod load-generator
# Watch pods scale down
kubectl get pods -n scaling-challenge -w
Test 4: Resource Pressure Test
# Create CPU pressure
kubectl run cpu-stress --image=busybox --requests=cpu=1000m --limits=cpu=2000m --command -- stress --cpu 4
# Watch scaling response
kubectl top pods -n scaling-challenge
๐ SCALING MONITORING
Real-time Scaling Dashboard
# Run scaling monitor
python3 monitor-scaling.py
# Features:
# - Current pod count and target
# - CPU and memory utilization
# - Scaling events and history
# - Performance metrics
# - Resource efficiency analysis
HPA Metrics Analysis
# Check HPA metrics
kubectl get hpa scaling-demo-hpa -n scaling-challenge -o yaml
# View scaling events
kubectl get events -n scaling-challenge --sort-by='.lastTimestamp' | grep HPA
Resource Usage Tracking
# Monitor resource usage
kubectl top pods -n scaling-challenge
# Check node resource allocation
kubectl top nodes
๐ฏ SUCCESS CRITERIA
โ Scenario 03 Complete Checklist:
- โ Auto-scaling application deployed successfully
- โ HPA configured with intelligent policies
- โ Load testing platform operational
- โ Real-time scaling dashboard working
- โ Traffic spike handling verified
- โ Scale down efficiency confirmed
- โ Chaos Agent's resource attacks defeated! โก
Key Learning Outcomes:
- โ HPA Configuration - Mastered auto-scaling setup
- โ Resource Management - Optimized requests and limits
- โ Scaling Policies - Implemented smart scaling behavior
- โ Load Testing - Experienced real-time scaling
- โ Performance Monitoring - Tracked scaling efficiency
- โ Chaos Resilience - Survived resource exhaustion attacks
๐ NEXT STEPS
What's Next:
- Scenario 04: Blue-Green Deployment Strategies
- Scenario 05: GitOps with ArgoCD and Argo Rollouts
Production Auto-scaling:
- Apply these HPA patterns to production applications
- Implement custom metrics for application-specific scaling
- Add predictive scaling based on historical patterns
- Regular load testing and capacity planning
๐ TROUBLESHOOTING
Common Auto-scaling Issues:
Issue: HPA not scaling
# Solution: Check metrics server
kubectl top nodes
kubectl top pods -n scaling-challenge
Issue: Pods not scaling up
# Solution: Check HPA configuration
kubectl describe hpa scaling-demo-hpa -n scaling-challenge
Issue: Scaling too aggressively
# Solution: Adjust scaling policies
kubectl patch hpa scaling-demo-hpa -n scaling-challenge --type='merge' -p='{"spec":{"behavior":{"scaleUp":{"stabilizationWindowSeconds":60}}}}'
Issue: Resource limits too low
# Solution: Increase resource limits
kubectl patch deployment scaling-demo-app -n scaling-challenge --type='merge' -p='{"spec":{"template":{"spec":{"containers":[{"name":"app","resources":{"limits":{"cpu":"1000m","memory":"512Mi"}}}]}}}}'
๐ CONCLUSION
Congratulations! You've successfully defeated Chaos Agent's resource exhaustion attacks! โก
What You've Accomplished:
- โ Implemented intelligent auto-scaling with HPA
- โ Built real-time load testing platform
- โ Created comprehensive scaling monitoring
- โ Proven chaos resilience through testing
- โ Optimized resource management for efficiency
Key Auto-scaling Takeaways:
- HPA provides intelligent scaling based on metrics
- Resource optimization prevents waste and improves performance
- Real-time monitoring enables proactive scaling
- Load testing validates scaling behavior
- Smart scaling policies prevent thrashing
You're now ready for the next challenge: Blue-Green Deployments! ๐
Remember: In the world of Kubernetes auto-scaling, intelligence and automation are your weapons against chaos! ๐