Labels, selectors and annotations
Choose the objects you mean
Labels are short key/value strings used to group API objects. A selector is a condition evaluated against those labels. Labels need not uniquely identify a Pod: several replicas should often share an application label. Annotations store descriptive or tool-specific metadata and do not participate in ordinary label selection. All examples are offline illustrations, not observed cluster output. Optional read-only commands require an explicitly approved disposable cluster, a separately prepared ./lab.kubeconfig, a verified bytemind-lab context and matching fixture inventory. No cluster or kubeconfig is included. Do not query an unknown context.
metadata: name: catalog-a namespace: book-lab labels: app: catalog track: stable annotations: bytemind.example/note: "Reviewed training fixture"The app and track labels can be queried with -l; the note annotation cannot. Both maps contain string values. Quoting values such as "true" or "42" avoids YAML converting them to non-string scalars.
Read a small label inventory
The book-lab inventory has four fictional Pods: catalog-a (app=catalog, track=stable), catalog-b (app=catalog, track=canary), catalog-c (app=catalog, no track), and worker-a (app=worker, track=stable). A fifth Pod named catalog-a in stage-lab carries the same labels as the first. It is not in the namespace selected by the following commands.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l 'app=catalog'kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l 'app=catalog,track=stable'The first query matches catalog-a, catalog-b and catalog-c in book-lab. The second matches catalog-a only. Commas combine requirements with AND, not OR. The == operator is equivalent to =. Results describe the fixture, not a live response.
Understand missing keys
An inequality filter matches a different value and also an absent key. Likewise, notin accepts objects without the named key. If the key must exist, add an existence requirement explicitly. Use shell quotes around selector expressions to preserve punctuation and spaces.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l 'app=catalog,track!=stable'kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l 'app=catalog,track,track notin (stable)'kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l 'app=catalog,!track'These select catalog-b plus catalog-c; then catalog-b alone; then catalog-c alone. A positive in filter such as track in (stable,canary) accepts either listed value on the same key. Standard label requirements cannot express arbitrary OR across different keys.
Selectors inside resource specifications
Some objects express selectors through fields rather than CLI text. A typical Service selector is a simple equality map. A Deployment selector supports matchLabels and matchExpressions, must match its Pod template labels, and is immutable in apps/v1 after creation. Its controller uses the selector as part of workload management. A Service uses its selector to find backend Pods; matching alone does not establish backend readiness. These are previews for the next chapters.
spec: selector: matchLabels: app: catalog matchExpressions: - key: track operator: In values: [stable, canary]The requirements are combined: app must be catalog and track must be one of the two allowed values. Do not assume empty or omitted selectors have the same meaning for every API field. Avoid overlapping controller selectors and review candidate matches before changes.
Design a consistent labeling scheme
Each object can have only one value per label key. The key name segment and non-empty values are limited to 63 characters, with alphanumerics at the ends and letters, digits, dot, underscore or hyphen in between. An optional DNS subdomain prefix (up to 253 characters) separates organizational keys with a slash. An empty value is permitted. Prefixes kubernetes.io and k8s.io are reserved for core components. Common app.kubernetes.io/name and app.kubernetes.io/instance labels help identify apps and installations. Annotations can hold longer string descriptions; never put credentials in either map. Avoid putting a volatile release hash in a long-lived controller selector unless that is an intentional design.
Practice: predict selection before changes
Use labeled-pods.json and selector-cases.json. Predict each list of names before comparing with the answer data. Explain why an annotation named track cannot make catalog-c match track=stable. Preview matches before any authorized label changes; editing labels can redirect traffic or change which objects a controller sees. Next, distinguish selection from ownership.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Labels, selectors and annotations — knowledge check
Apply the lesson concepts to original offline scenarios.
Which field is used by an ordinary label selector?
Which book-lab Pods match app=catalog,track=stable?
Which catalog Pods match track!=stable in the book-lab fixture?
How can you exclude missing track labels as well as stable values?
What should a Deployment selector match?