🔌 Access K8s Services Like They’re Local
Service running in cluster. Need to test it. Don’t want to expose it publicly. Port-forward to localhost.
Basic Port Forward
# Forward pod port to localhost kubectl port-forward pod/my-pod 8080:80 # Now access: http://localhost:8080 # Traffic goes to pod's port 80 # Forward service instead kubectl port-forward svc/my-service 8080:80 # Forward deployment kubectl port-forward deployment/my-app 8080:80
💡 Common Use Cases
- Database access: Connect to PostgreSQL in cluster from local GUI
- API testing: Test backend API before exposing via ingress
- Dashboard access: View Grafana/Kibana without public internet
- Debugging: Attach debugger to running pod
# Example: Access PostgreSQL kubectl port-forward svc/postgres 5432:5432 # Now connect with: # psql -h localhost -p 5432 -U admin # Example: Multiple ports kubectl port-forward pod/my-pod 8080:80 9090:9000 # Background mode kubectl port-forward svc/my-api 8080:80 &
“Port-forward is my daily debugging tool. Test services before exposing them. Connect local tools to cluster databases. No need for LoadBalancer costs during development.”
