Course contentsShow
← Cloud and Infrastructure
Lesson 1142 of 2,98922. Kubernetes ArchitectureFree lesson

kubectl port-forward for Local Access

Forwarding local ports to pods or services for testing and debugging without exposing services externally.

kubectl port-forward for Local Access

What you'll learn: How to temporarily expose a Kubernetes pod or service to your local machine for testing and debugging without creating external endpoints.


What Is Port Forwarding?

kubectl port-forward creates a secure tunnel between your local workstation and a specific pod or service running inside your Kubernetes cluster. Think of it as drilling a temporary, private tunnel through the cluster wall directly to one container—no LoadBalancer or Ingress required.

This is invaluable when you need to test a database, debug an API, or access an admin dashboard that shouldn't be publicly exposed.

How It Works

Port forwarding maps a local port on your machine to a port in the target pod or service. All traffic you send to localhost:LOCAL_PORT gets securely forwarded through the Kubernetes API server to the pod's port.

Basic syntax:

kubectl port-forward <resource-type>/<resource-name> <local-port>:<pod-port>

Example forwarding to a pod:

kubectl port-forward pod/my-app-pod 8080:80

Now visiting http://localhost:8080 in your browser connects directly to port 80 inside my-app-pod.

Example forwarding to a service:

kubectl port-forward service/my-service 3000:80

This forwards to one of the pods behind the service.

When to Use It

  • Local testing: Access a microservice directly without external routing
  • Database connections: Connect your local SQL client to a cluster database
  • Debugging: Inspect application behavior without modifying network policies
  • Admin interfaces: Temporarily access internal dashboards

Port forwarding runs in the foreground—press Ctrl+C to stop it. It's intentionally temporary and meant for individual developer access, not production traffic.

Key Takeaway:
kubectl port-forward creates a secure, temporary tunnel from your local machine to a pod or service, perfect for testing and debugging without exposing services externally.