Auto-scaling (HPA & VPA)

Automatically adjust the number of running Pod replicas or node pool sizes based on real-time application resource consumption.

1. Prerequisites: Metrics Server

Before HPA can scale pods based on CPU or Memory metrics, the Kubernetes Metrics Server must be deployed in your cluster:

# Deploy Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# Check node & pod CPU/RAM metrics
kubectl top nodes
kubectl top pods

2. Production Horizontal Pod Autoscaler (HPA) Manifest

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 75 # Scale up when average CPU exceeds 75%!

Next Up

Learn Helm Package Manager: Helm Charts, values.yaml, helm install, upgrade, and rollback commands.

Next Lesson: Helm Package Manager →