Kubectl is a powerful command-line tool used to interact with Kubernetes clusters. One of its essential functionalities is port-forwarding, which allows you to access services running within a Kubernetes cluster as if they were running on your local machine. This is particularly useful for debugging, accessing web applications, or interacting with internal services without exposing them to the wider network.
Here’s how you can use kubectl port-forward
with various examples:
Example 1: Forwarding a single port
To forward port 8080 of a pod named my-pod
in the namespace my-namespace
to your local machine:
kubectl port-forward -n my-namespace my-pod 8080
This command forwards traffic from port 8080 on the pod my-pod
to port 8080 on your local machine.
Example 2: Forwarding multiple ports
Forwarding ports 8080 and 8443 simultaneously from a pod named backend
in the default namespace:
kubectl port-forward backend 8080:80 8443:443
This command forwards port 80 of the pod backend
to port 8080 locally, and port 443 to port 8443 locally.
Example 3: Forwarding to a specific IP address
Forwarding port 8080 of a pod to a specific IP address (useful for testing connectivity from other machines):
kubectl port-forward my-pod 8080:8080 --address 0.0.0.0
This command makes the service available on all network interfaces of your machine, accessible via the specified IP address.
Example 4: Forwarding from a deployment
Forwarding port 80 of a deployment named frontend
in the default namespace:
kubectl port-forward deployment/frontend 8080:80
This command forwards traffic from the first available pod of the deployment frontend
to port 8080 on your local machine.
Example 5: Forwarding from a service
Forwarding port 5432 from a service named postgres
in the default namespace:
kubectl port-forward service/postgres 5432:5432
This command allows you to connect to the PostgreSQL service running in Kubernetes on port 5432, locally.
Example 6: Forwarding with pod selector
Forwarding port 8080 from any pod with the label app=nginx
in the namespace prod
:
kubectl port-forward -n prod pod -l app=nginx 8080:80
This command selects all pods with the label app=nginx
in the namespace prod
and forwards traffic from port 80 to port 8080 locally.
Example 7: Forwarding from a specific pod in a deployment
Forwarding port 8080 from a specific pod of a deployment named api
in the namespace dev
:
kubectl port-forward -n dev pod/api-78f8c79d76-9btfc 8080:80
This command forwards traffic from port 80 of the pod api-78f8c79d76-9btfc
to port 8080 on your local machine.
Example 8: Using random local ports
Forwarding ports from a pod named redis
to random ports locally:
kubectl port-forward redis 0:6379
This command forwards the default port 6379 of the pod redis
to a randomly chosen port on your local machine.
Example 9: Forwarding from a statefulset
Forwarding port 3306 from a statefulset named mysql
in the namespace prod
:
kubectl port-forward statefulset/mysql 3306:3306
This command forwards traffic from port 3306 of the MySQL statefulset mysql
to port 3306 locally.
Example 10: Verbose output
Forwarding port 8080 of a pod with verbose output for debugging:
kubectl port-forward -v=8 my-pod 8080:8080
This command provides detailed logging (level 8) to help diagnose any issues during the port forwarding process.
Also check similar articles.
Execute Commands in Kubernetes Pods with kubectl exec
Attach to Running Containers with kubectl attach
Retrieve Container Logs Using kubectl logs
Detailed Resource Inspection with kubectl describe
Update Node Taints with kubectl taint
Discussion about this post