Kubernetes Administration
medium

Questão de Kubernetes Administration #019e3a4f

Enunciado da Questão
Kubernetes Administration
#kubectl
#exec
#containers
#pods
Médio

How do you perform a command in a running container of a Pod?

A

kubectl exec --

Correta
B

docker exec

C

kubectl run --

D

kubectl attach -i

Explicação Oficial / Explicações da Comunidade

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 the kubectl CLI 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 exec operates 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 --: The kubectl run command 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: The kubectl attach command 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 -i flag makes it interactive, but it still does not allow running arbitrary commands like exec does.