How do I implement auto-scaling in Kubernetes?

Kubernetes auto-scaling comes in three layers: Horizontal Pod Autoscaler (HPA) for replica count, Vertical Pod Autoscaler (VPA) for pod resource requests, and Cluster Autoscaler for node count. You’ll typically use HPA first, add Cluster Autoscaler when nodes run out of capacity, and use VPA carefully — it restarts pods and can fight with HPA if both target the same workload.


1. Horizontal Pod Autoscaler (HPA)

The Horizontal Pod Autoscaler adjusts replica count for a Deployment, StatefulSet, or ReplicationController based on CPU, memory, or custom metrics.

Prerequisites

Install the Metrics Server so HPA can read resource utilization:

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

Your workload must have resource requests set. Example Deployment:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: nginx
resources:
requests:
cpu: "250m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"

Create the HPA

Scale on CPU utilization (70% average across pods):

yaml
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

Apply and verify:

bash
kubectl apply -f my-app-hpa.yaml
kubectl get hpa


2. Vertical Pod Autoscaler (VPA)

VPA watches actual usage and updates the pod’s resource requests. In “Auto” mode it evicts pods so they restart with new requests — this can cause downtime. Do not run VPA and HPA on the same workload unless VPA is in “Off” or “Initial” mode, or you’ve configured HPA to use custom metrics only.

Install VPA

Apply the official components (includes admission controller, recommender, updater):

bash
kubectl apply -f https://github.com/kubernetes/autoscaler/releases/download/vertical-pod-autoscaler-1.2.0/vpa-release-1.2.0.yaml

Create a VPA object

yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: my-app-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
updatePolicy:
updateMode: "Auto" # Options: "Off", "Initial", "Auto"

Apply and check recommendations:

bash
kubectl apply -f my-app-vpa.yaml
kubectl get vpa my-app-vpa -o yaml


3. Cluster Autoscaler

Cluster Autoscaler adds or removes nodes when pods are pending due to insufficient resources or when nodes are underutilized. There is no single generic manifest — you must deploy the version built for your cloud provider (AWS, GCP, Azure, etc.) and configure cloud-provider flags, IAM roles, and node group labels.

Typical setup

  1. Enable node pool auto-scaling in your managed Kubernetes service (EKS, GKE, AKS).
  2. Deploy the Cluster Autoscaler image matching your Kubernetes minor version.
  3. Set --cloud-provider and any provider-specific flags (e.g., --nodes=2:10:my-node-group for AWS).
  4. Run it in the kube-system namespace with appropriate RBAC.

Check logs to verify scaling decisions:

bash
kubectl logs -f deployment/cluster-autoscaler -n kube-system


Operational notes

  • Monitoring: Wire up Prometheus/Grafana or the Kubernetes Dashboard to watch HPA/VPA/CA metrics and scaling events.
  • Testing: Generate load (e.g., hey or wrk) and confirm each autoscaler reacts as expected.
  • Resource requests: HPA and VPA both depend on accurate requests in pod specs. Missing or wildly wrong requests break scaling logic.
  • Custom metrics: For HPA scaling on non-resource metrics (queue depth, latency), deploy the Prometheus Adapter and define External or Pods metric types.

Liked this briefing? Get it in your inbox

Every morning: the stories that make tech leaders act, with official clips and primary sources. Free, 5-minute read.

Free. One click to unsubscribe. We never share your email. Privacy

How do I implement auto-scaling in Kubernetes?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top