kubectl get

Display one or many resources. The most frequently used kubectl command for inspecting cluster state.

kubectl get [TYPE] [NAME] [flags]

Common Flags

FlagShortDescription
--output-oOutput format: json, yaml, wide, jsonpath, custom-columns
--selector-lFilter by label selector
--all-namespaces-AList resources across all namespaces
--watch-wWatch for changes after listing
--field-selectorFilter by field values (e.g., status.phase=Running)

Examples

List all pods in the current namespace

kubectl get pods

List pods across all namespaces with extra info

kubectl get pods -A -o wide

Get a specific deployment as YAML

kubectl get deployment my-app -o yaml

List pods with a specific label

kubectl get pods -l app=nginx

Watch pods in real-time

kubectl get pods -w

When to Use kubectl get

kubectl get is the command you'll use most often. It retrieves a summary table of one or more resources. For detailed information about a specific resource, use kubectl describe instead.

Output Formats

The -o flag supports several powerful formats:

# JSONPath for extracting specific fields
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

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

# JSON for programmatic processing
kubectl get pods -o json | jq '.items[] | .metadata.name'

Combining with Other Tools

# Count running pods
kubectl get pods --field-selector=status.phase=Running --no-headers | wc -l

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

Interview Questions About This Command

How do you list all resources in a namespace?
Use kubectl get all -n <namespace>, though this doesn't truly show all resources — it misses ConfigMaps, Secrets, etc.
How do you filter pods by label?
Use -l flag: kubectl get pods -l app=nginx,tier=frontend. Supports =, ==, != operators and set-based selectors.
What's the difference between -o wide and -o yaml?
-o wide adds extra columns (node, IP). -o yaml outputs the full resource spec for inspection or backup.

Common Mistakes

  • Using 'kubectl get all' and assuming it shows every resource type — it misses CRDs, ConfigMaps, Secrets, and more.
  • Forgetting to specify -n <namespace> and looking in the wrong namespace.
  • Not using --field-selector to filter by status, which is more efficient than piping through grep.

Related Commands