• HPA: scales replica count based on metrics
  • VPA: adjusts CPU/memory requests per container (not CKAD)
  • KEDA: event-driven autoscaling (not CKAD)

Prerequisites

  • Metrics Server must be installed (assume present in exam)
  • Target Deployment must have CPU requests set: without it, TARGETS shows <unknown>/50%

HPA YAML

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: webapp-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: webapp
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue         # memory cannot use Utilization
        averageValue: 200Mi

Target types

typeMetric valueTypical use
Utilization% of requestCPU
AverageValueRaw absolute per podMemory, custom metrics
ValueTotal raw across all podsRare

Scaling behaviour

spec:
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Pods
        value: 4
        periodSeconds: 60
      selectPolicy: Min
    scaleDown:
      stabilizationWindowSeconds: 300   # default
      policies:
      - type: Pods
        value: 1
        periodSeconds: 60

Imperative commands

kubectl autoscale deployment webapp --min=2 --max=10 --cpu-percent=60

# For memory or multiple metrics: generate YAML and edit
kubectl autoscale deployment webapp --min=2 --max=10 --cpu-percent=60 \
  --dry-run=client -o yaml > hpa.yaml

kubectl: inspect and debug

kubectl get hpa
kubectl get hpa webapp-hpa -o wide
kubectl describe hpa webapp-hpa
kubectl get hpa --watch

Reading kubectl get hpa output

NAME         REFERENCE           TARGETS               MINPODS   MAXPODS   REPLICAS
webapp-hpa   Deployment/webapp   45%/60%, 180Mi/200Mi   2         10        4
  • <unknown> → metrics server not running OR CPU requests missing

Troubleshooting

SymptomCauseFix
TARGETS: <unknown>/60%No CPU requests OR metrics-server downAdd resources.requests.cpu
HPA not scaling upminReplicas = maxReplicas, or load too lowVerify load and events
HPA scales down too faststabilizationWindowSeconds too lowAdd spec.behavior.scaleDown
HPA ignores memoryUsed Utilization for memoryChange to AverageValue

Scaling formula

desiredReplicas = ceil(currentReplicas × (currentMetric / desiredMetric))

e.g. 4 replicas at 80% CPU, target 60% → ceil(4 × 80/60) = 6 replicas

Exam gotchas

  • scaleTargetRef must match exact resource name and kind
  • kubectl describe hpa for scaling events: shows WHY it scaled or didn't
  • HPA + manual kubectl scale conflict: HPA overrides manual within a period
  • minReplicas defaults to 1 if not set
  • autoscaling/v2 is current; autoscaling/v2beta2 deprecated