liveBook · Manning

Port forwarding

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

Port forwarding is the redirection of network traffic arriving at one port to a designated port on another network endpoint.

Port forwarding lets a request to a local port reach an application listening at another endpoint. It is useful both for reaching a microservice in a Docker container and for testing a microservice that is normally accessible only inside a Kubernetes cluster. section 3.8.3 section 11.3.2

cd chapter-3/example-1
docker run -d -p 3000:3000 -e PORT=3000 video-streaming

Run the container with host port 3000 bound to container port 3000. The PORT environment variable separately tells the microservice which port to listen on. section 3.8.3

The two port settings must work together: binding a container port does not configure the microservice to listen on it. If host port 3000 is already allocated, stop the other application or choose another available port. Check the running mapping with docker container list; its PORTS column shows where host traffic goes. section 3.8.3

kubectl port-forward metadata-55bb6bdf58-7pjn2 6000:80

Forward local port 6000 to port 80 on the named Kubernetes pod. While the command runs, requests to http://localhost:6000 reach the internal microservice without exposing it to the outside world. section 11.3.2

Use kubectl port-forward <pod-name> [<local-port>:]<pod-port> when testing a different pod or port. This provides a way to check responses from an internal microservice or reach a cluster-hosted tool such as the RabbitMQ dashboard. Take care when inspecting customer-facing systems. section 11.3.2

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Shows Docker and kubectl commands, port mappings, and basic verification; does not explain network-routing internals.
Examples
Uses a video-streaming container mapped from port 3000 to 3000 and a metadata pod forwarded from local port 6000 to port 80.
What it assumes
Assumes an existing Docker image or Kubernetes pod and a microservice configured to listen on a port.
What you can do after
Operate/configure it with Docker or kubectl to reach a listening microservice and check its responses locally.
How it connects
Connects Docker containers, Kubernetes pods, microservices, environment variables, and production debugging.

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 Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.