CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Kubernetes / kubectl port-forward

kubectl port-forward

Reach a pod or Service from your laptop

Intermediatekubernetesnetworkingdebugging

kubectl port-forward opens a local port on your machine and tunnels its connections, through the API server and the node's kubelet, to a port inside a pod. It's how you reach an internal dashboard, a database or one specific pod without exposing anything publicly.

Syntaxkubectl port-forward (POD | pod/NAME | svc/NAME | deploy/NAME) [LOCAL_PORT:]REMOTE_PORT [...] [--address 127.0.0.1] [-n NS]

Watch it run

One api replica returns errors. Talk to that exact pod from your laptop, bypassing the Service and the load balancer.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
kubectl port-forward pod/api-7f9c5d4b6-2xkqp 8080:8080 -n prod

kubectl listens on localhost (IPv4 and IPv6) and stays in the foreground. Leave it running and use a second terminal.

Anatomy of a command

$ :
commandkubectl

The Kubernetes CLI.

How it works

kubectl resolves the target to a single pod (for svc/ or deploy/ it lists the Service's or Deployment's pods and picks one running pod, translating the Service port to that pod's containerPort). It opens a listener on your machine, then POSTs to the pod's portforward subresource and upgrades to a streaming connection (WebSocket in current versions). The API server authorises create on pods/portforward and proxies the stream to the kubelet, which asks the container runtime to connect to the port inside the pod's network namespace. Each local TCP connection becomes a pair of data and error streams over that single tunnel. Only TCP works, not UDP. If the chosen pod dies, the tunnel breaks and kubectl exits with 'lost connection to pod'; it does not fail over to another replica.

Options you'll actually use

LOCAL:REMOTEMap a local port to the pod's port, e.g. 8080:80
:REMOTEPick a random free local port
pod/ svc/ deploy/Target type; svc and deploy pick one pod
--address ADDRLocal listen address(es); default localhost
--pod-running-timeoutHow long to wait for a running pod (default 1m)
-n NSNamespace

Common mistakes

Load-testing a Service through kubectl port-forward svc/api

All traffic goes to one pod through the API server, which becomes the bottleneck; you measure the tunnel, not the app.

Test from inside the cluster or through the real Ingress/load balancer
--address 0.0.0.0 on a laptop at a café

Anyone on that network can reach your forwarded database or admin UI with your access.

Keep the default localhost binding
Using port-forward as the way users reach an app

It's a single developer tunnel that breaks on pod restarts and needs cluster credentials.

Expose it with a Service of type LoadBalancer or an Ingress/Gateway
Port-forwarding a UDP port (DNS 53/udp)

port-forward only carries TCP.

kubectl exec into a pod and use dig there, or run a debug pod

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command to reach port 9090 of the Prometheus Service in namespace monitoring on your local port 19090.

  2. 2

    Read this and explain the arrow: you ran 'kubectl port-forward svc/api 8080:80' and got 'Forwarding from 127.0.0.1:8080 -> 8080'.

  3. 3

    Practise the 'port already in use' check in the sandbox: start a listener on 8080 with nc, then find which process holds the port.

Related commands

kubectl execRun a command or shell inside a running containerssSee listening ports and open connectionscurlMake HTTP requests and debug APIskubectl getList Kubernetes resources and their status
Previouskubectl rolloutNextkubectl top
Linux / Kubernetes / kubectl port-forward

kubectl port-forward

Reach a pod or Service from your laptop

Intermediatekubernetesnetworkingdebugging

kubectl port-forward opens a local port on your machine and tunnels its connections, through the API server and the node's kubelet, to a port inside a pod. It's how you reach an internal dashboard, a database or one specific pod without exposing anything publicly.

Syntaxkubectl port-forward (POD | pod/NAME | svc/NAME | deploy/NAME) [LOCAL_PORT:]REMOTE_PORT [...] [--address 127.0.0.1] [-n NS]

Watch it run

One api replica returns errors. Talk to that exact pod from your laptop, bypassing the Service and the load balancer.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
kubectl port-forward pod/api-7f9c5d4b6-2xkqp 8080:8080 -n prod

kubectl listens on localhost (IPv4 and IPv6) and stays in the foreground. Leave it running and use a second terminal.

Anatomy of a command

$ :
commandkubectl

The Kubernetes CLI.

How it works

kubectl resolves the target to a single pod (for svc/ or deploy/ it lists the Service's or Deployment's pods and picks one running pod, translating the Service port to that pod's containerPort). It opens a listener on your machine, then POSTs to the pod's portforward subresource and upgrades to a streaming connection (WebSocket in current versions). The API server authorises create on pods/portforward and proxies the stream to the kubelet, which asks the container runtime to connect to the port inside the pod's network namespace. Each local TCP connection becomes a pair of data and error streams over that single tunnel. Only TCP works, not UDP. If the chosen pod dies, the tunnel breaks and kubectl exits with 'lost connection to pod'; it does not fail over to another replica.

Options you'll actually use

LOCAL:REMOTEMap a local port to the pod's port, e.g. 8080:80
:REMOTEPick a random free local port
pod/ svc/ deploy/Target type; svc and deploy pick one pod
--address ADDRLocal listen address(es); default localhost
--pod-running-timeoutHow long to wait for a running pod (default 1m)
-n NSNamespace

Common mistakes

Load-testing a Service through kubectl port-forward svc/api

All traffic goes to one pod through the API server, which becomes the bottleneck; you measure the tunnel, not the app.

Test from inside the cluster or through the real Ingress/load balancer
--address 0.0.0.0 on a laptop at a café

Anyone on that network can reach your forwarded database or admin UI with your access.

Keep the default localhost binding
Using port-forward as the way users reach an app

It's a single developer tunnel that breaks on pod restarts and needs cluster credentials.

Expose it with a Service of type LoadBalancer or an Ingress/Gateway
Port-forwarding a UDP port (DNS 53/udp)

port-forward only carries TCP.

kubectl exec into a pod and use dig there, or run a debug pod

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command to reach port 9090 of the Prometheus Service in namespace monitoring on your local port 19090.

  2. 2

    Read this and explain the arrow: you ran 'kubectl port-forward svc/api 8080:80' and got 'Forwarding from 127.0.0.1:8080 -> 8080'.

  3. 3

    Practise the 'port already in use' check in the sandbox: start a listener on 8080 with nc, then find which process holds the port.

Related commands

kubectl execRun a command or shell inside a running containerssSee listening ports and open connectionscurlMake HTTP requests and debug APIskubectl getList Kubernetes resources and their status
Previouskubectl rolloutNextkubectl top