Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 417 of 8026. Kubernetes Traffic and ScalingFree lesson

Avoiding Conflicts Between HPA and VPA

You will learn the architectural limitations of using Horizontal and Vertical Pod Autoscalers together and how to safely implement them for different metrics.

In this lesson, you will learn why running Horizontal Pod Autoscalers (HPA) and Vertical Pod Autoscalers (VPA) together on the same resource metrics creates a feedback loop that leads to unstable infrastructure.

The Conflict Mechanism

The core architectural limitation arises from their competing goals. The HPA aims to maintain a target utilization percentage by adding or removing replicas, while the VPA adjusts the resource requests of each pod to match actual usage.

If both are configured to scale based on the same metric (e.g., CPU utilization), you create a "scaling war":

  1. The HPA detects high CPU usage and adds more replicas.
  2. The VPA observes the resulting load distribution and attempts to "optimize" requests, potentially causing pod evictions to apply new resource limits.
  3. As the VPA evicts pods, the HPA sees the sudden drop in capacity and reacts by adding even more replicas.
  4. The cycle continues, leading to "thrashing"—where your cluster fluctuates wildly in both pod count and container resource allocations.

How to Safely Combine Them

To implement both effectively, you must decouple their trigger mechanisms. The most reliable pattern is to use the VPA for CPU and Memory "right-sizing" and the HPA for replica scaling based on external custom metrics (such as request throughput or queue length).

If you must use both, ensure they operate on different signals. For example:

# HPA monitors custom metrics like 'http_requests_per_second'
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
spec:
  metrics:
  - type: Pods
    pods:
      metric:
        name: http_requests_per_second
# VPA is configured to manage 'cpu' and 'memory' requests
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
spec:
  updatePolicy:
    updateMode: "Auto"

By ensuring the HPA scales based on traffic volume rather than resource utilization, you prevent it from interfering with the VPA’s attempts to tune the container's hardware footprint. Always favor the VPA in "Recommendation" mode during initial testing to observe how it impacts your HPA’s trigger thresholds before enabling full automation.

Key Takeaway: Never point HPA and VPA at the same resource metric; use VPA for resource requests and HPA for external traffic or custom business metrics to maintain cluster stability.