Linux command
kubectl-exec 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Execute command in pod
kubectl exec [pod-name] -- [command]
Open interactive shell
kubectl exec -it [pod-name] -- /bin/sh
Execute in specific container
kubectl exec [pod-name] -c [container] -- [command]
Execute bash in pod
kubectl exec -it [pod-name] -- /bin/bash
说明
kubectl exec runs a command directly inside a container that is part of a running Kubernetes pod, functioning similarly to `docker exec`. It establishes a connection to the container's runtime environment through the Kubernetes API server and kubelet, allowing you to inspect files, run diagnostics, or open an interactive shell session. The double dash (--) is required to separate kubectl flags from the command and its arguments that should be passed to the container. When combined with -i (stdin) and -t (tty) flags, it provides a fully interactive terminal session. For pods with multiple containers, the -c flag lets you specify which container to target.
参数
- -c, --container _name_
- Container name in multi-container pod.
- -i, --stdin
- Pass stdin to container.
- -t, --tty
- Allocate TTY.
- -n, --namespace _name_
- Kubernetes namespace.
FAQ
What is the kubectl-exec command used for?
kubectl exec runs a command directly inside a container that is part of a running Kubernetes pod, functioning similarly to `docker exec`. It establishes a connection to the container's runtime environment through the Kubernetes API server and kubelet, allowing you to inspect files, run diagnostics, or open an interactive shell session. The double dash (--) is required to separate kubectl flags from the command and its arguments that should be passed to the container. When combined with -i (stdin) and -t (tty) flags, it provides a fully interactive terminal session. For pods with multiple containers, the -c flag lets you specify which container to target.
How do I run a basic kubectl-exec example?
Run `kubectl exec [pod-name] -- [command]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -c, --container _name_ do in kubectl-exec?
Container name in multi-container pod.