Skip to content

Configure Kubernetes Auto-Scaling

Medium ~30 min

What you'll build

A Horizontal Pod Autoscaler (HPA) that automatically adds or removes Pods based on CPU utilization, keeping your application responsive under varying load.

Goals

  • Understand horizontal auto-scaling concepts
  • Deploy the Kubernetes Metrics Server
  • Create a Horizontal Pod Autoscaler
  • Test scaling by generating load

Prerequisites

  • A Kubernetes cluster with kubectl
  • An application deployed with CPU resource requests
  • Basic understanding of Kubernetes Deployments

Steps

  1. 1

    Install Metrics Server

    The Metrics Server provides resource usage data to Kubernetes. Install it if not already present in your cluster.

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

    Expected result

    Metrics Server is running. Verify with kubectl top nodes.

  2. 2

    Ensure the Deployment has resource requests

    The HPA needs CPU resource requests to work. Make sure your Deployment specifies resources.requests.cpu.

    Code
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 500m
        memory: 256Mi

    Expected result

    The Deployment has CPU requests set, which the HPA will use for scaling decisions.

  3. 3

    Create the Horizontal Pod Autoscaler

    Create an HPA that scales between 2 and 10 replicas, targeting 50% CPU utilization.

    Code
    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: 50

    Expected result

    The HPA is created and starts monitoring CPU usage.

  4. 4

    Generate load and watch scaling

    Generate load on your application to trigger scaling. Watch the HPA add Pods as CPU usage increases.

    Code
    kubectl get hpa -w
    # In another terminal, generate load:
    kubectl run -i --tty load-generator --rm --image=busybox -- /bin/sh -c "while true; do wget -q -O- http://my-app-service; done"

    Expected result

    As CPU usage rises, the HPA scales up the Pods. When load stops, it scales back down.

  5. 5

    Add a health check endpoint

    Add a /health endpoint returning 200 OK for orchestration systems (Kubernetes, load balancers) to probe liveness. Health checks are foundational for production deployment.

    Code
    # app.py (Flask example)
    from flask import Flask, jsonify
    app = Flask(__name__)
    
    @app.route("/health")
    def health():
        return jsonify({"status": "ok"}), 200
    
    @app.route("/ready")
    def ready():
        # Check DB connection etc.
        return jsonify({"ready": True}), 200

    Expected result

    curl localhost:5000/health returns {"status":"ok"} with a 200 status code.

Try it yourself

What you learned

You configured horizontal auto-scaling that automatically adjusts the number of Pods based on load. Auto-scaling is key to handling variable traffic efficiently — you save resources when demand is low and scale up when demand is high.

Related Resources

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic