Sub topic 12
Pulling out status fields with jsonpath and custom-columns
This chapter assumes you can run kubectl get and kubectl describe, read YAML and JSON, and have a cluster available (kind or minikube is fine). You should also know that a resource's status holds its observed state.
kubectl describe is good for reading one object. It is hard to script or compare across many objects. When you want one field from one Pod, or one column across fifty Pods, use -o jsonpath or -o custom-columns. Both read the same status fields you have already seen in -o yaml.
Choosing an output format
jsonpath for single values and lists
A jsonpath expression is wrapped in braces and starts from the object root. To print each Pod condition as type=status, loop with range:
kubectl get pod web-0 -o jsonpath='{range .status.conditions[*]}{.type}={.status}{"\n"}{end}'Inside range, paths such as .type are relative to each element. The {"\n"} inserts a newline, because jsonpath does not add one for you.
To find out why the previous container instance died, read lastState:
kubectl get pod web-0 -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'This prints a value such as OOMKilled or Error. The [0] selects the first container, so use the right index for Pods with sidecars.
The same approach works for Deployments. This command prints the condition types on one line, separated by spaces:
kubectl get deployment web -o jsonpath='{.status.conditions[*].type}'When you query a list, such as kubectl get pods, the objects sit under .items, so you write {.items[*].metadata.name}.
custom-columns for tables
custom-columns takes comma-separated HEADER:path pairs and prints a table. Paths have no surrounding braces and no leading {., but they start with a dot.
This command prints each node's name and the status of its Ready condition:
kubectl get nodes -o custom-columns='NAME:.metadata.name,READY:.status.conditions[?(@.type=="Ready")].status'The [?(@.type=="Ready")] part is a filter. It selects the condition whose type is Ready, because the conditions array has no fixed order. Do not rely on an index such as conditions[2].
You can do the same for Pods:
kubectl get pods -o custom-columns='NAME:.metadata.name,PHASE:.status.phase,RESTARTS:.status.containerStatuses[0].restartCount'Common errors
| Symptom | Likely cause |
|---|---|
| Empty output, no error | The field does not exist. For example, `lastState.terminated` is absent until a container has terminated. |
| `<none>` in a custom column | The path matched nothing for that object. |
| Quoting or `unexpected token` errors | The shell consumed your quotes or `$`. Wrap the whole expression in single quotes and use double quotes inside. |
| Only the first value appears in a column | `custom-columns` does not expand multi-valued paths into several rows. Use jsonpath with `range` instead. |
When to use which
Use describe and kubectl events when a person is reading one object. Use custom-columns for a quick comparison across many objects. Use jsonpath when a script needs one value, such as the Ready status or the last termination reason. Neither format shows events, because events are separate objects. Query them with kubectl get events.
Exercise
Write a command that lists every Pod in the current namespace with its name and the reason its first container last terminated. What will you see for a Pod that has never restarted?
Show answer
kubectl get pods -o custom-columns='NAME:.metadata.name,LAST:.status.containerStatuses[0].lastState.terminated.reason'. For a Pod whose container has never terminated, the LAST column shows <none>, because the path matches nothing.