DEV Community

Nishant Bhardwaj
Nishant Bhardwaj

Posted on

Kubernetes HPA: Guide with Apache on KIND

Horizontal Pod Autoscaler (HPA) automatically increases or decreases the number of Pods based on CPU or memory utilization. In this project, I configured HPA on a local KIND cluster and verified autoscaling using CPU-based metrics.


1. Namespace

A Namespace logically separates Kubernetes resources, making it easier to organize and manage applications independently.

namespace.yml

kind: Namespace
apiVersion: v1
metadata:
  name: apache
Enter fullscreen mode Exit fullscreen mode

2. Deployment

The Deployment manages the desired number of application Pods. I configured CPU requests and limits because HPA uses these values to calculate resource utilization and make scaling decisions.

deployment.yml

apiVersion: apps/v1
kind: Deployment

metadata:
  name: apache-deployment
  namespace: apache

spec:
  replicas: 3

  selector:
    matchLabels:
      app: apache-app

  template:
    metadata:
      labels:
        app: apache-app

    spec:
      containers:
        - name: apache

          image: httpd:latest

          imagePullPolicy: Always

          ports:
            - containerPort: 80

          resources:
            requests:
              cpu: "100m"
              memory: "100Mi"

            limits:
              cpu: "200m"
              memory: "250Mi"
Enter fullscreen mode Exit fullscreen mode

3. Service

A Service provides a stable endpoint for accessing the application and distributes incoming traffic across all available Pods.

service.yml

apiVersion: v1
kind: Service
metadata:
  name: apache-sv
  namespace: apache
spec:
  selector:
    app: apache-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
Enter fullscreen mode Exit fullscreen mode

4. Metrics Server

Metrics Server collects CPU and memory usage from each Pod and exposes these metrics to Kubernetes. Without Metrics Server, HPA cannot monitor resource utilization.

Verify Metrics Server

@root-IdeaPad-Gaming-3-15IHU6:~/Code/K8s/django_hpa$ kubectl top nodes -n apache
NAME                              CPU(cores)   CPU(%)   MEMORY(bytes)   MEMORY(%)   
demo-kind-cluster-control-plane   126m         1%       612Mi           7%          
demo-kind-cluster-worker          29m          0%       487Mi           6%          
demo-kind-cluster-worker2         27m          0%       471Mi           6%          
demo-kind-cluster-worker3         34m          0%       520Mi           6%          
@root-IdeaPad-Gaming-3-15IHU6:~/Code/K8s/django_hpa$ kubectl top pods -n apache
NAME                                 CPU(cores)   MEMORY(bytes)   
apache-deployment-67856f954c-b4n2x   1m           13Mi            
apache-deployment-67856f954c-xkmwb   1m           12Mi            
Enter fullscreen mode Exit fullscreen mode

If CPU and memory metrics are displayed, the Metrics Server is working correctly.


5. Horizontal Pod Autoscaler (HPA)

The Horizontal Pod Autoscaler continuously monitors CPU utilization and automatically increases or decreases the number of Pod replicas based on the configured target utilization.

hpa.yml

# Horizontal Pod Autoscaler for Django Todo Application
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: apache-hpa
  namespace: apache
spec:
  # Target the Apache deployment
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: apache-deployment  

  # Scaling limits
  minReplicas: 2
  maxReplicas: 10

  # Metrics to scale on
  metrics:
  # CPU-based scaling
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50  

  # Memory-based scaling
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70  

  # Scaling behavior configuration
  behavior:
    # Scale down policies
    scaleDown:
      stabilizationWindowSeconds: 30 
      policies:
      - type: Percent
        value: 50 
        periodSeconds: 15
      - type: Pods
        value: 2  
        periodSeconds: 15
      selectPolicy: Min  

    # Scale up policies
    scaleUp:
      stabilizationWindowSeconds: 0  
      policies:
      - type: Percent
        value: 100  
        periodSeconds: 15
      - type: Pods
        value: 4  
        periodSeconds: 15
      selectPolicy: Max  
Enter fullscreen mode Exit fullscreen mode

6. Generate Load

Generate continuous HTTP requests to simulate client traffic and trigger autoscaling.

while true; do curl -s http://localhost:8000 > /dev/null; done
Enter fullscreen mode Exit fullscreen mode

7. Monitor Autoscaling

Watch the HPA status in real time.

kubectl get hpa -w
Enter fullscreen mode Exit fullscreen mode

Watch Pods being created or terminated.

kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

Check CPU utilization of each Pod.

kubectl top pods
Enter fullscreen mode Exit fullscreen mode

HPA Workflow

                 Client Requests
                        │
                        ▼
                    Kubernetes Service
                        │
                        ▼
                    Deployment
                        │
                        ▼
                Application Pods
                        │
                        ▼
                 Metrics Server
                        │
                        ▼
        Horizontal Pod Autoscaler
                        │
                        ▼
        Scale Pods Up or Scale Pods Down
Enter fullscreen mode Exit fullscreen mode

Key Learnings

  • HPA automatically scales Pods based on CPU or Memory utilization.
  • Metrics Server is mandatory for CPU-based autoscaling.
  • CPU requests must be defined in the Deployment.
  • kubectl top pods helps monitor real-time resource usage.
  • HPA improves application scalability without manual intervention.

Conclusion

Horizontal Pod Autoscaler makes Kubernetes applications more resilient by automatically adjusting the number of running Pods according to workload demand. This hands-on implementation on a KIND cluster helped me understand the complete autoscaling workflow—from collecting metrics to dynamically scaling application replicas.

Top comments (0)