0
0
Kubernetesdevops~10 mins

kubectl port-forward for local access in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - kubectl port-forward for local access
Start kubectl port-forward
Connect local port to pod port
Listen on local port
Forward traffic to pod
Access pod service locally
Stop port-forward on user interrupt
This flow shows how kubectl port-forward connects a local port to a pod port, allowing local access to pod services.
Execution Sample
Kubernetes
kubectl port-forward pod/my-pod 8080:80
This command forwards local port 8080 to port 80 on the pod named 'my-pod'.
Process Table
StepActionLocal PortPod PortResult
1Start port-forward command--kubectl starts listening on local port 8080
2Connect local port to pod port808080Traffic on local 8080 will go to pod port 80
3User accesses localhost:8080808080Request forwarded to pod's port 80
4Pod responds808080Response sent back to local client
5User stops port-forward--kubectl stops forwarding and exits
💡 User interrupts port-forward, stopping the connection
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Local Port ListeningNoYes (8080)Yes (8080)Yes (8080)Yes (8080)No
Pod Port ConnectedNoNoYes (80)Yes (80)Yes (80)No
Traffic FlowNoneNoneNoneForwardedResponseStopped
Key Moments - 3 Insights
Why do we specify two ports like 8080:80 in the command?
The first port (8080) is the local machine port to listen on, and the second port (80) is the pod's port to forward traffic to, as shown in execution_table step 2.
What happens if the local port 8080 is already in use?
kubectl will fail to start port-forward because it cannot listen on the local port, so step 1 in execution_table will not succeed.
Does port-forward keep running after the command finishes?
No, port-forward runs until the user stops it manually, as shown in step 5 where the forwarding stops on user interrupt.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does traffic start forwarding from local port to pod port?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Check the 'Result' column in step 2 where connection is established.
According to variable_tracker, what is the state of 'Local Port Listening' after step 5?
ANo
BYes (8080)
CUnknown
DListening on pod port
💡 Hint
Look at the 'Local Port Listening' row under 'Final' column.
If the user changes the command to 'kubectl port-forward pod/my-pod 9090:80', what changes in the execution table?
ANo change in ports
BPod Port changes to 9090
CLocal Port changes to 9090 in all steps
DBoth ports change to 9090
💡 Hint
Refer to the 'Local Port' column in execution_table.
Concept Snapshot
kubectl port-forward <pod-name> <local-port>:<pod-port>
- Forwards local port to pod port
- Allows local access to pod services
- Runs until user stops it
- Local port must be free
- Useful for debugging or accessing pod apps
Full Transcript
kubectl port-forward lets you connect a port on your computer to a port on a Kubernetes pod. You run a command like 'kubectl port-forward pod/my-pod 8080:80'. This means your local port 8080 will send traffic to port 80 on the pod. When you open localhost:8080 in your browser, the request goes to the pod's port 80. The command keeps running and forwarding traffic until you stop it. If the local port is busy, the command won't start. This is useful to access pod services without exposing them externally.