# Understanding Kubernetes: Part 30 Horizontal Pod Autoscaler (HPA)

***

<figure><img src="https://cdn-images-1.medium.com/max/800/1*b6hQAv_-1I58atHEuY3n1Q.png" alt=""><figcaption></figcaption></figure>

📢 If you’ve been following our Kubernetes series 2025, welcome back! For new readers, check out [Part 29 Service Account](https://techwithpatil.medium.com/understanding-kubernetes-part-29-service-account-0d1dd569b22c)

#### What is a Horizontal Pod Autoscaler (HPA)?

A **Horizontal Pod Autoscaler (HPA)** is a Kubernetes resource that automatically scales the number of pod replicas in a deployment, replica set, or stateful set based on CPU, memory, or custom metrics. It ensures optimal resource utilization and cost efficiency by adjusting the number of pods dynamically.

#### How HPA Works

HPA continuously monitors specified metrics (e.g., CPU or memory usage) and increases or decreases the number of pods to maintain a target threshold. The scaling decision is based on data collected from the **Metrics Server** or external monitoring systems like **Prometheus**.

***

#### Use Cases

**1. Auto-Scaling Based on CPU Usage**

Ensuring that applications scale up when CPU load increases and scale down when demand drops.

**2. Memory-Based Scaling**

Optimizing resource usage by dynamically adjusting pod replicas based on memory consumption.

**3. Scaling with Custom Metrics**

Using external metrics (e.g., request rates, queue length) from Prometheus or an external API to trigger scaling.

**4. Cost Optimization**

Automatically reducing pod count during low traffic periods to save resources.

***

#### HPA Syntax

A Horizontal Pod Autoscaler is created using the following YAML configuration:

```
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: webapp-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: webapp
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50
```

#### Explanation:

* **`scaleTargetRef`** – Specifies the target deployment (`webapp`) to scale.
* **`minReplicas` & `maxReplicas`** – Define the range of pod replicas.
* **`metrics`** – Uses **CPU utilization** as the scaling metric with a target of **50% usage**.

***

#### Applying HPA

To apply the HPA configuration:

```
kubectl apply -f hpa.yaml
```

To check the status of the HPA:

```
kubectl get hpa
```

***

#### Removing HPA

To delete an HPA:

```
kubectl delete hpa webapp-hpa
```

***

#### Conclusion

The **Horizontal Pod Autoscaler (HPA)** is a powerful Kubernetes feature for dynamically adjusting workloads. It helps maintain application performance while optimizing resource usage.

#### In My Previous Role

As a **Senior DevOps Engineer**, I effectively utilized HPA for scaling workloads:

* **CPU-Based Auto-Scaling:** Implemented HPA to scale backend services based on CPU spikes during high traffic periods.
* **Custom Metrics Scaling:** Integrated HPA with Prometheus to scale pods based on request count and queue depth.
* **Cost Optimization:** Used HPA to reduce the number of running pods in low-demand periods, improving cloud cost efficiency.

#### 🚀 Ready to Master Kubernetes?

Take your Kubernetes journey to the next level with the **Master Kubernetes: Zero to Hero course!** 🌟 Whether you’re a beginner or aiming to sharpen your skills, this hands-on course covers:

✅ Kubernetes Basics — Grasp essential concepts like nodes, pods, and services. \
✅ Advanced Scaling — Learn HPA, VPA, and resource optimization. \
✅ Monitoring Tools — Master Prometheus, Grafana, and AlertManager. \
✅ Real-World Scenarios — Build production-ready Kubernetes setups.

#### 🔥 Flash Sale: Buy Kubernetes Course, Get Terraform FREE! Limited Time Offer!

🔥 Start Learning Now: \[Join the Master Kubernetes Course + FREE Access to Terraform Course]\([**https://cloudops0.gumroad.com/l/k8s**](https://cloudops0.gumroad.com/l/k8s))

Don’t miss your chance to become a Kubernetes expert! 💻✨

🚀 Stay ahead in DevOps and SRE! 🔔 [**Subscribe now**](https://techwithpatil.medium.com/subscribe) and never miss a beat on Kubernetes and more. 🌟

#### 🚀 Master Terraform: Infrastructure as Code

🔥 **Start Learning Now:** [Join the Master Terraform Course](https://cloudops0.gumroad.com/l/tf)
