Topic 02: ReplicaSets Link to heading
A ReplicaSet ensures a specified number of pod replicas are running at all times. If a pod dies, the ReplicaSet creates a replacement. In practice you rarely create ReplicaSets directly — Deployments manage them for you. But you need to know the structure because Deployments use the same pattern.
YAML Link to heading
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-rs
spec:
replicas: 3
selector:
matchLabels:
app: web # which pods this RS manages
template:
metadata:
labels:
app: web # must match selector — RS stamps these on pods it creates
spec:
containers:
- name: web
image: nginx
The Three Label Locations Link to heading
ReplicaSets have labels in three distinct places:
metadata:
labels:
app: web # labels ON the RS object itself — for filtering the RS
spec:
selector:
matchLabels:
app: web # the query — "find pods with these labels"
template:
metadata:
labels:
app: web # labels stamped on pods — must match selector
metadata.labels— cosmetic, used to filter the RS object itselfspec.selector.matchLabels— the query the RS uses to find its podsspec.template.metadata.labels— what gets stamped on pods at creation
If selector and template labels don’t match, the API rejects the resource at creation.
Imperative Link to heading
# No direct imperative for ReplicaSet — generate YAML
k create deployment web --image=nginx $do # then change kind to ReplicaSet
# Scale
k scale rs web-rs --replicas=5
# Delete RS but keep pods
k delete rs web-rs --cascade=orphan
k get rs
k describe rs web-rs
Common Mistakes Link to heading
selector.matchLabelsandtemplate.metadata.labelsmust match exactly — mismatch = API rejection- ReplicaSet will adopt any existing pod in the namespace that matches its selector — even pods it didn’t create
Exam Tips Link to heading
- In practice: always use Deployments, not ReplicaSets directly
k get rs— check ReplicaSet status after a deployment rollout- ReplicaSet controller does NOT retry after
FailedCreatefor a missing ServiceAccount — must delete and recreate the Deployment