StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 8 · Deployments & ReplicaSets

Selector-Template Contract

mediumSelectors & Pod TemplatesController Ownership & Orphaning—No data yet

Problem statement

Somebody tidied the labels on a Deployment last month. It stopped managing its own pods, sat there reporting zero, and started reaching for the batch workload running next to it - which is when the incident began. Namespace label-contract has that neighbour in it: report-worker, two pods, minding its own business. Build the search tier beside it and find out exactly what a selector is allowed to do.

About this exercise

A Deployment carries two things that both mention labels, and they answer different questions:

  • The pod template's labels are stamped onto every pod the ReplicaSet creates.
  • The selector decides which pods the controller considers its own - now and forever, not just at creation time.

Between them there is a contract the API server enforces: the selector must match the template's labels. A Deployment whose selector asks for labels its own template does not carry would create pods it immediately disowns, and then create more, forever. So it is refused at admission - the object is never stored, and the error names the field. The reverse is fine: a template may carry labels the selector never mentions.

Because the selector is evaluated continuously, membership is not decided once. A pod's labels are mutable, and a running pod whose labels are edited out of the selector is released: the controller removes its own owner reference and stops counting it. Nothing else happens to that pod. It keeps running, keeps serving, keeps its address - it simply belongs to nobody. Meanwhile its former owner sees itself one pod short and creates a replacement, so the namespace quietly ends up with one more pod than anybody declared. The orphan will not be scaled, will not be upgraded, and will not be replaced when it dies.

The selector is also why the tidy-up incident was possible at all. A selector points at labels, not at objects, so a selector that names somebody else's labels is pointing at somebody else's pods.

kubectl explain deployment.spec.selector and kubectl explain deployment.spec.template.metadata are the reference.

How this project is graded

The grader reads owner references from the live pods, re-runs your rejected manifest against the API server itself in a way that stores nothing, and checks the neighbouring workload still has its own pods. Your saved answers are compared against the cluster. The namespace is re-seeded before each run, so the report worker comes back if you remove it.

What you will build

Everything lives in namespace label-contract.

1. Deployment search-api, described in manifest.yaml:

replicas      3
selector      app=search, tier=api
pod labels    app=search, tier=api, role=frontend
container     name api, image nginx:alpine

2. broken.yaml - a Deployment named search-api-broken that must never end up in the cluster:

replicas      2
selector      app=report, tier=batch      the report worker's labels
pod labels    app=search, tier=api
container     name api, image nginx:alpine

3. Two answers in answers/:

rejection.txt   what the API server said when broken.yaml was applied
orphan.txt      the name of the search-api pod you relabelled, exactly that name

4. One search-api pod has its tier label changed to quarantine and is left running.

Do not touch report-worker or its pods.

Step 1 - the tier

Create search-api and let it settle at three ready replicas. Note that the selector names two of the template's three labels; the third is along for the ride.

deployment_reaches_the_declared_size_with_matching_pods should pass

Step 2 - the selector that reaches next door

Write broken.yaml as specified and apply it. It will not be created - keep exactly what came back, and leave the cluster without it.

mismatched_selector_is_rejected_at_admission should pass

Step 3 - labelling a pod out of its own tier

Pick one of the three search-api pods, record its name, and change its tier label to quarantine. Then look at the namespace: how many pods there are, which ones the ReplicaSet still counts, and what happened to the owner reference on the one you edited.

relabelled_pod_is_orphaned_and_replaced and orphan_keeps_running_without_a_controller should pass

Step 4 - who owns what, afterwards

The ReplicaSet's own readback has to agree with its selector: three pods owned, the orphan not among them, and the neighbour untouched throughout.

replicaset_owns_exactly_the_pods_its_selector_matches and neighbour_pods_stay_with_their_own_replicaset should pass

When every test passes

You have:

  • A selector refused at admission because it did not cover its own template, before it could do any damage
  • A pod released by its controller in front of you, and a replacement built without anyone asking
  • An orphan still running, still serving, and managed by nothing
  • Ownership read back from the objects rather than assumed from names

You do not have yet:

  • All of it under one roof. A web tier that holds its declared size, heals, scales on command and never mismanages a pod - with the whole episode auditable - is the next exercise.

Verify

Click Test after every step.

More in Deployments & ReplicaSets

See the module
Declarative ReplicasReplace a hand-built pod fleet with a Deployment, read the ReplicaSet and owner references it generates, and prove the count heals itself when a pod is deleted.—No data yetmediumScalingGrow and shrink a running Deployment in place and then from its manifest, tracking desired, ready and available counts through every step on one ReplicaSet.—No data yetmediumSelf-Healing Web TierBuild a web tier Deployment and run the chaos drill on it - healing after kills, scaling both ways, a refused selector and a deliberate orphan - with an ownership and status audit proving every phase.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.