Amazon EKS — Scaling Hands-On Lab

1 minute read

In this lab, you’ll configure HPA for a web application, watch it scale under load, and set up VPA for right-sizing recommendations.


Prerequisites

  • Running EKS cluster with Karpenter or Cluster Autoscaler
  • kubectl, helm configured
  • Metrics Server installed

Step 1 — Install Metrics Server

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

# Verify
kubectl get pods -n kube-system -l k8s-app=metrics-server
kubectl top nodes

Step 2 — Deploy a Sample Application

# php-apache.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-apache
  template:
    metadata:
      labels:
        app: php-apache
    spec:
      containers:
        - name: php-apache
          image: registry.k8s.io/hpa-example
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: 200m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 256Mi
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache
spec:
  selector:
    app: php-apache
  ports:
    - port: 80
      targetPort: 80
kubectl apply -f php-apache.yaml

Step 3 — Create HPA

kubectl autoscale deployment php-apache \
  --cpu-percent=50 \
  --min=1 \
  --max=20

# Check HPA
kubectl get hpa

Step 4 — Generate Load

Open a second terminal:

kubectl run load-generator --image=busybox:latest --restart=Never -- \
  /bin/sh -c "while true; do wget -q -O- http://php-apache; done"

Watch the HPA:

kubectl get hpa -w

Expected behavior:

  1. CPU usage rises above 50%
  2. HPA increases replicas (1 → 2 → 4 → …)
  3. If nodes are full, Karpenter/CA adds new nodes
  4. Scaling stabilizes when load is distributed

Step 5 — Stop Load and Watch Scale-Down

kubectl delete pod load-generator

Watch scale-down (takes 5-10 minutes with default stabilization):

kubectl get hpa -w
kubectl get pods -w

Step 6 — VPA Recommendations

Install VPA:

git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler
./hack/vpa-up.sh
cd -

Create VPA in recommendation mode:

# vpa.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: php-apache-vpa
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  updatePolicy:
    updateMode: "Off"
kubectl apply -f vpa.yaml

# Wait a few minutes for data collection, then check recommendations
kubectl describe vpa php-apache-vpa

Step 7 — Apply Recommendations

Based on VPA output, update the Deployment:

# Example: VPA recommends 150m CPU, 110Mi memory
kubectl set resources deployment php-apache \
  --requests=cpu=150m,memory=110Mi \
  --limits=cpu=400m,memory=256Mi

Cleanup

kubectl delete deployment php-apache
kubectl delete svc php-apache
kubectl delete hpa php-apache
kubectl delete vpa php-apache-vpa

Updated: