Bird
Raised Fist0
Kubernetesdevops~20 mins

kubectl exec for container access in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kubectl Exec Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of kubectl exec with command
What is the output of this command if the pod myapp-pod has a container named app-container running a Linux shell?

kubectl exec myapp-pod -c app-container -- ls /tmp
AReturns an error: container app-container not found
BShows the logs of the container app-container
CStarts an interactive shell session inside the container
DLists files and directories inside /tmp of the container
Attempts:
2 left
💡 Hint
The command runs 'ls /tmp' inside the specified container.
💻 Command Output
intermediate
2:00remaining
Error when container name is missing
What error will you get if you run this command on a pod with multiple containers?

kubectl exec mypod -- ls /
AError: a container name must be specified for pod mypod, choose one of: [container1 container2]
BLists root directory contents of the first container automatically
CSyntax error: missing container name
DCommand runs successfully on all containers
Attempts:
2 left
💡 Hint
Pods with multiple containers require specifying which container to exec into.
🔀 Workflow
advanced
2:00remaining
Correct command to start an interactive shell inside a container
Which command correctly starts an interactive bash shell inside the container named web in pod frontend-pod?
Akubectl exec -it frontend-pod -c web -- /bin/bash
Bkubectl exec frontend-pod -c web /bin/bash -i -t
Ckubectl exec frontend-pod -c web --interactive --tty /bin/bash
Dkubectl exec frontend-pod -c web -- bash -it
Attempts:
2 left
💡 Hint
Use -i and -t flags before pod name for interactive terminal.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting kubectl exec permission denied error
You run kubectl exec mypod -- cat /etc/shadow and get a permission denied error. What is the most likely reason?
AThe pod is not running
Bkubectl exec requires root privileges on the host machine
CThe user inside the container does not have permission to read /etc/shadow
DThe container image does not have /etc/shadow file
Attempts:
2 left
💡 Hint
File /etc/shadow is usually restricted to root user inside Linux.
✅ Best Practice
expert
2:00remaining
Best practice for running commands inside containers with kubectl exec
Which practice is best when using kubectl exec to run commands inside containers in production?
ARun kubectl exec commands as root user inside containers for full access
BAlways specify the container name with -c to avoid ambiguity
CUse kubectl exec without -it flags to avoid interactive sessions
DModify container images to include debugging tools for exec commands
Attempts:
2 left
💡 Hint
Pods often have multiple containers; specifying container avoids mistakes.

Practice

(1/5)
1. What does the kubectl exec command do in Kubernetes?
easy
A. Lists all running pods in the cluster
B. Deletes a pod from the cluster
C. Creates a new pod in the cluster
D. Runs a command inside a running container in a pod

Solution

  1. Step 1: Understand the purpose of kubectl exec

    The kubectl exec command is used to run commands inside a container that is already running inside a pod.
  2. Step 2: Compare with other kubectl commands

    Deleting pods uses kubectl delete pod, creating pods uses kubectl create, and listing pods uses kubectl get pods. These are different commands.
  3. Final Answer:

    Runs a command inside a running container in a pod -> Option D
  4. Quick Check:

    kubectl exec = run command inside container [OK]
Hint: kubectl exec runs commands inside containers [OK]
Common Mistakes:
  • Confusing exec with pod creation or deletion
  • Thinking exec lists pods
  • Using exec without specifying pod
2. Which of the following is the correct syntax to start an interactive shell inside a container named web in pod frontend-pod?
easy
A. kubectl exec -it frontend-pod -c web -- /bin/bash
B. kubectl exec frontend-pod -c web -it /bin/bash
C. kubectl exec -c web frontend-pod /bin/bash -it
D. kubectl exec frontend-pod /bin/bash -c web -it

Solution

  1. Step 1: Recall correct kubectl exec syntax

    The correct syntax for interactive shell is: kubectl exec -it POD_NAME -c CONTAINER_NAME -- /bin/bash. The -it flags come before the pod name, and -- separates command from options.
  2. Step 2: Analyze each option

    kubectl exec -it frontend-pod -c web -- /bin/bash matches the correct syntax exactly. The other options have misplaced flags or arguments causing syntax errors.
  3. Final Answer:

    kubectl exec -it frontend-pod -c web -- /bin/bash -> Option A
  4. Quick Check:

    Correct flag order and -- separator = kubectl exec -it frontend-pod -c web -- /bin/bash [OK]
Hint: Use -it before pod name and -- before command [OK]
Common Mistakes:
  • Placing -it after pod name
  • Omitting -- before command
  • Misordering container flag -c
3. What will be the output of this command if the pod db-pod has a container named mysql running and you execute:
kubectl exec db-pod -c mysql -- ls /var/lib/mysql?
medium
A. Lists files and directories inside /var/lib/mysql in the mysql container
B. Shows an error: container mysql not found
C. Lists files in the host machine's /var/lib/mysql directory
D. Starts an interactive shell inside the mysql container

Solution

  1. Step 1: Understand the command

    The command runs ls /var/lib/mysql inside the mysql container of the db-pod pod. It lists files in that directory inside the container.
  2. Step 2: Eliminate wrong options

    Shows an error: container mysql not found is wrong because the container exists. Lists files in the host machine's /var/lib/mysql directory is wrong because kubectl exec runs inside container, not host. Starts an interactive shell inside the mysql container is wrong because no shell is started; only ls runs.
  3. Final Answer:

    Lists files and directories inside /var/lib/mysql in the mysql container -> Option A
  4. Quick Check:

    kubectl exec runs command inside container = Lists files and directories inside /var/lib/mysql in the mysql container [OK]
Hint: kubectl exec runs command inside container, not host [OK]
Common Mistakes:
  • Thinking command runs on host machine
  • Confusing exec with shell start
  • Assuming container does not exist
4. You run kubectl exec -it mypod -- /bin/sh but get the error: error: a container name must be specified for pod mypod, choose one of: [app backend]. What is the fix?
medium
A. Restart the pod before running exec
B. Add -c app or -c backend to specify the container
C. Remove -it flags to fix the error
D. Use kubectl exec mypod /bin/sh without flags

Solution

  1. Step 1: Understand the error message

    The error says multiple containers exist in pod mypod, so you must specify which container to exec into using -c.
  2. Step 2: Apply the fix

    Add -c app or -c backend to the command to specify the container name explicitly.
  3. Final Answer:

    Add -c app or -c backend to specify the container -> Option B
  4. Quick Check:

    Multiple containers need -c flag = Add -c app or -c backend to specify the container [OK]
Hint: Specify container with -c when pod has multiple containers [OK]
Common Mistakes:
  • Ignoring container name when multiple containers exist
  • Removing -it flags unnecessarily
  • Restarting pod without reason
5. You want to run an interactive bash shell inside the container named worker in pod job-pod. The pod has two containers: worker and logger. Which command correctly achieves this?
hard
A. kubectl exec job-pod -c worker -it /bin/bash
B. kubectl exec -it job-pod /bin/bash -c worker
C. kubectl exec -it job-pod -c worker -- /bin/bash
D. kubectl exec job-pod -it -c worker /bin/bash

Solution

  1. Step 1: Recall correct syntax for interactive exec with container

    The correct syntax is kubectl exec -it POD_NAME -c CONTAINER_NAME -- /bin/bash. The -it flags come before pod name, -c specifies container, and -- separates command.
  2. Step 2: Check options for correct order and flags

    kubectl exec -it job-pod -c worker -- /bin/bash matches the correct syntax exactly. The other options have misplaced flags or missing --, causing errors.
  3. Final Answer:

    kubectl exec -it job-pod -c worker -- /bin/bash -> Option C
  4. Quick Check:

    Correct flag order and -- separator = kubectl exec -it job-pod -c worker -- /bin/bash [OK]
Hint: Use -it before pod, -c container, and -- before command [OK]
Common Mistakes:
  • Misordering flags -it and -c
  • Omitting -- before command
  • Not specifying container when multiple exist
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard