How to Set Up Horizontal Pod Autoscaling in Kubernetes

Horizontal Pod Autoscaling (HPA) automatically adjusts the number of running Pod replicas based on observed CPU, memory, or custom metrics — scaling up under load and back down when demand drops, without manual intervention.

Prerequisites

  • A working Kubernetes cluster with the Metrics Server installed
  • An existing Deployment to scale

Step 1 — Install the Metrics Server (If Not Already Present)

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

The Metrics Server collects resource usage data that HPA relies on to make scaling decisions.

Step 2 — Verify Metrics Are Available

kubectl top nodes
kubectl top pods

Step 3 — Ensure Your Deployment Has Resource Requests Defined

spec:
  containers:
  - name: my-app
    image: my-app:latest
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 500m
        memory: 512Mi

HPA calculates scaling decisions as a percentage of the requests value — without this defined, autoscaling based on CPU/memory won't function correctly.

Step 4 — Create an HPA Resource

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
kubectl apply -f hpa.yaml

This configuration keeps average CPU utilization around 70%, scaling between 2 and 10 replicas as needed.

Step 5 — Create via Command Line (Alternative, Quicker)

kubectl autoscale deployment my-app --cpu-percent=70 --min=2 --max=10

Step 6 — Monitor Autoscaling in Action

kubectl get hpa my-app-hpa --watch

Shows current CPU utilization vs target, and current replica count, updating live.

Testing Autoscaling with a Load Generator

kubectl run load-test --image=busybox --restart=Never -- /bin/sh -c \
  "while true; do wget -q -O- http://my-app-service; done"

Generates continuous load against your service to observe the HPA scale up in real time; delete this test Pod once done (kubectl delete pod load-test).

Scaling on Memory Instead of (or Alongside) CPU

metrics:
- type: Resource
  resource:
    name: memory
    target:
      type: Utilization
      averageUtilization: 80

Common Errors

HPA shows "unknown" for current metrics — the Metrics Server isn't installed or isn't functioning correctly; verify with kubectl top pods first.

HPA never scales despite high load — verify resource requests are actually defined on the target Deployment's containers; this is the single most common oversight.

FAQ

Does HPA also scale the underlying VPS/node resources?
No — HPA only scales the number of Pod replicas within existing node capacity; scaling the nodes themselves (Cluster Autoscaler) is a separate, more advanced mechanism not typically applicable to a fixed set of VPS instances.

Continue Reading

Browse more articles in Kubernetes & Container Orchestration.

  • kubernetes autoscaling, horizontal pod autoscaler, hpa, kubernetes scaling
  • 0 Корисниците го најдоа ова како корисно
Дали Ви помогна овој одговор?

Понудени резултати

What Is Kubernetes and When Do You Need It on a VPS?

Kubernetes is a container orchestration platform — it automates deploying, scaling, and...

How to Install a Single-Node Kubernetes Cluster with k3s

k3s is a lightweight, certified Kubernetes distribution designed to run efficiently on modest...

How to Install kubeadm and Set Up a Multi-Node Kubernetes Cluster

kubeadm is the official tool for bootstrapping a standard, full-featured Kubernetes cluster. This...

Kubernetes Pods, Deployments & Services Explained

Understanding these three core Kubernetes objects — Pods, Deployments, and Services —...

How to Expose Applications with a Kubernetes Ingress Controller (Nginx Ingress)

An Ingress lets you route external HTTP/HTTPS traffic to multiple services within your cluster...