Creating Basic HPAs
What you'll learn: How to define a Horizontal Pod Autoscaler resource that automatically scales your pods based on CPU usage.
What Is an HPA Resource?
Now that you understand HPA fundamentals, it's time to create one. An HPA resource is a Kubernetes object you define in YAML (or via kubectl) that watches metrics and adjusts replica counts accordingly. The most common starting point is CPU-based scaling—using the resource metrics API (via Metrics Server) to monitor CPU utilization.
Think of an HPA like a smart thermostat: you set a target temperature (CPU threshold), minimum and maximum settings (replica bounds), and it automatically adjusts heating/cooling (pod count) to maintain your target.
Defining a Basic HPA
A minimal CPU-based HPA includes:
scaleTargetRef: Which Deployment/ReplicaSet/StatefulSet to scaleminReplicas: Floor—never scale below thismaxReplicas: Ceiling—never scale above thistargetCPUUtilizationPercentage: Desired average CPU utilization across all pods
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 70
This HPA maintains 2–10 replicas, targeting 70% CPU utilization. If average CPU exceeds 70%, it scales up; below 70%, it scales down (within bounds).
Creating via kubectl
kubectl autoscale deployment my-app --cpu-percent=70 --min=2 --max=10
This imperative command creates the same HPA instantly.
Key Takeaway:
A basic HPA resource defines min/max replica bounds and a CPU utilization target—Kubernetes automatically scales your workload to maintain that threshold, ensuring responsive performance without manual intervention.