Selectors and output formats
Filter the inventory, not the workload
A selector is a condition used to choose objects. Labels are key/value metadata attached to objects. A label selector filters by those keys and values; it does not rename, move or change the objects. The fixture has three Pods in book-lab, and a fourth catalog Pod in other-lab. The namespace limits the list before you reason about its contents. All outputs below are derived from synthetic JSON, not actual kubectl execution. Optional commands require the same approved lab setup as the previous lessons.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l app=catalogExpected selected names: catalog-a and catalog-b. The worker does not match; catalog-other is outside the chosen namespace.
Combine requirements and inspect labels
A comma joins requirements using AND. app=catalog,tier=web selects objects meeting both. Set-based selectors such as tier in (web,batch) accept one of the listed values; quote the expression in a POSIX-style shell so parentheses and spaces remain one argument. Use --show-labels to inspect metadata before designing selectors. An inequality requirement can also match objects missing that key, so do not assume app!=catalog means only a known worker group. Exact label/selector ownership design belongs in the next chapter.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods --show-labelskubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l 'app=catalog,tier in (web,batch)'The second query selects catalog-a and catalog-b. It does not require the tier to be both web and batch; it requires membership in the set and app=catalog.
Filter by an API field
Field selectors are not arbitrary JSONPath expressions. The API supports a defined subset of fields for each resource; Pods support status.phase. Use --field-selector status.phase=Running to ask the server for objects whose phase is Running. This includes a Pod that is not ready. An unsupported field selector can produce a server error; do not invent one for every nested field.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l app=catalog --field-selector status.phase=RunningBoth catalog Pods match. Only catalog-a has Ready=True in this snapshot. To reason about readiness, examine conditions or use a bounded wait for a particular Pod.
Choose output for your question
Default tables are designed for people; -o wide adds information such as node and IP for Pods. -o yaml and -o json expose object structure, while -o name shows resource identifiers. Full objects can contain sensitive configuration or operational metadata, so keep them inside the lab. For automation, select structured fields instead of splitting table columns that can change. List output has an items array; a single object has fields at the top level.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pod/catalog-b -o jsonpath='{.status.phase}'kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l app=catalog -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'The first expected value is Running. The second is two names on separate lines. Do not assume order unless you explicitly sort; the fixture contains catalog-a then catalog-b. JSONPath here is kubectl’s template dialect, not a label selector.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pod/catalog-b -o yamlkubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l app=catalog -o jsonkubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get pods -l app=catalog -o nameThe single-object YAML has metadata and status at the root. The JSON list contains matching objects under items; name output uses identifiers like pod/catalog-a.
Common mistakes and next step
Do not add all-namespaces to fix a query without authorization. A successful empty result can mean that no labels match, not that a cluster is empty. Do not confuse an API field filter with output projection: JSONPath chooses what to print from a returned object; a selector chooses which objects to retrieve. Practice with pods.json and other-namespace-pod.json. Predict results, then explain why Running is not Ready. Next: namespaces, labels, selectors and ownership.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Selectors and output formats — knowledge check
Apply the lesson concepts to original offline scenarios.
Which names match app=catalog in book-lab?
What does a comma between label requirements mean?
Which fixture Pods match status.phase=Running?
What does JSONPath output do in kubectl get?
Which output is most suitable for structured machine processing of a Pod list?