liveBook · Manning

Port forwarding

As explained in Kubernetes for Developers

Port forwarding maps a network port on one endpoint to a port on another endpoint so traffic can reach a service.

Port forwarding connects a convenient local port to a service running elsewhere. It lets you test a container or Kubernetes Service through localhost without assigning the service a public endpoint. section 3.4.3 section 2.2.1

Use port forwarding when you need to:

  • Browse or query a containerized HTTP server locally, such as mapping host port 8080 to container port 80. section 2.2.1
  • Reach a Kubernetes Service locally when a LoadBalancer Service has no external IP in a local cluster. section 3.4.3
  • Debug a cloud Kubernetes Service or let a Docker container reach it through host.docker.internal. section 3.4.3
kubectl port-forward service/$SERVICE_NAME $FROM_PORT:$TO_PORT

Run kubectl port-forward with a Service name and a local-to-Service port pair: kubectl port-forward service/$SERVICE_NAME $FROM_PORT:$TO_PORT. section 3.4.3

Expose a Kubernetes time service

Forward the Service with kubectl port-forward service/timeserver 8080:80, then open http://localhost:8080. For a Docker container using Redis, forward 6379:6379 and connect to host.docker.internal on port 6379. section 3.4.3

How Kubernetes for Developers covers it

How deep it goes
Explains host-to-service and host-to-container mappings with commands, access patterns, and a network-interface flag.
Examples
Uses a Python HTTP server, timeserver Kubernetes Service, localhost:8080, Redis on port 6379, and curl verification.
What it assumes
Assumes a running container or Kubernetes Service and basic port awareness; demonstrates access through browsers, curl, and client libraries.
What you can do after
Apply it via kubectl or Docker to test services locally and connect external Docker containers to forwarded Kubernetes services.
How it connects
Ties port forwarding to Kubernetes Services, Docker containers, LoadBalancer Services, Minikube, kubectl, and host.docker.internal.

Also explained in

Compare how 5 books explain port forwarding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.