Bird
Raised Fist0
Kubernetesdevops~20 mins

Executing commands in Pods in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pod Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of command executed inside a Pod
You run the command kubectl exec mypod -- ls /app on a Pod named mypod. The Pod's /app directory contains files index.html and config.yaml. What will be the output?
A
index.html
config.yaml
B
mypod
index.html
config.yaml
CError: directory not found
DNo resources found.
Attempts:
2 left
💡 Hint
The kubectl exec command runs the given command inside the Pod's container.
🧠 Conceptual
intermediate
1:30remaining
Understanding kubectl exec command syntax
Which of the following is the correct syntax to execute the command cat /etc/hosts inside a Pod named webserver?
Akubectl exec --webserver cat /etc/hosts
Bkubectl exec webserver -- cat /etc/hosts
Ckubectl exec webserver cat /etc/hosts
Dkubectl exec -- pod=webserver -- cat /etc/hosts
Attempts:
2 left
💡 Hint
The Pod name comes immediately after kubectl exec, followed by -- to separate the command.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting kubectl exec permission error
You try to run kubectl exec mypod -- whoami but get the error: error: unable to upgrade connection: Forbidden. What is the most likely cause?
AThe Kubernetes API server is down
BThe Pod does not exist
CThe command <code>whoami</code> is not installed in the Pod
DThe user running kubectl lacks permission to exec into the Pod
Attempts:
2 left
💡 Hint
The error mentions 'Forbidden', which relates to permissions.
🔀 Workflow
advanced
2:00remaining
Executing an interactive shell inside a Pod
You want to open an interactive bash shell inside a Pod named backend. Which command should you use?
Akubectl exec -it backend -- /bin/bash
Bkubectl exec backend /bin/bash -i
Ckubectl exec backend --interactive /bin/bash
Dkubectl exec backend -bash -it
Attempts:
2 left
💡 Hint
Use flags -i and -t for interactive terminal.
✅ Best Practice
expert
2:30remaining
Best practice for running commands in multi-container Pods
A Pod has two containers: app and sidecar. You want to run ls /data inside the sidecar container. Which command is correct?
Akubectl exec mypod --container sidecar ls /data
Bkubectl exec mypod --container sidecar -- ls /data
Ckubectl exec mypod -c sidecar -- ls /data
Dkubectl exec mypod --container=sidecar ls /data
Attempts:
2 left
💡 Hint
Use -c or --container with kubectl exec to specify container.

Practice

(1/5)
1. What is the primary purpose of the kubectl exec command in Kubernetes?
easy
A. To list all Pods in a namespace
B. To create a new Pod
C. To run commands inside a running Pod
D. To delete a Pod

Solution

  1. Step 1: Understand the function of kubectl exec

    The kubectl exec command is used to execute commands inside a running Pod's container.
  2. Step 2: Differentiate from other commands

    Creating, deleting, or listing Pods are done with other commands like kubectl create, kubectl delete, and kubectl get.
  3. Final Answer:

    To run commands inside a running Pod -> Option C
  4. Quick Check:

    kubectl exec runs commands inside Pods [OK]
Hint: Remember: exec means execute inside Pod [OK]
Common Mistakes:
  • Confusing exec with create or delete commands
  • Thinking exec lists Pods
  • Using exec to start new Pods
2. Which of the following is the correct syntax to run the command ls /app inside a Pod named web-pod?
easy
A. kubectl exec web-pod ls /app
B. kubectl exec web-pod -- ls /app
C. kubectl exec -- web-pod ls /app
D. kubectl exec -c web-pod ls /app

Solution

  1. Step 1: Identify correct command separator

    The -- separates the Pod name from the command to run inside the Pod.
  2. Step 2: Apply syntax rules

    The correct syntax is kubectl exec [pod-name] -- [command], so kubectl exec web-pod -- ls /app is correct.
  3. Final Answer:

    kubectl exec web-pod -- ls /app -> Option B
  4. Quick Check:

    Use -- before command [OK]
Hint: Use double dash (--) before command [OK]
Common Mistakes:
  • Omitting the -- separator
  • Placing -- before Pod name
  • Using -c without container name
3. Given a Pod named db-pod with two containers: mysql and backup, what is the output of this command?
kubectl exec db-pod -c backup -- echo Hello
medium
A. Pod not found
B. Error: container name required
C. No such container: backup
D. Hello

Solution

  1. Step 1: Understand the command components

    The command runs echo Hello inside the backup container of the db-pod Pod.
  2. Step 2: Predict the command output

    Since the container exists and the command is valid, it outputs Hello.
  3. Final Answer:

    Hello -> Option D
  4. Quick Check:

    Correct container specified with -c outputs command result [OK]
Hint: Use -c to specify container, output is command result [OK]
Common Mistakes:
  • Not specifying container when multiple exist
  • Using wrong container name
  • Expecting Pod name as container
4. You run this command but get an error:
kubectl exec mypod ls /data

What is the most likely cause?
medium
A. Missing -- before the command
B. Pod mypod does not exist
C. The ls command is invalid
D. You need to specify the container with -c

Solution

  1. Step 1: Check command syntax

    The command is missing the -- separator between Pod name and command.
  2. Step 2: Understand effect of missing --

    Without --, kubectl treats ls as a Pod or option, causing an error.
  3. Final Answer:

    Missing -- before the command -> Option A
  4. Quick Check:

    Always use -- before command [OK]
Hint: Always put -- before command to avoid errors [OK]
Common Mistakes:
  • Forgetting the -- separator
  • Assuming container must be specified when only one exists
  • Thinking command is invalid
5. You want to run cat /etc/config inside the app-container of Pod multi-pod which has three containers. Which command is correct?
hard
A. kubectl exec multi-pod -c app-container -- cat /etc/config
B. kubectl exec multi-pod cat /etc/config -c app-container
C. kubectl exec -c app-container multi-pod cat /etc/config
D. kubectl exec multi-pod -- -c app-container cat /etc/config

Solution

  1. Step 1: Identify correct order of options

    The -c option specifying the container must come after the Pod name and before the -- separator.
  2. Step 2: Confirm command separator usage

    The -- must separate the Pod/container specification from the command to run.
  3. Step 3: Validate each option

    kubectl exec multi-pod -c app-container -- cat /etc/config correctly places -c app-container before -- and then the command cat /etc/config.
  4. Final Answer:

    kubectl exec multi-pod -c app-container -- cat /etc/config -> Option A
  5. Quick Check:

    -c before --, then command [OK]
Hint: Put -c before --, then command [OK]
Common Mistakes:
  • Placing -c after the command
  • Putting -- before -c
  • Omitting -- separator
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