liveBook · Manning

Kubectl port-forward

As explained in Kubernetes in Action, Second Edition

kubectl port-forward is a kubectl operation that forwards traffic from a local port to a port on a Kubernetes resource such as a pod.

kubectl port-forward creates a local proxy to an application port on a Kubernetes pod or service. It is especially useful during development and troubleshooting because you can reach a pod without finding its IP address. section 5.3.1

Forward a local port to a pod

Run `kubectl port-forward kiada 8080` in one terminal, then run `curl localhost:8080` in another. Requests to local port 8080 travel through the proxy and return the application response from the pod. section 5.3.1

Use port forwarding for focused local access, while keeping its operational boundaries in mind:

  • Forward several application ports together, such as HTTP 8080, HTTPS 8443, and an administration port 9901. section 5.4.3
  • Access services through localhost with tools such as curl or a browser; HTTPS may require handling a self-signed certificate and hostname mismatch. section 5.4.3
  • Remember that traffic crosses the local proxy, API server, and Kubelet before reaching the pod, so a broken path can prevent access even when the application is otherwise reachable. section 5.3.1
  • Use `kubectl port-forward` with services as well as pods when the target should be a service rather than one specific pod. section 5.3.1
$ kubectl port-forward kiada-ssl 8080 8443 9901
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
Forwarding from 127.0.0.1:8443 -> 8443
Forwarding from [::1]:8443 -> 8443
Forwarding from 127.0.0.1:9901 -> 9901
Forwarding from [::1]:9901 -> 9901

Inspect a multi-port forwarding session and compare the local listeners created for the application. section 5.4.3

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains commands, local responses, multi-port forwarding, and the proxy path through cluster components.
Examples
Uses Kiada pod requests, localhost curl commands, and HTTP, HTTPS, and administration ports in a multi-container pod.
What it assumes
Assumes a running Kubernetes application with known target and container ports, while avoiding any need to discover the pod IP.
What you can do after
Apply it via kubectl to access pod or service ports locally and test application responses with curl or a browser.
How it connects
Connects pods and services with local proxies, curl, the API server, Kubelet, containers, and HTTPS certificates.

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 Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.