StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 3 · Labels, Selectors & Annotations

Labeling Scheme

easyObject LabelsMetadata Management—No data yet

Problem statement

Two hundred pods, zero conventions. A release cannot find the pods it just shipped, the dashboards cannot group anything, and the cleanup script deletes by a hand-written list of names that is wrong within a day. The namespace fleet is a small slice of that mess: four workloads, each labeled a different way. Bring them to one scheme.

About this exercise

A label is a key/value pair in an object's metadata. Nothing in Kubernetes reads a label by itself - labels exist so that other things can find the object by them: selectors, services, dashboards, cleanup jobs. A fleet with no agreed keys is a fleet nothing can query, which is why the scripts fall back on name lists.

Labels are metadata, not spec, so they can be changed on a live object without recreating it. Three operations matter and they are deliberately different:

  • Setting a key that is not there yet succeeds quietly.
  • Setting a key that already has a value is refused unless the command is told that overwriting is intended. That refusal is the guard rail: it stops a bulk relabel from silently rewriting a value somebody else depends on.
  • Removing a key is its own operation, not "setting it to empty". An empty value is still a value, and a selector for the key would still match it.

Labels can also be set at creation time, which is the only way a new object is queryable the moment it exists rather than at some later relabel.

kubectl label --help and kubectl run --help are the reference.

The scheme every object in fleet must carry - exactly these three keys, no others:

app    the service's own name        checkout, ledger, search, invoice, receipts
tier   the role it plays             one of: web, worker, batch
env    where it runs                 one of: prod, staging, dev

squad is a retired key from the old convention. Nothing reads it any more and it must not survive on any object.

How this project is graded

The grader reads the labels off the live objects in fleet and compares them against the scheme, key by key and value by value. It checks that the objects you were not asked to touch still carry exactly what they started with, and that your readback file agrees with the cluster.

What you will build

One new pod, three corrections to live objects, and one readback file:

receipts-web            a new pod in fleet, image nginx:alpine, labeled at creation
search-indexer          env is missing; tier says batch but it is a worker
ledger-worker           still carries the retired squad key

answers/labels.txt one line per pod in fleet, pods sorted by name: <pod-name> <key>=<value>,<key>=<value>,... keys within a line sorted alphabetically, no other text

The intended labels:

receipts-web      app=receipts   tier=web     env=prod
search-indexer    app=search     tier=worker  env=staging

Step 1 - labeled at birth

Create receipts-web in fleet from image nginx:alpine, carrying exactly the three scheme labels and nothing else. A pod created without labels and relabeled afterwards was unfindable in between - and a creation shortcut that invents its own label leaves a fourth key behind.

created_pod_carries_exactly_the_scheme_labels should pass

Step 2 - correcting live objects

search-indexer is missing env and its tier is wrong. Add the one that is absent and correct the one that is already set - the second is the case the command refuses by default.

relabel_adds_the_missing_scheme_key and overwrite_replaces_an_existing_value should pass

Step 3 - retiring a key

Take squad off ledger-worker. Its other three labels must come through unchanged, and invoice-batch - which you were not asked to touch - must be exactly as it was.

deprecated_key_removed_and_nothing_else_changed should pass

Step 4 - the fleet reads back clean

Every pod in fleet now carries exactly app, tier and env with values from the scheme. Record what the cluster reports into answers/labels.txt, in the format above.

every_object_matches_the_scheme and label_readback_matches_the_cluster should pass

When every test passes

You have:

  • One label scheme carried by every object in the namespace, applied at creation and by correcting live objects
  • A retired key removed without collateral damage to the labels beside it
  • A readback that proves the fleet's labels from the cluster rather than from a spreadsheet

You do not have yet:

  • Anything that uses the scheme. Labels only pay off when something queries them, and selectors are the next exercise.

Verify

Click Test after every step.

More in Labels, Selectors & Annotations

See the module
Selector QueriesAnswer seven operational questions about a labeled namespace with equality and set-based selectors - in, notin, exists - including the empty result and the objects missing the key.—No data yeteasyAnnotations for MachineryAttach deploy metadata - a ticket, an owner, a build JSON and an over-length summary - to the object it describes, update one key, retire another, and prove selectors never see any of it.—No data yeteasyFleet BookkeepingBring an unmanaged namespace under one label scheme, attach the machine-readable deploy record, and answer every fleet operation by selector - proven by an object the grader adds behind your back.—No data yeteasy

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.