How does horizontal scaling differ from vertical scaling in Kubernetes?
Compare horizontal and vertical scaling in Kubernetes: HPA vs VPA, scaling out vs up, node limits, and when to use each with YAML examples.
Expected Interview Answer
Horizontal scaling adds or removes Pod replicas to handle load (scaling out), while vertical scaling adjusts the CPU and memory resources allocated to each Pod (scaling up).
In Kubernetes, horizontal scaling is driven by the HorizontalPodAutoscaler (HPA), which changes a Deployment's replica count based on metrics like CPU utilization or custom metrics. Vertical scaling is handled by the VerticalPodAutoscaler (VPA), which right-sizes the resources.requests and limits of containers, typically recreating Pods to apply new values. Horizontal scaling improves availability and throughput for stateless workloads, whereas vertical scaling suits workloads that are hard to parallelize but is bounded by a single node's capacity.
- Horizontal scaling boosts availability and throughput
- Handles traffic spikes by adding stateless replicas
- Vertical scaling right-sizes resources to reduce waste
- No app redesign needed to scale vertically
- HPA and VPA can automate both based on metrics
AI Mentor Explanation
Horizontal scaling is fielding more players across the ground so more balls are covered at once — you spread the workload across many bodies. Vertical scaling is instead making one star all-rounder stronger and faster so a single fielder covers more ground; powerful, but one player can only stretch so far before hitting a physical limit.
Step-by-Step Explanation
Step 1
Set resource requests
Define CPU/memory requests and limits so the scheduler and autoscalers have a baseline to reason about.
Step 2
Configure HPA
Create a HorizontalPodAutoscaler targeting a Deployment with a metric (e.g. 70% CPU) and min/max replicas.
Step 3
Scale out on load
HPA increases replica count when metrics exceed the target, spreading traffic across more Pods.
Step 4
Configure VPA (optional)
Use a VerticalPodAutoscaler to recommend or auto-apply right-sized requests, recreating Pods to take effect.
Step 5
Choose per workload
Prefer horizontal for stateless services; use vertical for hard-to-parallelize workloads, mindful of node capacity.
What Interviewer Expects
- Horizontal = more replicas; vertical = bigger Pods
- Knowledge of HPA and VPA controllers
- Understanding that vertical is bounded by a single node
- Stateless workloads scale horizontally best
- Awareness that HPA and VPA can conflict on the same metric
Common Mistakes
- Confusing scaling out (replicas) with scaling up (resources)
- Running HPA and VPA on CPU for the same workload simultaneously
- Forgetting resource requests, which HPA needs to compute utilization
- Assuming stateful apps scale horizontally without redesign
- Ignoring node capacity limits when scaling vertically
Best Answer (HR Friendly)
“Horizontal scaling means running more copies of an app to share the load, while vertical scaling means giving one copy more CPU and memory. Kubernetes can automate both, but adding more copies is usually the more flexible way to handle growing traffic.”
Code Example
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70# Scale out to 5 replicas immediately
kubectl scale deployment web --replicas=5
# Inspect current autoscaler status
kubectl get hpa web-hpaFollow-up Questions
- Why do HPA and VPA conflict when both watch CPU?
- What metrics besides CPU can drive an HPA?
- How does the Cluster Autoscaler complement horizontal scaling?
- Why is vertical scaling bounded by a single node?
- How does HPA compute utilization from resource requests?
MCQ Practice
1. Which Kubernetes object adds or removes Pod replicas automatically?
The HPA changes a workload's replica count based on observed metrics like CPU utilization.
2. What does vertical scaling adjust in Kubernetes?
Vertical scaling (VPA) right-sizes each Pod's resource requests and limits.
3. Why is horizontal scaling usually preferred for web services?
Adding replicas spreads load and improves availability without being capped by one node's capacity.
Flash Cards
Horizontal scaling — Adding or removing Pod replicas (scaling out/in), automated by the HorizontalPodAutoscaler.
Vertical scaling — Increasing or decreasing a Pod's CPU/memory resources (scaling up/down), automated by the VerticalPodAutoscaler.
Which controller drives horizontal scaling? — The HorizontalPodAutoscaler (HPA), using metrics such as CPU utilization or custom metrics.
Main limit of vertical scaling — A Pod can only grow up to the capacity of a single node.