0
0
Kubernetesdevops~5 mins

kubectl exec for container access in Kubernetes - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the command kubectl exec do?
It lets you run commands inside a running container in a Kubernetes pod, like opening a terminal session or running a script.
Click to reveal answer
beginner
How do you open an interactive shell inside a container using kubectl exec?
Use kubectl exec -it <pod-name> -- /bin/sh or /bin/bash to start an interactive shell session.
Click to reveal answer
beginner
What does the -i and -t flags mean in kubectl exec -it?
-i keeps the input open (interactive), and -t allocates a terminal, so you can type commands like in a normal shell.
Click to reveal answer
intermediate
How do you specify which container to exec into if a pod has multiple containers?
Use the -c <container-name> option to choose the container inside the pod.
Click to reveal answer
beginner
What happens if you run kubectl exec <pod-name> -- ls /?
It runs the ls / command inside the container, listing the root directory contents without opening an interactive shell.
Click to reveal answer
Which command opens an interactive bash shell inside a pod named 'webapp'?
Akubectl shell webapp
Bkubectl exec -it webapp -- /bin/bash
Ckubectl get pods webapp --shell
Dkubectl run webapp -- /bin/bash
What does the -- signify in the command kubectl exec pod -- ls?
ASpecifies the pod namespace
BEnds the command
CSeparates kubectl options from the command to run inside the container
DIndicates the container name
If a pod has two containers, how do you run a command in the second container?
Akubectl exec pod -c container2 -- command
Bkubectl exec pod --container container2 command
Ckubectl exec pod container2 -- command
Dkubectl exec pod --container=container2 command
What is the purpose of the -t flag in kubectl exec -it?
AAllocates a pseudo-TTY (terminal)
BRuns the command in the background
CSpecifies the target pod
DTerminates the session after command
Which command lists files in the root directory of a pod named 'db' without opening a shell?
Akubectl exec db ls /
Bkubectl exec -it db -- ls /
Ckubectl exec db -c ls /
Dkubectl exec db -- ls /
Explain how to use kubectl exec to open an interactive shell inside a container.
Think about how to connect to a container like opening a terminal.
You got /5 concepts.
    Describe the difference between running a command with kubectl exec interactively and non-interactively.
    Consider when you want to type commands versus just get results.
    You got /4 concepts.