liveBook · Manning

Kubectl port-forward

As explained in Learn Docker in a Month of Lunches, Second Edition

kubectl port-forward is a kubectl command that forwards local network traffic to a port on a Kubernetes Pod or other resource.

kubectl port-forward sends traffic from a local machine into a port on a Kubernetes Pod. It provides a quick way to test or debug an HTTP application, including one running in a remote cluster. section 15.2

Forward traffic to a Pod

After deploying the numbers-api Pod, run `kubectl port-forward pod/numbers-api 8015:80`. Browse to `http://localhost:8015/rng` to reach the Pod's port 80 through local port 8015. Stop forwarding with Ctrl-C or Cmd-C. section 15.2

Use port forwarding for:

  • Local testing of a Pod that is not directly reachable from outside the cluster. section 15.2
  • Debugging an application in a cloud-hosted Kubernetes cluster through the local machine. section 15.2
  • Temporary access rather than public exposure; use a Kubernetes Service for incoming application traffic. section 15.2
# deploy the application:
kubectl apply -f ch15/exercises/numbers-api/pod.yaml
# list Pods, showing the labels:
kubectl get pods --show-labels
# print human-friendly details about the Pod:
kubectl describe pod numbers-api
# set kubectl to listen on port 8015
# and relay traffic to the Pod on port 80:
kubectl port-forward pod/numbers-api 8015:80
# now browse to http:/./localhost:8015/rng
# Ctrl-C or Cmd-C to exit the port-forward

Shows the deployment, inspection, port-forward command, local URL, and exit instruction in one runnable sequence. section 15.2

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains the traffic path and demonstrates one command, but stops short of detailed forwarding internals.
Examples
Demonstrates forwarding localhost:8015 to numbers-api port 80 and browsing to /rng.
What it assumes
Assumes a deployed Pod and basic kubectl use; introduces the need for Services when exposure must be public.
What you can do after
Operate/configure kubectl port-forward to reach and debug a Pod through a local browser.
How it connects
Connects Pods, Kubernetes networking, kubectl, HTTP, and Services.

Also explained in

Compare how 2 books explain kubectl port-forward →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.