Skip to content

kubectl Pod port-forwarding

kubectl Pod port-forwarding is a networking command used to forward one or more local ports to a specific Pod within a cluster.^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md]

This mechanism allows users to access applications running on a Pod directly via localhost on their local machine.^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md] This is particularly useful for debugging, database access, or exposing a service temporarily without creating a [[Service]] resource.

Syntax

The basic command requires the resource type (Pod), the resource name, and the port mapping in the format local_port:container_port.^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md]

The command syntax follows this structure:^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md]

kubectl port-forward [Pod](<./pod.md>)/<pod_name> <local_port>:<pod_port>

Usage Example

In a typical workflow, you first identify the target Pod, establish the forwarding connection, and then access the application via a local request.^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md]

For example, to forward local port 8080 to port 80 on a Pod named nginx-pod:^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md]

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

Once the tunnel is active, you can interact with the Pod using localhost on the specified port:^[400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md]

curl http://localhost:8080

Sources

  • 400-devops__06-Kubernetes__k8s-learning__07.pv-pvc__README.md