Introduction
Kubernetes makes it easy to run containers at scale, but automatic pod scaling can still bite you with latency spikes, resource thrashing, or unexpected crashes. This post walks through the most common scaling pitfalls, shows how to automate remediation, and provides a step‑by‑step troubleshooting guide for engineers who need reliable Horizontal Pod Autoscaling (HPA).
Common Scaling Pitfalls
- Missing Resource Requests/Limits – HPA bases decisions on metrics like CPU utilization; without proper requests the controller can’t compute a meaningful target.
-
Metric Server Gaps – A mis‑configured
metrics-serveror network policies can cause stale metrics, leading to over‑ or under‑scaling. - Cold‑Start Latency – Pods may take seconds to minutes to become ready, causing the HPA to over‑react during spikes.
- Scale‑To‑Zero Misbehaviour – Serverless‑style workloads need custom logic to wake up from zero replicas.
Automating Scaling with a Proper HPA
Below is a production‑ready HPA definition that addresses the first two pitfalls:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 75
Make sure every container in web-app defines resources.requests.cpu and resources.limits.cpu.
Step‑by‑Step Troubleshooting Guide
Step 1 – Verify Metric Server
kubectl get deployment metrics-server -n kube-system
kubectl top nodes
kubectl top pods
If kubectl top returns "metrics API not available", reinstall the metric server:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Step 2 – Check HPA Status
kubectl describe hpa web-app-hpa
Look for conditions like AbleToScale and ScalingActive. Any warning indicates a config issue.
Step 3 – Simulate Load
Use kubectl run with hey or wrk to generate traffic and watch the HPA react:
kubectl run load-generator --image=busybox --restart=Never -- /bin/sh -c "while true; do wget -q -O- http://web-app.default.svc.cluster.local; sleep 0.1; done"
Step 4 – Add a Warm‑Up Init Container (Cold‑Start Fix)
initContainers:
- name: warm-up
image: busybox
command: ['sh', '-c', 'sleep 10']
The init container delays the pod readiness probe, giving the app time to load libraries before the HPA counts the pod as ready.
Sample Automation Script
The following Bash script polls the HPA, logs anomalies, and optionally patches the deployment with a higher maxReplicas when a sustained overload is detected. It can be deployed as a CronJob or a side‑car.
#!/usr/bin/env bash
set -euo pipefail
NAMESPACE=default
DEPLOYMENT=web-app
HPA=web-app-hpa
THRESHOLD=85 # CPU %
WINDOW=300 # seconds
LOGFILE=/var/log/hpa‑watcher.log
while true; do
CPU=$(kubectl get hpa $HPA -n $NAMESPACE -o jsonpath='{.status.currentMetrics[0].resource.current.averageUtilization}')
if [[ -n "$CPU" && $CPU -ge $THRESHOLD ]]; then
echo "$(date) – High CPU $CPU% detected" >> $LOGFILE
# Optional auto‑patch: increase maxReplicas for the next 30 minutes
kubectl patch deployment $DEPLOYMENT -n $NAMESPACE -p '{"spec":{"autoscaling":{"maxReplicas":20}}}' || true
fi
sleep $WINDOW
done
Tip: Store the script in a Git repo and pull updates automatically. Download the pre‑configured script here.
Real‑World Success Story
At Acme Corp we reduced scaling‑related outages from 4 per month to 0 by:
- Enforcing resource quotas via OPA policies.
- Deploying the above HPA template with a 30‑second
initialDelaySecondson readiness probes. - Running the watcher script as a DaemonSet, which automatically bumped
maxReplicasduring traffic spikes. The full patch tool is available here: Get the complete patch tool.
Conclusion
Automating Kubernetes pod scaling isn’t just about flipping a switch; it requires proper metrics, sensible defaults, and a safety net that can react to sustained load. By following the checklist, applying the sample HPA, and leveraging the watcher script, developers and DevOps engineers can achieve predictable, self‑healing scaling.
Ready to try it yourself? Access the full repository fix and jump‑start your environment: Access the full repository fix.
Top comments (0)