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

Declarative Replicas

mediumDeployments & ReplicaSetsOwner References & Self-Healing—No data yet

Problem statement

The content fleet in namespace fleet was built one manifest at a time: five pods, five kubectl invocations, no record anywhere of how many there are supposed to be. This morning there are four. content-node-4 died in the night and stayed dead, because nothing in the cluster was ever told that five is the number. Declare the fleet instead, and let the cluster hold it there.

About this exercise

A Deployment does not run pods. It records an intent - a replica count and a pod template - and hands that intent to a ReplicaSet, which it creates for you. The ReplicaSet is the part that counts: it compares the pods matching its selector against the number it was given, and creates or deletes pods until the two agree. That loop never stops, which is why a pod that disappears at three in the morning is replaced at three in the morning.

The chain is not a naming convention, it is written into the objects. Every pod the ReplicaSet creates carries an owner reference back to that ReplicaSet, and the ReplicaSet carries one back to the Deployment. Ownership is what tells a controller which pods are its responsibility, and it is why deleting the Deployment takes the whole chain with it while deleting a pod takes only the pod - briefly.

Two label sets do different jobs, and confusing them is the classic mistake. The pod template's labels are stamped onto every pod the ReplicaSet creates, all of them, including ones nothing selects on. The selector is the separate question of which pods the ReplicaSet counts as its own. The controller also adds a label of its own to the template it stores, so that pods belonging to different revisions of the same Deployment can be told apart.

kubectl explain deployment.spec and kubectl explain replicaset.spec are the reference for the fields.

How this project is graded

The grader reads the live cluster: the Deployment's status, the ReplicaSet it generated, and the owner reference on every pod. It compares your saved answers against the names the cluster actually assigned, and it looks for evidence in the fleet itself that a pod really was removed and really was replaced - a fleet that was never disturbed does not pass that one. The namespace is re-seeded before each run, so the hand-built pods come back if you remove them.

What you will build

Everything lives in namespace fleet.

1. Deployment content-fleet:

replicas      5
selector      app=content, tier=fleet
pod labels    app=content, tier=fleet, role=edge
container     name node, image nginx:alpine

The selector matches on app and tier only. role=edge is carried by the template and by nothing else.

2. Two answers in answers/:

replicaset.txt   the name of the ReplicaSet the Deployment generated, exactly that name
healed.txt       the name of the fleet pod you deleted, exactly that name

The four hand-built pods content-node-1, -2, -3 and -5 stay where they are. They are the control group: nothing manages them, and nothing will replace them.

Step 1 - declare the fleet

Create the Deployment and let it settle. When it is done, the Deployment's own status - not your manifest - has to report five replicas, five ready and five available.

deployment_reports_the_declared_replicas_ready_and_available should pass

Step 2 - read the chain it built

You created one object and now there are seven. Find the ReplicaSet the Deployment generated, record its name, and confirm the ownership chain runs Deployment to ReplicaSet to pod.

generated_replicaset_is_owned_by_the_deployment and every_pod_is_owned_by_the_generated_replicaset should pass

Step 3 - what the template stamped

Every pod the ReplicaSet made carries the template's labels, role=edge included, plus the one the controller added. The hand-built pods carry none of that.

template_labels_flow_to_the_created_pods should pass

Step 4 - kill one

Delete a fleet pod, record which one, and watch what the cluster does about it. Do the same to a content-node- pod if you want the contrast.

deleted_pod_is_replaced_and_the_count_restored should pass

When every test passes

You have:

  • A fleet whose size is a declared fact in the cluster instead of five remembered commands
  • The ReplicaSet the Deployment generated, and the owner references that make it responsible for those pods
  • Template labels landing on every pod, distinct from the selector that decides which pods are counted
  • A pod deleted at will and replaced without anybody noticing

You do not have yet:

  • Any way to change that number. Growing and shrinking a running Deployment, and reading the status while it happens, is the next exercise.

Verify

Click Test after every step.

More in Deployments & ReplicaSets

See the module
ScalingGrow 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 yetmediumSelector-Template ContractProve the selector-template contract on a live Deployment: a mismatched selector is refused at admission, and a pod labelled out of the selector is released, replaced and left running unmanaged.—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.