kubectl get
Display one or many resources. The most frequently used kubectl command for inspecting cluster state.
kubectl get [TYPE] [NAME] [flags]Common Flags
| Flag | Short | Description |
|---|---|---|
| --output | -o | Output format: json, yaml, wide, jsonpath, custom-columns |
| --selector | -l | Filter by label selector |
| --all-namespaces | -A | List resources across all namespaces |
| --watch | -w | Watch for changes after listing |
| --field-selector | — | Filter by field values (e.g., status.phase=Running) |
Examples
List all pods in the current namespace
kubectl get podsList pods across all namespaces with extra info
kubectl get pods -A -o wideGet a specific deployment as YAML
kubectl get deployment my-app -o yamlList pods with a specific label
kubectl get pods -l app=nginxWatch pods in real-time
kubectl get pods -wWhen 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
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.