Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Install metrics-server, Create a HorizontalPodAutoscaler at 50% CPU, Generate Load and Watch Pods Scale Out and In

Intermediate45 minkind · kubectl · Docker

Course: DevOps · Chapter 18: Kubernetes Workloads · Lesson: 18.13 Autoscaling: HPA and two names to recognise

Chapter 18 explains the HPA; this lab makes it scale with real load on your laptop.

Chala mitrano! Sale chya divashi traffic dahaa pat. Pod haatane vaadhavnar ka? Nahi, HPA karel.

Suppose we are…

Suppose we are on an Instagram-style team before a big festival campaign. Traffic can jump ten times in minutes and fall back at night. Fixed replicas either waste money or fall over.

In this lab a HorizontalPodAutoscaler watches CPU and adds Pods when average CPU goes above 50% of the request, then removes them when load stops.

Goal of this lab

By the end you will have:

  • metrics-server running, so kubectl top pods works.
  • An HPA with min 1 and max 5 replicas at 50% CPU.
  • Replicas that scale out under load and back in after it stops.

What you need

  • A kind cluster (kind create cluster --name lab).
  • Internet access to pull images.
  • 45 minutes (scale-in waits about 5 minutes by design).

Part 1: metrics-server

  1. Install metrics-server and allow kind's self-signed kubelet certificates (lab only):

    kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
    kubectl -n kube-system patch deploy metrics-server --type=json -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'
    kubectl -n kube-system rollout status deploy/metrics-server
    

    --kubelet-insecure-tls is only for local kind clusters; managed clusters such as EKS do not need it.

  2. Wait one minute, then:

    kubectl top nodes
    

    What you should see: CPU and memory numbers for lab-control-plane.

Part 2: App and HPA

  1. Deploy the CPU demo app (it burns CPU on every request) with a CPU request, and a Service:

    kubectl create deployment php-apache --image=registry.k8s.io/hpa-example --port=80
    kubectl set resources deploy/php-apache --requests=cpu=200m --limits=cpu=500m
    kubectl expose deploy php-apache --port=80
    

    The HPA percentage is measured against the request. Without a CPU request, the HPA shows <unknown>.

  2. Create the HPA as YAML:

    hpa.yaml

    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: php-apache
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: php-apache
      minReplicas: 1
      maxReplicas: 5
      metrics:
        - type: Resource
          resource:
            name: cpu
            target:
              type: Utilization
              averageUtilization: 50
    
  3. Apply and check:

    kubectl apply -f hpa.yaml
    kubectl get hpa php-apache
    

    What you should see: TARGETS cpu: 0%/50% (after a minute), REPLICAS 1.

Part 3: Load, scale out, scale in

  1. In a second terminal start a load generator Pod:

    kubectl run load --rm -it --image=busybox:1.36 --restart=Never -- /bin/sh -c "while true; do wget -q -O- http://php-apache; done"
    
  2. In the first terminal watch the HPA:

    kubectl get hpa php-apache -w
    

    What you should see: CPU rising above 50% and REPLICAS going 1 → 2 → 4 or 5 within a few minutes.

    NAME         REFERENCE               TARGETS        MINPODS   MAXPODS   REPLICAS
    php-apache   Deployment/php-apache   cpu: 0%/50%    1         5         1
    php-apache   Deployment/php-apache   cpu: 248%/50%  1         5         1
    php-apache   Deployment/php-apache   cpu: 248%/50%  1         5         5
    php-apache   Deployment/php-apache   cpu: 58%/50%   1         5         5
    
  3. Stop the load generator with Ctrl+C, then keep watching for about 5 minutes.

    What you should see: CPU drops to 0% and replicas go back to 1 after the scale-down stabilisation window.

Clean up

  1. Delete the lab objects, or the whole cluster:

    kubectl delete hpa php-apache
    kubectl delete svc,deploy php-apache
    # kind delete cluster --name lab
    

Ravindra Bagale's Tip

Set a sensible maxReplicas. Autoscaling without a ceiling can turn a bug or an attack into a very large bill.

Common mistakes

MistakeWhat happensFix
No CPU request on the containerHPA target shows <unknown>Set resources.requests.cpu
metrics-server not readykubectl top errorsWait a minute; on kind add --kubelet-insecure-tls
Expecting instant scale-inReplicas stay high for minutesNormal: scale-down waits ~5 minutes to avoid flapping
Scaling manually while an HPA existsHPA overrides youChange minReplicas/maxReplicas instead

Self-check checklist

0 of 4 done

Try-at-home challenge

Add a behavior.scaleDown.stabilizationWindowSeconds: 60 block to the HPA and compare how fast it scales in. Write one reason a team would want a long window.

Samajla ka? Requests set, ceiling set, HPA does the night shift. Aata pudhe jaauya Services ani volumes.