Port forwarding in Kubernetes

Port forwarding in Kubernetes allows you to access internal pod services (like a web server, database, or API) on your local machine, even if they’re not exposed externally via a Service or Ingress.

When you use K9s (a terminal-based UI for managing Kubernetes clusters), it gives you a quick and user-friendly way to set up port forwarding to pods—without manually typing out kubectl port-forward commands.

K9s itself doesn’t directly configure port forwarding, but it offers a convenient way to initiate port forwarding to Kubernetes pods using a terminal UI. Here’s how to do port forwarding using K9s:


Steps to Use Port Forwarding in K9s

  1. Start K9s
    Run this in your terminal: bashCopyEditk9s
  2. Navigate to Pods (:pods or just po)
    • Use arrow keys or / to filter and select the pod you want.
  3. Select the Target Pod
    • Move the cursor to highlight the desired pod.
    • Press Shift+F (uppercase F) to start port forwarding.
  4. Enter Local and Remote Ports
    • You’ll be prompted to specify ports in the format: [localPort]:[remotePort] For example: 8080:80 This maps your localhost:8080 to the pod’s port 80.
  5. Verify Forwarding
    • K9s will show a status line that the port is forwarded.
    • You can now access the service at localhost:[localPort].
  6. Stop Forwarding
    • Use Ctrl+C or select the pod again and press Shift+F to toggle off forwarding.

Leave a comment