Linux command
kubectl-run 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Run pod
kubectl run [pod-name] --image=[nginx]
Run interactive pod
kubectl run [pod-name] --image=[busybox] -it --rm -- [/bin/sh]
Run with port
kubectl run [pod-name] --image=[nginx] --port=[80]
Run with env vars
kubectl run [pod-name] --image=[nginx] --env="[KEY=value]"
Dry run output
kubectl run [pod-name] --image=[nginx] --dry-run=client -o yaml
Run with command
kubectl run [pod-name] --image=[busybox] -- [echo hello]
说明
kubectl run creates and starts a single pod in the cluster from a specified container image. It is designed for quick, ad-hoc pod creation and is commonly used for debugging, running one-off tasks, and testing container images without writing a full manifest file. The command supports interactive mode with `-it` for attaching a terminal session directly to the container, which is useful for launching temporary troubleshooting pods with tools like busybox or curl. Combined with `--rm`, the pod is automatically deleted when the session ends. The `--dry-run=client -o yaml` pattern is frequently used to generate a pod manifest template that can be customized and applied separately. In earlier Kubernetes versions, `kubectl run` could create deployments and other resource types, but it now exclusively creates standalone pods. For production workloads, use deployments, statefulsets, or jobs instead to get replication, rolling updates, and self-healing capabilities.
参数
- --image _IMAGE_
- Container image.
- -it
- Interactive TTY.
- --rm
- Delete pod on exit.
- --port _PORT_
- Container port.
- --env _VAR=VALUE_
- Environment variable.
- --dry-run _MODE_
- `none`, `client`, or `server` — `client` prints the manifest without contacting the API; `server` validates against the cluster.
- --restart _POLICY_
- `Always` (default), `OnFailure`, or `Never`. `Never` produces a bare Pod; the others adjust the generated PodSpec accordingly.
- --command
- Treat extra args after `--` as the container's `command` (entrypoint) instead of arguments to the image entrypoint.
- --labels, -l _KEY=VALUE,..._
- Comma-separated labels to set on the pod.
- -o _FORMAT_
- Output format: yaml, json, name, jsonpath, etc. Combine with `--dry-run=client -o yaml` to template manifests.
- --image-pull-policy _POLICY_
- `Always`, `IfNotPresent`, or `Never`.
- --overrides _JSON_
- JSON merge patch applied to the generated PodSpec for fields not exposed as flags.
- --help
- Display help information.
FAQ
What is the kubectl-run command used for?
kubectl run creates and starts a single pod in the cluster from a specified container image. It is designed for quick, ad-hoc pod creation and is commonly used for debugging, running one-off tasks, and testing container images without writing a full manifest file. The command supports interactive mode with `-it` for attaching a terminal session directly to the container, which is useful for launching temporary troubleshooting pods with tools like busybox or curl. Combined with `--rm`, the pod is automatically deleted when the session ends. The `--dry-run=client -o yaml` pattern is frequently used to generate a pod manifest template that can be customized and applied separately. In earlier Kubernetes versions, `kubectl run` could create deployments and other resource types, but it now exclusively creates standalone pods. For production workloads, use deployments, statefulsets, or jobs instead to get replication, rolling updates, and self-healing capabilities.
How do I run a basic kubectl-run example?
Run `kubectl run [pod-name] --image=[nginx]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --image _IMAGE_ do in kubectl-run?
Container image.