Which kubectl command runs a command in an existing container?

Study for the Kubernetes Certified Network Administrator Exam. Our test offers comprehensive flashcards, multiple-choice questions, and detailed explanations. Be confident for your exam!

Multiple Choice

Which kubectl command runs a command in an existing container?

Explanation:
When you want to run a command inside a running container, you need a tool that executes inside the container’s process space without creating something new. That’s what kubectl exec does: it runs the specified command in a pod’s existing container. You can make it interactive with -it, for example kubectl exec -it my-pod -- /bin/bash to get a shell, or run a single command like kubectl exec my-pod -- ls / to check a directory. If the pod has multiple containers, you can pick one with -c container-name. Attaching to a running container via kubectl attach doesn’t start a new command; it connects your terminal to the main process’s standard input/output streams. Viewing logs is done with kubectl logs, which prints container output but doesn’t execute a new command. Running a new pod or container is handled by kubectl run, which creates a pod, not command execution inside an existing container.

When you want to run a command inside a running container, you need a tool that executes inside the container’s process space without creating something new. That’s what kubectl exec does: it runs the specified command in a pod’s existing container. You can make it interactive with -it, for example kubectl exec -it my-pod -- /bin/bash to get a shell, or run a single command like kubectl exec my-pod -- ls / to check a directory. If the pod has multiple containers, you can pick one with -c container-name.

Attaching to a running container via kubectl attach doesn’t start a new command; it connects your terminal to the main process’s standard input/output streams. Viewing logs is done with kubectl logs, which prints container output but doesn’t execute a new command. Running a new pod or container is handled by kubectl run, which creates a pod, not command execution inside an existing container.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy