Skip to content
Back to Deployment

Kubernetes Autoscaling

HPA, VPA, and Cluster Autoscaler for scaling pods and nodes.

ContainersAdvanced

Why

K8s autoscaling adjusts to demand: HPA scales pod replicas by CPU/memory/custom metrics, VPA adjusts resource requests, Cluster Autoscaler adds/removes nodes. Docker is used by 59% of professional developers per the Stack Overflow 2024 survey, and Kubernetes runs 60% of containerized workloads in production (CNCF 2024 survey).

Container orchestration reduces deployment-related incidents by 35% through declarative rollouts.

When

Use HPA for stateless web apps, VPA for resource right-sizing, Cluster Autoscaler for node scaling. Skip containers for simple static sites — a CDN + static export is cheaper and faster.

Avoid Kubernetes for teams under 10 people; the operational overhead is not justified.

How

1. HPA — HorizontalPodAutoscaler, scales replicas 2. Metrics — CPU, memory, or custom (Prometheus) 3. VPA — VerticalPodAutoscaler, adjusts requests 4. Cluster Autoscaler — adds nodes when pods pending 5. KEDA — event-driven autoscaling (SQS, Kafka) 6. Min/max — set bounds for safety 7. Cooldown — scaleDown stabilizationWindowSeconds

Example

# hpa.yaml — scale on CPU apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: { name: myapp } spec: scaleTargetRef: { kind: Deployment, name: myapp } minReplicas: 2 maxReplicas: 20 metrics: - type: Resource resource: { name: cpu, target: { type: Utilization, averageUtilization: 70 } } behavior: scaleDown: stabilizationWindowSeconds: 300 # wait 5min before down # Custom metric (Prometheus adapter) metrics: - type: Pods pods: metric: { name: http_requests_per_second } target: { type: AverageValue, averageValue: 100 }

Common Problems

  • ⚠HPA not scaling — metrics-server not installed, required for CPU/memory
  • ⚠Thrashing — scale up/down too fast, set stabilizationWindowSeconds
  • ⚠Configuration drift between environments — use infrastructure-as-code (Terraform/Pulumi) to keep dev/staging/prod identical

Best Practices

  • ✓Set minReplicas > 0 — avoids scale to zero, use KEDA for zero-scaling
  • ✓Use custom metrics for real load — CPU doesn't reflect queue depth or RPS
  • ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit

FAQ