Skip to content

kubectl port-forward

kubectl port-forward is a command-line utility used to map a local network port to a port on a specific resource within a Kubernetes cluster.^[400-devops__06-Kubernetes__k8s-ithelp__Day6__README.md]

Functionality

The command creates a secure tunnel that forwards traffic from a specified port on the local machine (localhost) to a designated port on a Kubernetes resource.^[400-devops__06-Kubernetes__k8s-ithelp__Day6__README.md] This allows users to interact with internal cluster applications as if they were running locally, which is particularly useful for debugging or database access.

Syntax

The general syntax for the command follows this format:

[kubectl](<./kubectl.md>) port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT
  • TYPE/NAME: The resource type and name (e.g., pod/foo).
  • [LOCAL_PORT:]: The port number on the local machine. This is optional; if omitted, a random local port is chosen.
  • REMOTE_PORT: The port number on the target resource (e.g., the Pod's container port).^[400-devops__06-Kubernetes__k8s-ithelp__Day6__README.md]

Usage Example

To forward local port 8080 to port 8080 on a Pod named foo, the following command is used:

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

Upon execution, the terminal will display output indicating the forwarding is active, such as:

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

This confirms that traffic sent to localhost:8080 is being tunneled to the specified port on the Kubernetes resource.^[400-devops__06-Kubernetes__k8s-ithelp__Day6__README.md]

Sources

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