🎯 Goal
Deploy an NGINX Deployment and configure Horizontal Pod Autoscaler using a YAML manifest to scale pods automatically based on CPU utilization.
🧰 Step 1 — Create the NGINX Deployment
cat <<EOF > nginx-hpa-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-hpa-demo
spec:
replicas: 1
selector:
matchLabels:
app: nginx-hpa-demo
template:
metadata:
labels:
app: nginx-hpa-demo
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
limits:
cpu: 200m
EOF
Apply it:
kubectl apply -f nginx-hpa-deploy.yaml
Check:
kubectl get pods -l app=nginx-hpa-demo
🧰 Step 2 — Expose the Deployment
kubectl expose deployment nginx-hpa-demo --port=80 --target-port=80
Check:
kubectl get svc nginx-hpa-demo
⚙️ Step 3 — Verify Metrics Server
Make sure Metrics Server is running (this is required for HPA):
kubectl get deployment metrics-server -n kube-system
If not present, install:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
🧠 Step 4 — Create the HPA Manifest
cat <<EOF > nginx-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-demo
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-hpa-demo
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
EOF
Apply it:
kubectl apply -f nginx-hpa.yaml
🧩 Step 5 — Verify HPA Status
Check your HPA:
kubectl get hpa nginx-hpa-demo
You should see:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-hpa-demo Deployment/nginx-hpa-demo 0%/50% 1 5 1 1m
🔥 Step 6 — Generate CPU Load
Run a BusyBox pod to generate traffic/load:
kubectl run loader --image=busybox --restart=Never -it -- /bin/sh
Inside:
while true; do wget -q -O- http://nginx-hpa-demo.default.svc.cluster.local > /dev/null & done
Let it run for a few minutes.
📈 Step 7 — Observe Autoscaling
In a new terminal:
kubectl get hpa -w
You’ll see something like:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-hpa-demo Deployment/nginx-hpa-demo 75%/50% 1 5 3 2m
Replicas will increase automatically based on CPU load, and scale down when idle.
🧹 Step 8 — Cleanup
kubectl delete -f nginx-hpa.yaml
kubectl delete -f nginx-hpa-deploy.yaml
kubectl delete svc nginx-hpa-demo
🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.
— Latchu | Senior DevOps & Cloud Engineer
☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions




Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.