EKS - HPA

Step 1: Ensure Metrics Server is Running

Since HPA relies on the Metrics Server, ensure it's running:

kubectl get deployment -n kube-system metrics-server

If it's not installed, install it:

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

Check if Metrics Server is collecting data:

kubectl top nodes
kubectl top pods

Step 2: Configure HPA for the Nginx Deployment

Create an HPA (Horizontal Pod Autoscaler) YAML file:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment 
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50  

Apply the HPA:

kubectl apply -f nginx-hpa.yaml

Verify the HPA is created:

kubectl get hpa

Step 3: Generate Load on Nginx Deployment

To test autoscaling, generate a CPU load.

Method 1: Using a Load Generator Pod

Run a temporary busybox pod and send HTTP requests in a loop:

kubectl run -it --rm load-generator --image=busybox -- /bin/sh

Inside the pod, run:

while true; do wget -q -O- http://nginx-service.default.svc.cluster.local; done

Exit the pod using Ctrl+C.


Method 2: Using Apache Benchmark (ab)

Install ab on your local machine or inside a pod, then run:

ab -n 100000 -c 100 http://nginx.local/

This sends 100,000 requests with 100 concurrent users.


Step 4: Monitor HPA Scaling

Check HPA scaling events:

kubectl get hpa

Check running pods to see if replicas increased:

kubectl get pods -l app=nginx

Once load decreases, replicas should scale down automatically.


Step 5: Cleanup (Optional)

If you want to remove HPA and stop scaling:

kubectl delete hpa nginx-hpa

Now your Nginx deployment will automatically scale based on CPU usage! 🚀

Updated on