CKA — Scaling & HPA

4 minute read

CKA Exam Domain

Workloads & Scheduling — 15%

Use the scale and autoscale commands to manage Deployments.

Scaling is a fundamental cluster operations skill. The CKA exam expects you to manually scale workloads and understand how the HorizontalPodAutoscaler (HPA) works.

Manual Scaling

The simplest way to scale a Deployment:

# Scale up
kubectl scale deployment web --replicas=5

# Scale down
kubectl scale deployment web --replicas=2

# Scale to zero (stop all Pods)
kubectl scale deployment web --replicas=0

You can also scale other workload types:

kubectl scale replicaset my-rs --replicas=3
kubectl scale statefulset db --replicas=3

Verify scaling

kubectl get deployment web
kubectl get pods -l app=web

Exam tip: kubectl scale is fast and straightforward. Use it for any question that asks you to change the replica count.

HorizontalPodAutoscaler (HPA)

The HPA automatically scales the number of Pods in a Deployment (or ReplicaSet/StatefulSet) based on observed CPU utilization, memory usage, or custom metrics.

How HPA Works

  1. HPA queries the Metrics Server every 15 seconds (default)
  2. Compares current metric value against the target
  3. Calculates the desired replica count:
    desiredReplicas = ceil(currentReplicas × (currentMetric / targetMetric))
    
  4. Scales the Deployment up or down accordingly

Example

If you have 3 replicas at 80% CPU and your target is 50%:

desiredReplicas = ceil(3 × (80 / 50)) = ceil(4.8) = 5

Metrics Server

The Metrics Server is a prerequisite for HPA. It collects resource usage data from kubelets and exposes it via the Metrics API.

Install Metrics Server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Note: In local clusters (minikube, kind), you may need to add --kubelet-insecure-tls to the Metrics Server Deployment args.

Verify Metrics Server

# Check if Metrics Server is running
kubectl get pods -n kube-system | grep metrics-server

# Query node metrics
kubectl top nodes

# Query Pod metrics
kubectl top pods

# Pod metrics in a specific namespace
kubectl top pods -n kube-system

Example output:

$ kubectl top nodes
NAME           CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
control-plane  250m         12%    1024Mi          54%
worker-1       180m         9%     768Mi           40%

$ kubectl top pods
NAME                   CPU(cores)   MEMORY(bytes)
web-6d4cf56db6-abc12   5m           32Mi
web-6d4cf56db6-def34   8m           35Mi
web-6d4cf56db6-ghi56   6m           30Mi

Creating HPA

Imperative (fastest for the exam)

kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=50

This creates an HPA that:

  • Keeps a minimum of 2 replicas
  • Scales up to a maximum of 10 replicas
  • Targets 50% average CPU utilization

Declarative YAML

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: 50
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70

Important: For HPA to work, your Pods must have resource requests defined. HPA calculates utilization as a percentage of the requested resources.

Verify HPA

kubectl get hpa

Example output:

NAME      REFERENCE        TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
web-hpa   Deployment/web   35%/50%         2         10        3          5m

The TARGETS column shows current/target. If you see <unknown>/50%, the Metrics Server may not be running or Pods may not have resource requests set.

HPA Behavior

Starting from autoscaling/v2, you can fine-tune how quickly HPA scales up and down:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 20
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Percent
        value: 100
        periodSeconds: 60
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Percent
        value: 10
        periodSeconds: 60
Parameter What It Does Default
stabilizationWindowSeconds (scaleUp) Prevents flapping by looking at metrics over this window 0 (scale up immediately)
stabilizationWindowSeconds (scaleDown) Waits this long before scaling down 300 seconds (5 min)
policies.type: Percent Scale by a percentage of current replicas —
policies.type: Pods Scale by a fixed number of Pods —

Key insight: By default, HPA scales up quickly and scales down slowly to prevent oscillation.

Custom Metrics Scaling

Beyond CPU and memory, HPA can scale based on custom metrics (e.g., requests per second from Prometheus) or external metrics (e.g., queue length from a cloud service).

This requires a custom metrics adapter like:

  • Prometheus Adapter
  • KEDA (Kubernetes Event-Driven Autoscaling)

CKA scope: The CKA exam focuses on CPU/memory-based HPA. Custom metrics are more relevant to CKAD and advanced setups.

kubectl Commands

# Manual scaling
kubectl scale deployment web --replicas=5

# Create HPA
kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=50

# View HPAs
kubectl get hpa

# Detailed HPA info
kubectl describe hpa web-hpa

# Delete HPA
kubectl delete hpa web-hpa

# View resource usage
kubectl top nodes
kubectl top pods
kubectl top pods --containers  # per-container metrics

Practice Questions

Q1. Scale the Deployment backend to 6 replicas.

Show Answer
kubectl scale deployment backend --replicas=6

Verify:

kubectl get deployment backend

Q2. Create an HPA for Deployment api with min 3 replicas, max 15 replicas, targeting 60% CPU utilization.

Show Answer
kubectl autoscale deployment api --min=3 --max=15 --cpu-percent=60

Verify:

kubectl get hpa

Q3. The HPA shows <unknown>/50% in the TARGETS column. What are two possible causes?

Show Answer
  1. Metrics Server is not installed — HPA cannot query resource metrics without it
  2. Pods have no resource requests defined — HPA calculates utilization as a percentage of requests. Without requests, it reports <unknown>

Fix: Install Metrics Server and ensure the Deployment’s Pod spec includes resources.requests.


Q4. What is the default stabilization window for scale-down?

Show Answer

300 seconds (5 minutes). This means HPA waits 5 minutes after the load decreases before scaling down, to prevent rapid scale-up/scale-down oscillation (flapping).


Q5. Scale the Deployment web to zero replicas. Then scale it back to 3.

Show Answer
kubectl scale deployment web --replicas=0
kubectl get pods -l app=web   # Should show no Pods

kubectl scale deployment web --replicas=3
kubectl get pods -l app=web   # Should show 3 Pods

Next — understand how to set resource boundaries and prevent workloads from consuming too many cluster resources.

Next: Resource Limits →

Updated: