Scaling — Cheat Sheet
Kubernetes · 1 topics. Download the PDF or the Instagram carousel and share it.
Cheat Sheet · AiCanCode.org
Scaling
Kubernetes1 topicsQuick revision reference
1
Autoscaling — HPA, VPA & Cluster Autoscaler
Kubernetes has three autoscaling dimensions: HPA scales pod replicas based on CPU/memory/custom metrics, VPA adjusts pod resource requests, and Cluster Autoscaler adds/removes nodes based on pending pods.
- ✓HPA scales pod count based on CPU/memory/custom metrics — requires Metrics Server.
- ✓KEDA extends HPA to 50+ event sources (Kafka lag, SQS depth, Redis) and can scale to zero.
- ✓Cluster Autoscaler adds nodes for Pending pods and removes underutilised nodes — reduces cloud cost.
- ✓VPA recommends right-sized resource requests based on observed usage — start with updateMode: Off.
- ✓Never run HPA and VPA in Auto mode on the same deployment — they conflict. VPA: Off mode + HPA is safe.
- ✓Scale-to-zero with KEDA dramatically reduces costs for batch/event-driven workloads with idle periods.
hpa.yaml — HPA with CPU and custom metrics
# Prerequisite: install metrics-server
# kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# hpa.yaml — scale on CPU utilization
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # target: keep avg CPU at 70%
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 400Mi # keep avg memory usage under 400Mi
# Scale on custom metric (Prometheus via prometheus-adapter)
- type: External
external:
metric:
name: http_requests_per_second
selector:
matchLabels:
app: my-app
target:
type: AverageValue
averageValue: "1000" # 1000 req/s per pod
kubectl get hpa
# NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS
# my-app-hpa Deployment/my-app 45%/70% 2 20 3
# ↑ current/target
kubectl describe hpa my-app-hpa # see scale events and reasoningLearn this free with Aria, your AI tutor → AiCanCode.org/learn/kubernetes