Skip to content

Kubernetes Pod port-forwarding

In Kubernetes, port-forwarding is a mechanism used to map a local port on the host machine to a port on a specific Pod. This allows users to access applications running within a cluster via localhost as if they were running locally^[400-devops__06-Kubernetes__k8s-ithelp__Day17__README.md].

This is commonly used for debugging, accessing internal dashboards, or connecting to databases that are not exposed via a Service^[400-devops__06-Kubernetes__k8s-ithelp__Day17__README.md].

Usage

The general syntax to forward a port requires specifying the resource type (e.g., pod), the resource name, and the port mapping [local_port]:[pod_port]^[400-devops__06-Kubernetes__k8s-ithelp__Day17__README.md].

Command Example

To forward port 8080 on the local machine to port 80 on a Pod named emptydir-pod:

[kubectl port-forward](<./kubectl-port-forward.md>) [Pod](<./pod.md>)/emptydir-pod 8080:80

Upon execution, the terminal will display confirmation of the forwarding connection, indicating that traffic destined for 127.0.0.1:8080 is being directed to the Pod^[400-devops__06-Kubernetes__k8s-ithelp__Day17__README.md].

Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

Verifying Connectivity

Once the forwarding is active, standard tools like curl can interact with the Pod through the local port^[400-devops__06-Kubernetes__k8s-ithelp__Day17__README.md].

curl http://localhost:8080
  • Pod
  • [[Kubernetes Volumes]]
  • [[Services]]

Sources

^[400-devops__06-Kubernetes__k8s-ithelp__Day17__README.md]