🧠 Concept 14: HPA (Horizontal Pod Autoscaling πŸ’―)

Image

Image

Image

Image


πŸš€ 1. Core Idea (1-line)

πŸ‘‰ HPA automatically increases/decreases number of Pods based on load


🧠 2. Why HPA Exists (VERY IMPORTANT ⚠️)

Without autoscaling:

  • Traffic spike β†’ app crashes ❌

  • Low traffic β†’ wasted resources ❌

πŸ‘‰ Manual scaling is not practical


πŸ’‘ 3. What HPA Does

πŸ‘‰ Based on metrics (usually CPU):

  • High load β†’ increase pods πŸ“ˆ

  • Low load β†’ decrease pods πŸ“‰


βš™οΈ 4. How It Works (VERY IMPORTANT πŸ”₯)

Flow:

Metrics Server β†’ HPA β†’ Deployment β†’ ReplicaSet β†’ Pods

πŸ“Š 5. Example Logic

Target CPU = 50%
 
If current = 80% β†’ scale UP  
If current = 20% β†’ scale DOWN

πŸ“¦ 6. Example YAML

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-hpa
spec:
  scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
resource:
  name: cpu
  target:
type: Utilization
averageUtilization: 50

🧠 7. Requirements (IMPORTANT ⚠️)

πŸ‘‰ HPA needs:

  • Metrics Server installed

  • Resource requests defined ❗


πŸ”₯ 8. Real-world Example (Your Domain πŸ‘€)

ML API:

  • Normal traffic β†’ 2 pods

  • Traffic spike β†’ 10 pods

  • Midnight β†’ back to 2

πŸ‘‰ Fully automatic ⚑


πŸ’₯ 9. Types of Metrics

  • CPU (most common)

  • Memory

  • Custom metrics (Prometheus) πŸ”₯


⚠️ 10. Common Mistakes

❌ Not setting resource requests β†’ HPA fails
❌ No metrics server installed
❌ Wrong thresholds


πŸ’Ό 11. Interview Answer

πŸ‘‰ β€œHPA automatically scales the number of pods in a deployment based on observed metrics like CPU utilization to handle varying workloads efficiently.”


⚑ 12. Commands (CKA πŸ”₯)

kubectl get hpa
kubectl describe hpa <name>
 
kubectl autoscale deployment my-dep --cpu-percent=50 --min=2 --max=10

🧠 13. Memory Trick

πŸ‘‰ HPA = traffic-based scaling πŸ“ˆπŸ“‰


πŸ”₯ 14. Pro Insight (Real-world)

  • Combine:

  • HPA + Cluster Autoscaler

  • For full auto infra scaling πŸ’―


πŸš€ Next Step

Bol:

πŸ‘‰ β€œnext”

Then we go to:
πŸ”₯ Concept 15: Taints & Tolerations (Advanced Scheduling πŸ’― β€” VERY IMPORTANT FOR INTERVIEWS + REAL WORLD)