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
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.
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.
Final Answer:
kubectl exec -it frontend-pod -c web -- /bin/bash -> Option A
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
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.
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.
Final Answer:
Lists files and directories inside /var/lib/mysql in the mysql container -> Option A
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
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.
Step 2: Apply the fix
Add -c app or -c backend to the command to specify the container name explicitly.
Final Answer:
Add -c app or -c backend to specify the container -> Option B
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
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.
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.