Hint: Use pod/name and localPort:remotePort format [OK]
Common Mistakes:
Reversing port numbers (remote:local instead of local:remote)
Omitting 'pod/' prefix
Swapping ports in the command
3. Given the command kubectl port-forward svc/my-service 9090:80, what will happen when you open http://localhost:9090 in your browser?
medium
A. You will access the pod's port 9090 directly
B. You will access the service's port 80 through local port 9090
C. You will get a connection refused error
D. The command will fail because services cannot be port-forwarded
Solution
Step 1: Understand port-forward to service
The command forwards local port 9090 to port 80 of the service my-service. So accessing localhost:9090 connects to service port 80.
Step 2: Confirm service port-forward support
Kubectl supports port-forwarding to services, so the command works and forwards traffic correctly.
Final Answer:
You will access the service's port 80 through local port 9090 -> Option B
Quick Check:
Port-forward svc port works = access via local port [OK]
Hint: Local port maps to service port, access via localhost:localPort [OK]
Common Mistakes:
Thinking services cannot be port-forwarded
Confusing local and remote ports
Expecting pod port 9090 instead of service port 80
4. You run kubectl port-forward pod/my-pod 8080:80 but get the error: error: unable to forward port because pod is not running. What is the most likely cause?
medium
A. The pod my-pod is not in Running state
B. You used the wrong port numbers
C. Port-forward does not work with pods
D. You need to specify the namespace explicitly
Solution
Step 1: Analyze error message
The error says the pod is not running, so port-forward cannot connect to it.
Step 2: Check pod state requirement
Port-forward requires the pod to be in Running state to forward ports successfully.
Final Answer:
The pod my-pod is not in Running state -> Option A
Quick Check:
Pod must be Running for port-forward [OK]
Hint: Pod must be Running to port-forward [OK]
Common Mistakes:
Assuming port numbers cause this error
Thinking port-forward doesn't work with pods
Ignoring pod status before port-forward
5. You want to access two different pods pod-a and pod-b running on ports 8080 and 9090 respectively, on your local machine at ports 3000 and 4000 simultaneously. Which commands should you run?
hard
A. kubectl port-forward pod/pod-a 3000:8080; kubectl port-forward pod/pod-b 4000:9090
B. kubectl port-forward pod/pod-a 8080:3000 & kubectl port-forward pod/pod-b 9090:4000
C. kubectl port-forward pod/pod-a 3000:8080 & kubectl port-forward pod/pod-b 4000:9090
D. kubectl port-forward pod/pod-a 3000:8080 pod/pod-b 4000:9090
Solution
Step 1: Understand port-forward syntax and concurrency
Each port-forward command forwards one pod port to a local port. To run two simultaneously, run two commands in parallel using & or separate terminals.
Step 2: Verify port mappings
Local port 3000 maps to pod-a's 8080, and local 4000 maps to pod-b's 9090. The syntax is localPort:remotePort.
Step 3: Check command correctness
kubectl port-forward pod/pod-a 3000:8080 & kubectl port-forward pod/pod-b 4000:9090 correctly runs two commands in background with correct port mappings. kubectl port-forward pod/pod-a 3000:8080; kubectl port-forward pod/pod-b 4000:9090 runs sequentially, blocking second command.