Kubernetes · Chapter 4 of 28
Labels and selectors
By the end of this chapter you can put labels on any object and change or remove them, pick exactly the objects you want with equality and set selectors, show labels as columns, keep notes in annotations, and say why labels are what holds every later part of Kubernetes together. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Meet the cluster
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
Jhatpat's cluster will soon run about 40 pods for Lucknow alone: the storefront, the catalog, the cart, orders, payments, notifiers, each in several copies, some of them canaries (a new version tried on a few customers first). Bhola asks "which pods belong to the shop team?"; the on-call engineer asks "which backend pods are not canaries, so I can restart them?"; and at 2 am someone needs the phone number of the person who owns the catalog.
Names cannot answer those questions. storefront-2 says nothing about its team, its tier or whether it is a canary, and you cannot grep your way through 40 names reliably. Chapter 1 showed labels as a column with -L; this chapter makes them yours: you write them, and then the cluster can be asked for "everything with these labels". Every Deployment, Service and NetworkPolicy you meet later finds its pods this way.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Label the six pods by team and tier
Give each of the six pods in ch04 a team label and a tier label.
1. Run kubectl get pods --show-labels to see the labels they have now.
2. Use kubectl label. storefront-1 and storefront-2 get team=shop and tier=frontend.
3. catalog-1 and catalog-2 get team=shop and tier=backend.
4. notifier-1 and notifier-2 get team=growth and tier=backend.
To pass: each of the six pods has exactly these team and tier values. Each pod keeps its app label, and the canaries keep their track label. The Check reads the labels on the pods, not labels.txt.
- Task 2
selector.txt: the backend pods that are not canaries
Write one label selector that picks the backend pods that are not canaries.
1. Do task 1 first.
2. Find a selector for the pods whose tier is backend and whose track is not canary.
3. Try it with kubectl get pods -l in the terminal until it lists the right pods.
4. Write the selector in selector.txt, on the first line below the notes, without quotes.
To pass: the Check runs your selector with kubectl get pods -l in ch04. It must pick every such pod, and no other pod. You also ran kubectl get with -l, and the command worked.
- Task 3
The catalog owner's phone as an annotation
Save the catalog owner's phone number on the catalog pods.
1. Use kubectl annotate on both catalog pods, catalog-1 and catalog-2.
2. The key is jhatpat.in/owner-phone. The value is +91 98390 41275, with its two spaces.
3. The value has spaces, so put quotes around it.
To pass: catalog-1 and catalog-2 both have the annotation jhatpat.in/owner-phone with exactly that value. It must be an annotation, not a label.
- Task 4 · interview question
Interview: a git commit, label or annotation
Ammar will ask: "Should the git commit of a release be a label or an annotation?"
1. Write two or three sentences in your own words in answer.txt, below the notes.
2. Say which one you choose for the commit.
3. Say why: what labels are used for, and what annotations are used for.
To pass: answer.txt chooses an annotation, and says why, for example that labels are for selecting and annotations are information for people and tools.
What you will learn
- Labels: key and value pairs on any object, the key's optional prefix, and the rules for both.
- Changing them: kubectl label, --overwrite, and key- to remove one.
- Selecting: -l app=cart, !=, in (...), notin (...), -l key and -l '!key', and several at once.
- Seeing them: --show-labels and -L.
- Annotations: notes on an object that are never used to select.
- Recommended labels: app.kubernetes.io/name and its friends, and the label as glue.
More in Meet the cluster
Build "Labels and selectors" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.