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
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: app
image: nginx:1.25
kubectl get rs
kubectl scale rs frontend --replicas=5
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
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
Rollout commands
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
Exam gotchas
rollout history only shows change-cause if annotated; without it shows <none>
References