DEV Community

Cover image for 🧩Scenario #16 — Horizontal Pod Autoscaling Using YAML Manifest in Kubernetes
Latchu@DevOps
Latchu@DevOps

Posted on

🧩Scenario #16 — Horizontal Pod Autoscaling Using YAML Manifest in Kubernetes

🎯 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
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f nginx-hpa-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Check:

kubectl get pods -l app=nginx-hpa-demo
Enter fullscreen mode Exit fullscreen mode

🧰 Step 2 — Expose the Deployment

kubectl expose deployment nginx-hpa-demo --port=80 --target-port=80
Enter fullscreen mode Exit fullscreen mode

Check:

kubectl get svc nginx-hpa-demo
Enter fullscreen mode Exit fullscreen mode

1


⚙️ Step 3 — Verify Metrics Server

Make sure Metrics Server is running (this is required for HPA):

kubectl get deployment metrics-server -n kube-system
Enter fullscreen mode Exit fullscreen mode

If not present, install:

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Enter fullscreen mode Exit fullscreen mode

2


🧠 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
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f nginx-hpa.yaml
Enter fullscreen mode Exit fullscreen mode

🧩 Step 5 — Verify HPA Status

Check your HPA:

kubectl get hpa nginx-hpa-demo
Enter fullscreen mode Exit fullscreen mode

3

You should see:

NAME              REFERENCE                    TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
nginx-hpa-demo    Deployment/nginx-hpa-demo    0%/50%     1         5         1          1m
Enter fullscreen mode Exit fullscreen mode

🔥 Step 6 — Generate CPU Load

Run a BusyBox pod to generate traffic/load:

kubectl run loader --image=busybox --restart=Never -it -- /bin/sh
Enter fullscreen mode Exit fullscreen mode

Inside:

while true; do wget -q -O- http://nginx-hpa-demo.default.svc.cluster.local > /dev/null & done
Enter fullscreen mode Exit fullscreen mode

Let it run for a few minutes.


📈 Step 7 — Observe Autoscaling

In a new terminal:

kubectl get hpa -w
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

4

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
Enter fullscreen mode Exit fullscreen mode

🌟 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.