kubectl get -o

Reference for all kubectl output formats including json, yaml, wide, jsonpath, custom-columns, and go-template.

kubectl get RESOURCE -o FORMAT

Common Flags

FlagShortDescription
-o jsonOutput the full resource as JSON
-o yamlOutput the full resource as YAML
-o wideOutput with additional columns (node name, IP, etc.)
-o nameOutput only the resource name in type/name format
-o jsonpathOutput specific fields using JSONPath expressions
-o custom-columnsOutput custom table columns using JSONPath
-o go-templateOutput using Go template syntax

Examples

Output pods as JSON

kubectl get pods -o json

Output pod names only

kubectl get pods -o name

Output wide format with node info

kubectl get pods -o wide

Extract specific field with jsonpath

kubectl get pods -o jsonpath='{.items[*].metadata.name}'

Custom columns table

kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName

kubectl Output Format Reference

kubectl provides multiple output formats through the -o flag. Choosing the right format depends on whether you need human-readable output, data extraction, or machine-parsable input for scripts.

Format Overview

| Format | Use Case | Example | |--------|----------|---------| | (default) | Quick human-readable summary | kubectl get pods | | -o wide | Additional columns | kubectl get pods -o wide | | -o yaml | Full resource spec | kubectl get pod my-pod -o yaml | | -o json | Programmatic processing | kubectl get pods -o json | | -o name | Resource names for scripting | kubectl get pods -o name | | -o jsonpath | Extract specific fields | kubectl get pods -o jsonpath='{...}' | | -o custom-columns | Custom table layout | kubectl get pods -o custom-columns=... | | -o go-template | Complex formatting | kubectl get pods -o go-template='...' |

JSONPath Expressions

JSONPath is the most powerful extraction tool. The syntax follows the JSONPath specification:

# Get all pod names
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# Get pod names, one per line
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'

# Get pod name and IP as a table
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'

# Get the container image for a specific pod
kubectl get pod my-pod -o jsonpath='{.spec.containers[0].image}'

# Get all container images across all pods
kubectl get pods -o jsonpath='{.items[*].spec.containers[*].image}'

# Filter by condition
kubectl get nodes -o jsonpath='{.items[?(@.status.conditions[?(@.type=="Ready")].status=="True")].metadata.name}'

JSONPath Operators

| Operator | Description | Example | |----------|-------------|---------| | . | Child element | .metadata.name | | [*] | All elements of array | .items[*] | | [0] | First element | .spec.containers[0] | | [?()] | Filter expression | [?(@.type=="Ready")] | | {range} | Iterate over elements | {range .items[*]}...{end} | | {"\n"} | Newline in output | Used inside range | | {"\t"} | Tab in output | Used for column alignment |

Custom Columns

Create custom table views:

# Basic custom columns
kubectl get pods -o custom-columns=\
NAME:.metadata.name,\
STATUS:.status.phase,\
NODE:.spec.nodeName,\
IP:.status.podIP

# From a file
cat > /tmp/columns.txt <<EOF
NAME          STATUS          RESTARTS
.metadata.name  .status.phase  .status.containerStatuses[0].restartCount
EOF
kubectl get pods -o custom-columns-file=/tmp/columns.txt

# Pods with their resource requests
kubectl get pods -o custom-columns=\
NAME:.metadata.name,\
CPU_REQ:.spec.containers[0].resources.requests.cpu,\
MEM_REQ:.spec.containers[0].resources.requests.memory

Go Templates

For complex formatting logic:

# List pods with their status
kubectl get pods -o go-template='{{range .items}}{{.metadata.name}} {{.status.phase}}{{"\n"}}{{end}}'

# Conditional output
kubectl get nodes -o go-template='{{range .items}}{{if eq .spec.unschedulable true}}{{.metadata.name}} is cordoned{{"\n"}}{{end}}{{end}}'

# Format as CSV
kubectl get pods -o go-template='{{range .items}}{{.metadata.name}},{{.status.phase}},{{.spec.nodeName}}{{"\n"}}{{end}}'

The -o name Format

Perfect for piping into other commands:

# Get resource names
kubectl get pods -o name
# pod/my-app-7d4b8c5f9-x2k4j
# pod/my-app-7d4b8c5f9-abc12

# Delete all pods in a failed state
kubectl get pods --field-selector=status.phase=Failed -o name | xargs kubectl delete

# Describe all deployments
kubectl get deployments -o name | xargs kubectl describe

# Count resources
kubectl get pods -o name | wc -l

Combining -o json with jq

For the most flexibility, combine JSON output with jq:

# Get pods with high restart counts
kubectl get pods -o json | jq '.items[] | select(.status.containerStatuses[0].restartCount > 5) | .metadata.name'

# Get pods not in Running state
kubectl get pods -o json | jq -r '.items[] | select(.status.phase != "Running") | "\(.metadata.name): \(.status.phase)"'

# Extract all unique images in use
kubectl get pods -A -o json | jq -r '[.items[].spec.containers[].image] | unique[]'

# Resource usage summary
kubectl get pods -o json | jq -r '.items[] | "\(.metadata.name)\t\(.spec.containers[0].resources.requests.cpu // "none")\t\(.spec.containers[0].resources.requests.memory // "none")"'

The --sort-by Flag

Sort output by any field:

# Sort by creation time
kubectl get pods --sort-by=.metadata.creationTimestamp

# Sort by restart count
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

# Sort by node name
kubectl get pods --sort-by=.spec.nodeName

Practical Cheat Sheet

# Quick pod overview with IPs and nodes
kubectl get pods -o wide

# Extract just the image versions
kubectl get deploy -o jsonpath='{range .items[*]}{.metadata.name}: {.spec.template.spec.containers[0].image}{"\n"}{end}'

# List all services with their cluster IPs
kubectl get svc -o custom-columns=NAME:.metadata.name,TYPE:.spec.type,CLUSTER-IP:.spec.clusterIP,PORT:.spec.ports[0].port

# Export a resource (without cluster metadata)
kubectl get deploy my-app -o yaml | kubectl neat  # requires kubectl-neat plugin

Interview Questions About This Command

What output formats does kubectl support?
json, yaml, wide, name, jsonpath, custom-columns, go-template. Each serves different purposes from human-readable to machine-parsable.
How do you extract a specific field from kubectl output?
Use -o jsonpath='{.field.path}' for simple extraction, or pipe -o json to jq for complex filtering.
What is the difference between -o wide and -o yaml?
-o wide adds a few extra columns to the table view. -o yaml outputs the entire resource specification.

Common Mistakes

  • Using grep on default output instead of jsonpath or custom-columns, which is fragile and breaks with formatting changes.
  • Forgetting to wrap jsonpath expressions in single quotes, causing shell interpretation issues.
  • Not knowing about -o name for scripting, which gives clean type/name output ideal for piping.

Related Commands