The basic forms
The left side of the mapping is the port on your Mac. The right side is the port in the selected workload. When both are the same, one number is enough.
kubectl port-forward pod/api-7d9c8f6c9b-k2x4m 8080:8080 -n development
kubectl port-forward deployment/api 8080:8080 -n development
kubectl port-forward service/api 8443:https -n development
A Deployment or Service is convenient when pod names change. kubectl selects a matching pod. If that pod terminates, the forwarding session ends and must be started again. This is a temporary debugging connection, not a resilient tunnel.
Let kubectl choose a free local port
A fixed local port can collide with another process. Leave the local side empty and kubectl will select an available port.
kubectl port-forward deployment/api :8080 -n development
Read the command output to see the assigned local port. This is useful for one-off checks and when several forwarding sessions target the same remote port.
Localhost is the safer default
By default, kubectl listens on localhost. That makes the service available to processes on your machine but not generally to other devices on the network. Binding with --address 0.0.0.0 exposes the local listener on all IPv4 interfaces. Do that only when you have a specific, reviewed reason and understand the host firewall and service authentication.
A port forward can provide direct access that bypasses normal ingress paths, gateways, or network-level controls. It does not grant permissions by itself—the Kubernetes API server authorizes the request—but the resulting local connection may reach an endpoint that was never designed for broad access.
Understand pod, Deployment, and Service targets
Target a pod when you need one exact replica, perhaps to compare a faulty instance with a healthy one. Target a Deployment for convenience when any selected replica is suitable. Target a Service when you think in terms of a named service port. In each case, kubectl ultimately forwards to a pod.
When debugging uneven behaviour across replicas, do not target the Deployment and assume you know which pod was selected. Record the chosen pod from the output or select it directly.
Common failures
- Connection refused: the process may not be listening on the remote port, may be bound only to a different interface inside the container, or may not be ready.
- Address already in use: another local process holds the chosen local port. Pick another or let kubectl select one.
- Forbidden: the user lacks the required Kubernetes permissions, commonly including access to pods and the
pods/portforwardsubresource. - Session closes: the selected pod was replaced, the API connection dropped, your credentials expired, or the kubectl process ended.
- Wrong protocol: kubectl port-forward supports TCP, not UDP.
Test the connection deliberately
Once forwarding starts, use the client appropriate for the service: a browser, curl, a database client, or a debugger. If TLS terminates in the pod, connect with HTTPS. If the certificate name does not match localhost, use the intended hostname mapping or a client option approved for the test rather than disabling verification casually.
Keep a note of local port, remote port, namespace, context, target pod, and start time during an incident. Stop the process when the test is complete.
When to use something else
Port forwarding is good for short-lived operator access. Use an Ingress, Gateway, private load balancer, VPN, bastion, or service mesh feature when access must be reliable, shared, audited as a service, or maintained over time. Repeatedly restarting a developer's terminal tunnel is not an operational design.
Clusterdeck can create and track port-forward sessions from a selected Kubernetes resource on macOS. The active cluster, namespace, target, and local mapping remain visible, which helps when several sessions are open. Kubernetes RBAC still decides whether the operation is allowed.