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 redirects traffic arriving at one port to a designated port on another endpoint. It can make a containerized service available on a development computer or provide local access to a service inside a Kubernetes cluster. section 3.8.3 section 11.3.2

For a Docker container, use -p to bind a host port to a container port. The service must also be configured to listen on the container port. In this example, PORT=3000 configures the microservice, while -p 3000:3000 forwards host traffic to it. section 3.8.3

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

Run the video-streaming image in the background with host port 3000 mapped to container port 3000. section 3.8.3

If the host port is already allocated, stop the other application or choose another port. Use docker container list to check the running container and its port mapping. section 3.8.3 section 3.8.3

For a Kubernetes microservice accessible only inside the cluster, kubectl port-forward provides local access for testing without exposing the service to the outside world. The following command forwards localhost:6000 to port 80 on the named pod while the command is running. section 11.3.2

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

Forward local port 6000 to port 80 on the metadata pod, then send requests to http://localhost:6000. section 11.3.2

The general form is kubectl port-forward <pod-name> [<local-port>:]<pod-port>. The same technique can reach a third-party service such as a RabbitMQ dashboard. Take care when inspecting a customer-facing system. section 11.3.2

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

How deep it goes
Demonstrates port mappings and commands, including service configuration, port conflicts, and the lifetime of a Kubernetes forward.
Examples
Uses a video-streaming container on port 3000 and a metadata pod forwarded from local port 6000 to port 80.
What you can do after
Apply port forwarding via Docker and kubectl to reach a container or test a cluster-internal microservice locally.
How it connects
Connects port forwarding to containers, microservices, Kubernetes pods, and network ports.

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.