Kubernetes Cheatsheet
Scaling
Use this Kubernetes reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Manual Scaling
kubectl scale deployment api --replicas=5 kubectl scale statefulset postgres --replicas=3 # scale multiple at once kubectl scale deployment api worker --replicas=4 # conditional scale (only if current count matches) kubectl scale deployment api --replicas=10 --current-replicas=5
HorizontalPodAutoscaler (HPA)
Automatically adjusts replicas based on metrics. Requires metrics-server installed.
CPU/Memory HPA (autoscaling/v2)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # % of CPU request
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 200MiCustom / External Metrics HPA
metrics:
- type: Pods
pods:
metric:
name: http_requests_per_second
target:
type: AverageValue
averageValue: "500"
- type: External
external:
metric:
name: sqs_queue_depth
selector:
matchLabels:
queue: orders
target:
type: AverageValue
averageValue: "100"HPA Behavior (scale speed)
spec:
behavior:
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Pods
value: 4
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300 # wait 5 min before scaling down
policies:
- type: Percent
value: 10
periodSeconds: 60kubectl get hpa kubectl describe hpa api-hpa # current metrics, target, last scale event kubectl top pods # live CPU/memory (needs metrics-server)
VerticalPodAutoscaler (VPA)
Adjusts CPU/memory requests on existing Pods. Requires the VPA addon.
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: api-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: api
updatePolicy:
updateMode: "Auto" # Auto | Recreate | Initial | Off
resourcePolicy:
containerPolicies:
- containerName: api
minAllowed:
cpu: 100m
memory: 128Mi
maxAllowed:
cpu: 2
memory: 2GiDo not use HPA (CPU/memory) and VPA (Auto mode) together on the same Deployment — they conflict. Use VPA Off mode to just get recommendations, or use KEDA + VPA.
Cluster Autoscaler
Adds/removes Nodes when Pods are unschedulable or Nodes are underutilized. Configured at the Node Group / cloud provider level, not per-workload.
# check autoscaler logs kubectl -n kube-system logs deployment/cluster-autoscaler # annotate node to prevent scale-down kubectl annotate node my-node cluster-autoscaler.kubernetes.io/scale-down-disabled=true
KEDA (Kubernetes Event-Driven Autoscaling)
Scale to zero and scale from external event sources (queues, cron, Prometheus, etc.):
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: worker-scaledobject
spec:
scaleTargetRef:
name: worker
minReplicaCount: 0 # can scale to zero
maxReplicaCount: 50
pollingInterval: 15
cooldownPeriod: 60
triggers:
- type: aws-sqs-queue
metadata:
queueURL: https://sqs.us-east-1.amazonaws.com/123456/orders
queueLength: "10"
awsRegion: us-east-1Node Affinity and Taints (control where Pods land)
Node Selector (simple)
spec:
nodeSelector:
kubernetes.io/arch: arm64Node Affinity (expressive)
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-type
operator: In
values: [gpu]
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: zone
operator: In
values: [us-east-1a]Taints and Tolerations
# taint a node (no Pods without toleration) kubectl taint nodes gpu-node gpu=true:NoSchedule # remove taint kubectl taint nodes gpu-node gpu=true:NoSchedule-
spec:
tolerations:
- key: gpu
operator: Equal
value: "true"
effect: NoSchedulePod Anti-Affinity (spread across zones)
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
topologyKey: topology.kubernetes.io/zone
labelSelector:
matchLabels:
app: apiTopology Spread Constraints
Distribute Pods evenly across zones/nodes:
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: api