$ cat ckad-prep/part-03.md
CKAD Prep, Part 3: A ReplicaSet Only Counts

Where we are
Hi, I’m Reza. I’m sharing my preparation for the CKAD exam, practiced on a K3s cluster in my home lab. I am a junior engineer looking for my first SRE, DevOps or Cloud job, and that is why I learn in public. You can see how I think, what breaks, and how I fix it.
In Parts 1 and 2, I worked with bare pods. They’re disposable: when a pod dies, nothing brings it back. This part is the first controller that does: the ReplicaSet. It keeps a number of pods running.
Four scenarios, all on one ReplicaSet called rs-broken:
- B1: fix a ReplicaSet that the API server rejects.
- B2: scale it two ways, and see which pods get deleted.
- B3: create a stray pod, and watch the ReplicaSet take it over.
- B4: change the image, and see that nothing happens to the running pods.
All four make sense once you see what a ReplicaSet actually does. It’s one loop:

It’s the spec vs status idea from Part 1, automated: replicas is the desired state, and the number of matching pods is the actual state.
B1: fix a broken ReplicaSet
My first file was wrong:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-broken
namespace: core
Two things were wrong:
- The version was
apps/v2, and v2 does not exist for a ReplicaSet. - The namespace was
backend, and there is no namespace called backend. Onlycoreexists.
The API server rejected it with:
error: error validating "04-rs-broken.yaml": error validating data: ValidationError(ReplicaSet.spec.template.spec.containers[0]): unknown field "imageURL" in io.k8s.api.core.v1.Container; if you choose to ignore these errors, turn validation off with --validate=false
This told me three things:
- The API server checked my YAML against the schema for the version I asked for.
- The field name had to be
image, notimageURL. - It was a schema error, not a runtime error. Nothing was created and nothing was running.
I made these fixes:
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api # was: app: backend
spec:
containers:
- name: c
image: nginx:1.27
The template has to match the selector, and the containers have to match the image my cluster can pull.
Two valid fixes, but only one fits
I also tried two quick fixes:
- Change
apps/v2toapps/v1, keep everything else. The API server would accept it. - Change
apps/v2toapps/v1beta2. I tried this in my head and discarded it — v1beta2 was removed long ago, so the API server would reject it again.
And since I wanted replicas 3 in the core namespace, I also wrote the label correctly the first time.
I also wrote the file name correctly the first time: 04-rs-broken.yaml.
Result
I applied it:
NAME DESIRED CURRENT READY AGE
rs-broken 3 3 3 10s
NAME READY STATUS RESTARTS AGE LABELS
rs-broken-blzb2 1/1 Running 0 30s app=api
rs-broken-jq4vt 1/1 Running 0 30s app=api
rs-broken-lqqp2 1/1 Running 0 30s app=api
That’s 3 replicas, 3 ready, and the pods all carry the label app=api. The fix was a schema fix, not a runtime fix.
B2: scale two ways
The task: scale rs-broken up to 5, then down to 2, in two different ways, and watch which pods get deleted.
Up to 5, imperatively
Up is easy, and the command is an exam favorite:
kubectl scale rs rs-broken --replicas=5
Nothing in the file changed. The ReplicaSet controller saw a new desired count and created pods:
rs-broken-blzb2was already running.rs-broken-jq4vtwas already running.rs-broken-lqqp2was already running.rs-broken-xczv6andrs-broken-m9w4dwere created.
So the count went 3 to 5, but the file still says 3. If I applied the file again, the count would drift back to 3. That is drift, and it is the reason imperative commands are fine for practice, but dangerous in a real cluster.
Down to 2, declaratively
I edited the file:
spec:
replicas: 2 # was 3
and applied it:
kubectl apply -f 04-rs-broken.yaml
This time the file and the cluster agree. There is no drift, because the desired state lives in the file, and the file is the source of truth.
I checked:
grep replicas 04-rs-broken.yaml
kubectl get rs rs-broken -o jsonpath='{.spec.replicas}{"\n"}'
Both printed 2. The ReplicaSet controller then deleted pods to get from 5 to 2.
The lesson for the exam: if the question says “edit the file and apply it”, use the declarative way. If it says “scale it now”, use kubectl scale. Both work, but they leave the cluster in different states.
Which pods get deleted?
When a ReplicaSet has to delete pods, it deletes the cheapest first. The cheapest pods are the ones that are:
- Not ready.
- Have been running the shortest time.
- Have the lowest priority.
- Have the fewest restarts.
- Have the fewest resources requested.
What happened to my pods? All five were ready, running for about the same time, same priority, no restarts, and the same resources. So the tie-breaker was the pod name.
rs-broken-xczv6andrs-broken-m9w4dwere created last, and they were deleted first.rs-broken-blzb2,rs-broken-jq4vtandrs-broken-lqqp2were created first, and they survived.- The ReplicaSet kept the three oldest pods.
My prediction was wrong
I predicted the ReplicaSet would delete the two oldest pods, because I thought it would keep the newest ones. I was wrong. It kept the oldest pods and deleted the newest ones.
NAME DESIRED CURRENT READY AGE
rs-broken 2 2 2 25m
B3: the pod that disappeared
The task: create a pod that matches the selector, and watch the ReplicaSet take it over.
apiVersion: v1
kind: Pod
metadata:
name: stray
labels:
app: api
spec:
containers:
- name: c
image: nginx:1.27
This pod was not created by the ReplicaSet, but it has the same label the ReplicaSet selects on. So the ReplicaSet counts it.
- The ReplicaSet saw 3 pods for a desired count of 2.
- It deleted one pod to get back to 2.
- The pod it deleted was
stray, the one it did not create. - It deleted
straybecausestraywas the newest pod, and the cheapest to delete.
I applied the file, and kubectl said:
kubectl apply -f 05-stray-pod.yaml
pod/stray created
Watching it happen
I asked kubectl events what had happened:
kubectl events --for rs/rs-broken | tail -3
The output was:
7m27s Normal SuccessfulDelete replicaset/rs-broken Deleted pod: stray
7m28s Normal SuccessfulCreate replicaset/rs-broken Created pod: rs-broken-xczv6
7m29s Normal SuccessfulDelete replicaset/rs-broken Deleted pod: rs-broken-m9w4d
What happened, step by step:
- I created
stray, and the ReplicaSet counted 3 pods. - It deleted
strayto get back to 2. - Then I scaled the ReplicaSet back up to 5, and it created
rs-broken-xczv6. - Then I scaled it back down to 2, and it deleted
rs-broken-m9w4d.
I predicted it would adopt stray and keep it. It did the opposite: it deleted stray, because stray was the newest pod, and the cheapest to delete.
Writing the answer
I wrote the count and the reason to a file:
kubectl get pods -l app=api --no-headers | wc -l > /opt/ckad/00/adopt.txt
echo "The ReplicaSet adopted stray because ..." >> /opt/ckad/00/adopt.txt
The file now says:
2
The ReplicaSet adopted stray because it matches selector app=api, saw 3 pods for replicas 2, and deleted one to get back to 2.
My first try was wrong, because I thought the ReplicaSet would adopt stray and keep it. The answer file says the opposite, and the events prove it.
Why this matters in real clusters
This is the same idea as a Deployment that adopts a pod it did not create. If a pod has the right label, the controller counts it. That is why labels are not decoration: they are how controllers find their pods. And that is why a stray pod in the wrong namespace, or with the wrong label, can be adopted, counted, or deleted by a controller you did not expect.
B4: an image change that changes nothing
The task: change the image in the template from nginx:1.27 to nginx:1.26, and see what happens to the running pods.
template:
...
spec:
containers:
- name: c
image: nginx:1.26 # was nginx:1.27
I applied it:
kubectl apply -f 04-rs-broken.yaml
The output was “replicaset.apps/rs-broken configured”. The template now says 1.26. But the running pods still say 1.27.
Template vs running pods
The ReplicaSet says 1.26:
kubectl get rs rs-broken -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
There are two specs in a ReplicaSet: the spec of the ReplicaSet, and the spec of the pods it creates. The template is the spec of the pods it will create. The running pods are the pods it already created.
The pods say 1.27:
kubectl get pods -l app=api -o custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[0].image
The output was:
NAME IMAGE
rs-broken-blzb2 nginx:1.27
rs-broken-jq4vt nginx:1.27
This is confusing at first. The template says 1.26, and the pods say 1.27. But the ReplicaSet only uses its template when it creates new pods. It never updates existing pods. So the running pods keep their old image, and the new image only appears when a new pod is created.
I wrote the answer to a file:
echo "Running pods use nginx:1.27. A ReplicaSet only uses its template when it creates new pods; it never updates existing ones." > /opt/ckad/00/rs-image.txt
Forcing the new image
If I want the new image on the running pods, I have to delete them. The ReplicaSet will create new pods from the template, and the new pods will use nginx:1.26.
kubectl delete pod -l app=api
The ReplicaSet created two new pods, and they use nginx:1.26. The old pods are gone, and the new pods are the ones the template describes.
- Deleting a pod is not the same as updating it. The ReplicaSet creates a new pod, it does not change the old one.
- The new pod is created from the template, so it uses the new image.
- The old pod is gone, and its image is gone with it.
I also typed delete pod -l app=api once, and the ReplicaSet created two new pods. The pods were deleted, and the ReplicaSet created new ones.
This is why Deployments exist
A ReplicaSet only fixes the count. It never updates a running pod. That is why Deployments exist: a Deployment owns a ReplicaSet, and when you change the image, the Deployment creates a new ReplicaSet, and the new ReplicaSet creates new pods.
- Change the image in the Deployment.
- The Deployment creates a new ReplicaSet.
- The new ReplicaSet creates new pods.
So, side by side:
- A ReplicaSet fixes the count.
- A Deployment fixes the count and the image.
- A ReplicaSet never updates a running pod.
- A Deployment never updates a running pod either. It creates new pods, and deletes the old ones.
What I learned
- The API server validates my YAML against the schema for the version I asked for. If the version does not exist, or the field name is wrong, it rejects the file before anything is created.
- A ReplicaSet counts pods by label, not by name. If a pod has the right label, it is counted, even if the ReplicaSet did not create it.
- A ReplicaSet only fixes the count. It never updates a running pod, and it never changes a pod’s image.
kubectl scaleis imperative, and it leaves the file and the cluster out of sync. Editing the file and applying it is declarative, and it keeps them in sync.- When a ReplicaSet has to delete pods, it deletes the cheapest first: not ready, shortest running time, lowest priority, fewest restarts, fewest resources requested.
- A stray pod with the right label is counted, and the newest pod is the cheapest to delete.
- The template is the spec of the pods the ReplicaSet will create. The running pods are the pods it already created.
- A Deployment owns a ReplicaSet, and when you change the image, the Deployment creates a new ReplicaSet, and the new ReplicaSet creates new pods.
- I predicted the ReplicaSet would delete the oldest pods, and adopt
stray. I was wrong on both counts. The ReplicaSet kept the oldest pods, and deletedstray.
What is next
- Part 4 will cover Deployments.
- The Job Board API series will continue after the CKAD series.