Port forwarding works by creating a tunnel between a local port on your machine and a port on a remote service (like a pod in Kubernetes). Here’s a simplified explanation of how it functions:
-
Local Port: You specify a local port (e.g., 19000) on your machine where you will send requests.
-
Remote Port: You specify a remote port (e.g., 80) on the service (like a web server) that you want to access.
-
Establishing the Tunnel: When you run a port forwarding command (e.g.,
kubectl port-forward), it sets up a connection that listens on the local port. Any requests sent to this local port are forwarded to the specified remote port. -
Request Handling: When you send a request (e.g., using
curl http://localhost:19000), the port forwarding mechanism captures this request and redirects it to the remote service's port (80 in this case). -
Response: The response from the remote service is sent back through the same tunnel to your local port, allowing you to see the results of your request.
This method is useful for accessing services that are not exposed to the public internet, making it a secure way to test and debug applications during development.
