Kubernetes Administration
medium
Pregunta de Kubernetes Administration #019e3a4f
Enunciado de la Pregunta
Kubernetes Administration
#kubectl
#exec
#containers
#pods
Medio
How do you perform a command in a running container of a Pod?
A
kubectl exec --
Correcto
B
docker exec
C
kubectl run --
D
kubectl attach -i
Explicación Oficial / Explicaciones de la Comunidad
The correct command to execute a command inside a running container of a Pod is kubectl exec --. This command allows you to run arbitrary commands in a container, similar to docker exec but for Kubernetes pods. The -- separates kubectl options from the command to execute inside the container.
How it Works
kubectl exec requires a pod name and optionally a container name (if the pod has multiple containers). The syntax is:
kubectl exec <pod-name> [-c <container-name>] -- <command> [args...]
The double dash -- is used to indicate the end of kubectl options and the beginning of the command to run inside the container. For example:
kubectl exec my-pod -- ls /var/log
This runs ls /var/log inside the first container of my-pod.
Why the Correct Answer is Right
kubectl exec --: This is the standard Kubernetes command for executing commands in a container. It is part of thekubectlCLI and is designed for this purpose.
Why the Incorrect Answers are Wrong
docker exec: This command is used to run commands in a Docker container, not a Kubernetes pod. While Kubernetes uses containers (often Docker),docker execoperates on the Docker daemon directly and does not understand Kubernetes pod abstractions. It would only work if you know the underlying container ID, which is not recommended in a Kubernetes context.kubectl run --: Thekubectl runcommand is used to create and run a new pod or deployment, not to execute commands in an existing container. It is for launching new workloads, not for interacting with running containers.kubectl attach -i: Thekubectl attachcommand attaches to a running container's standard input/output/error streams. It is used for interactive sessions, but it does not execute a new command; it connects to the main process of the container. The-iflag makes it interactive, but it still does not allow running arbitrary commands likeexecdoes.