Architecture

Deployment → ReplicaSet → Pods
  • Template change → new ReplicaSet, gradual migration
  • Rarely create ReplicaSets directly

ReplicaSet (managed by Deployment)

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend          # immutable: must match template labels
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: app
        image: nginx:1.25
kubectl get rs
kubectl scale rs frontend --replicas=5  # prefer scaling Deployment instead
kubectl describe rs frontend
  • ReplicaSet ensures N running Pods; no rolling update logic
  • Changing Pod template on a ReplicaSet does not roll out gradually
  • spec.selector immutable

Full Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
  annotations:
    kubernetes.io/change-cause: "v2 - added caching"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0      # 0 = zero-downtime
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: webapp:2.0

Rules

  • spec.selector is immutable
  • Selector must match template labels

Strategy comparison

StrategyBehaviourDowntime?
RollingUpdateReplace pods graduallyNo (with maxUnavailable=0)
RecreateKill ALL old pods, then create newYes

Rollout commands

TaskCommand
Createkubectl create deployment webapp --image=nginx:1.25 --replicas=3
Scalekubectl scale deployment webapp --replicas=5
Update imagekubectl set image deployment/webapp webapp=nginx:1.26
Statuskubectl rollout status deployment/webapp
Historykubectl rollout history deployment/webapp
Revision detailkubectl rollout history deployment/webapp --revision=2
Rollbackkubectl rollout undo deployment/webapp
Rollback to Nkubectl rollout undo deployment/webapp --to-revision=1
Pausekubectl rollout pause deployment/webapp
Resumekubectl rollout resume deployment/webapp
Force restartkubectl rollout restart deployment/webapp
Record change causekubectl annotate deployment/webapp kubernetes.io/change-cause='v2 rollout'
kubectl create deployment d --image=nginx --replicas=3 --dry-run=client -o yaml > d.yaml
kubectl get deploy,rs,po

Rolling updates

  • Default keeps 10 revisions: spec.revisionHistoryLimit
  • rollout undo restores declared state only: not persistent data
kubectl set image deployment app-cache memcached=memcached:1.6.10
kubectl annotate deployment app-cache kubernetes.io/change-cause="Image updated to 1.6.10"

Auto Scaling

  • Requires CPU requests on Pod template: else TARGETS shows <unknown>
  • kubectl autoscale supports --cpu-percent only; YAML needed for memory or multiple metrics
kubectl autoscale deployment app-cache --min=3 --max=6 --cpu-percent=60
kubectl get hpa
kubectl describe hpa

Troubleshooting

SymptomCause
No podsSelector mismatch (immutable: fix requires new Deployment)
ImagePullBackOffWrong image name/tag
CrashLoopBackOffBad command / args

Exam gotchas

  • rollout history only shows change-cause if annotated; without it shows <none>

References