Linux command
kubectl-get 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
List pods
kubectl get pods
List all resources
kubectl get all
Output a specific pod as YAML
kubectl get pod [pod-name] -o yaml
Output all pods as JSON
kubectl get pods -o json
Watch for changes to pods
kubectl get pods --watch
List pods across all namespaces
kubectl get pods -A
Filter pods by label
kubectl get pods -l [app=nginx]
List pods with additional columns
kubectl get pods -o wide
Custom columns output
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
Get resources from a file
kubectl get -f [path/to/manifest.yaml]
说明
kubectl get retrieves and displays one or more Kubernetes resources from the cluster. It is the most frequently used kubectl subcommand for inspecting cluster state, supporting every resource type including pods, deployments, services, nodes, configmaps, and custom resources defined by CRDs. The command provides flexible output formatting through the `-o` flag, including human-readable tables, YAML, JSON, JSONPath expressions, and custom column definitions. Results can be filtered by label selectors, field selectors, and namespaces, or retrieved across all namespaces with the `-A` flag. The `--watch` flag enables real-time streaming of resource changes via the Kubernetes watch API. By default, the output shows a summary table with key fields like name, status, and age. The `wide` output format adds additional columns such as node assignments and IP addresses, while structured formats like YAML and JSON return the complete resource specification for scripting and automation.
参数
- -o, --output _FORMAT_
- Output format: json, yaml, wide, name, custom-columns, jsonpath, go-template, and more.
- -w, --watch
- Watch for changes after listing the requested objects.
- --watch-only
- Watch for changes without listing/getting first.
- -A, --all-namespaces
- List objects across all namespaces.
- -l, --selector _SELECTOR_
- Label selector (supports =, ==, !=, in, notin).
- -n, --namespace _NAMESPACE_
- Namespace scope for this request.
- -f, --filename _FILE_
- Filename, directory, or URL to files identifying the resource.
- -k, --kustomize _DIR_
- Process the kustomization directory.
- --sort-by _JSONPATH_
- Sort list output by the specified JSONPath expression.
- --field-selector _SELECTOR_
- Filter by field selector (e.g., status.phase=Running).
- -L, --label-columns _LABELS_
- Comma-separated list of labels to present as columns.
- --show-labels
- Show all labels as the last column of output.
- --show-kind
- List the resource type for the requested objects.
- --no-headers
- Omit table headers from output.
- --chunk-size _N_
- Return large lists in chunks rather than all at once (default 500).
- --ignore-not-found
- Suppress NotFound errors when getting specific resources.
- -R, --recursive
- Process the directory used in -f recursively.
- --help
- Display help information.
FAQ
What is the kubectl-get command used for?
kubectl get retrieves and displays one or more Kubernetes resources from the cluster. It is the most frequently used kubectl subcommand for inspecting cluster state, supporting every resource type including pods, deployments, services, nodes, configmaps, and custom resources defined by CRDs. The command provides flexible output formatting through the `-o` flag, including human-readable tables, YAML, JSON, JSONPath expressions, and custom column definitions. Results can be filtered by label selectors, field selectors, and namespaces, or retrieved across all namespaces with the `-A` flag. The `--watch` flag enables real-time streaming of resource changes via the Kubernetes watch API. By default, the output shows a summary table with key fields like name, status, and age. The `wide` output format adds additional columns such as node assignments and IP addresses, while structured formats like YAML and JSON return the complete resource specification for scripting and automation.
How do I run a basic kubectl-get example?
Run `kubectl get pods` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -o, --output _FORMAT_ do in kubectl-get?
Output format: json, yaml, wide, name, custom-columns, jsonpath, go-template, and more.