← All writing

Kubernetes Autoscaling: HPA, VPA & Karpenter vs. Cluster Autoscaler

· Sourabh G Kulkarni

Mastering horizontal and vertical pod autoscaling, why HPA and VPA conflict, and how Karpenter delivers sub-minute just-in-time node provisioning.

Kubernetes Autoscaling: HPA, VPA & Karpenter vs. Cluster Autoscaler

Autoscaling in Kubernetes operates at two distinct layers: Pod Autoscaling (adjusting workload capacity) and Node Autoscaling (adjusting underlying compute hardware).

Misunderstanding the interaction between these layers leads to the two most common scaling failures in production: cascading resource thrashing and waiting 5–10 minutes for cloud VM provisioning while requests time out.

Here is how modern platform teams configure autoscaling using HPA, VPA, and Karpenter.

flowchart TD
    subgraph WorkloadLayer["Workload Layer (Pods)"]
        HPA["Horizontal Pod Autoscaler (HPA)"]
        Metrics["Metrics Server (CPU / Memory / Custom Prometheus)"]
        Pods["ReplicaSet (Scale out 3 -> 25 pods)"]
    end

    subgraph ComputeLayer["Compute Layer (Node Provisioning)"]
        Pending["Unscheduled 'Pending' Pods"]
        Karpenter["AWS Karpenter (JIT Controller)"]
        Nodes["EC2 Right-Sized Instances (Provisioned in < 45s)"]
    end

    Metrics -->|Threshold > 75%| HPA
    HPA -->|Increase Replicas| Pods
    Pods -->|Node Resource Exhaustion| Pending
    Pending -->|Direct EC2 Fleet API| Karpenter
    Karpenter -->|Launch Optimal Spot / On-Demand| Nodes

1. HPA vs. VPA: The Conflict to Avoid

  • Horizontal Pod Autoscaler (HPA): Adds or removes pod replicas based on CPU, memory, or custom metrics (e.g., HTTP request rate via Prometheus Adapter).
  • Vertical Pod Autoscaler (VPA): Adjusts the CPU and memory requests and limits of containers based on historical usage.

⚠️ The Fatal Anti-Pattern:

Never run HPA and VPA on the same resource using CPU or memory metrics simultaneously!

  • HPA will see high CPU and add more pods.
  • VPA will simultaneously see high CPU and resize the pod, restarting it.
  • Both controllers fight in an infinite reconciliation loop.

Production Best Practice: Use VPA in Off (Recommendation-only) mode to right-size requests, and let HPA manage live dynamic scaling.


2. HPA Production Configuration

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: payment-api-hpa
  namespace: production
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: payment-api
  minReplicas: 3
  maxReplicas: 30
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300 # Prevent flapping by waiting 5 min before scaling in
      policies:
        - type: Percent
          value: 10
          periodSeconds: 60

3. The Node Layer: Karpenter vs. Legacy Cluster Autoscaler

For years, the Kubernetes Cluster Autoscaler (CAS) was the default. CAS watches for pending pods, increments an AWS Auto Scaling Group (ASG) desired count, and waits for EC2 instances to boot (~5 to 8 minutes).

Why Karpenter Won:

Karpenter bypasses Auto Scaling Groups completely:

  1. It listens directly to the Kubernetes scheduler for unschedulable pods.
  2. It examines the exact CPU/memory/GPU requests, topology spreads, and node affinity labels of those pods.
  3. It calls the AWS EC2 Fleet API directly and provisions the exact cheapest instance type (Spot or On-Demand) in under 45 seconds.
# Karpenter NodePool definition
apiVersion: karpenter.sh/v1beta1
kind: NodePool
metadata:
  name: general-compute
spec:
  template:
    spec:
      requirements:
        - key: "karpenter.sh/capacity-type"
          operator: In
          values: ["spot", "on-demand"]
        - key: "kubernetes.io/arch"
          operator: In
          values: ["amd64", "arm64"] # Support Graviton for 20% cost reduction
      nodeClassRef:
        name: default
  disruption:
    consolidationPolicy: WhenUnderutilized # Automatically merges underutilized nodes to save cash
    expireAfter: 720h # Rotate nodes every 30 days for automated patch hygiene

By coupling HPA with Karpenter, your platform scales pods in seconds and provisions underlying compute in less than a minute, eliminating traffic drops during sudden demand spikes.