Port forwarding within a Kubernetes cluster is a helpful tool for debugging. Find out how it’s done.
Port forwarding is a very handy tool that can help you debug various applications and deployments within your Kubernetes cluster. For example, you might have one specific pod that is misbehaving, so you need to connect to it directly. Because this is a microservice environment, you can (with the help of port forwarding) talk to a back-end service you wouldn’t otherwise expose.
How do you do that?
It’s actually pretty simple. Let me show you.
SEE: How to become a network administrator: A cheat sheet (TechRepublic)
What you’ll need
In order to pull this off, you’ll need a Kubernetes cluster up and running. If you’re not sure how to do that, read my tutorial: How to deploy a Kubernetes cluster on Ubuntu server.
How to deploy the pod
The first thing we’re going to do is deploy an NGINX pod. Do this with the command:
kubectl run web-pod --image=nginx --port=80 --generator=run-pod/v1
This will deploy a pod, named web-pod, using the NGINX image on port 80.
To make sure the pod has been successfully deployed, issue the command:
kubectl get pods
You should see web-pod listed (Figure A).
Get the detailed information about the pod with the command:
kubectl describe pods web-pod
You should see more information about the pod than you probably need (Figure B).
How to configure port forwarding for the pod
It’s now time to configure port forwarding for our newly-deployed NGINX pod. This is done using the port-forward option of the kubectl command like so:
kubectl port-forward web-pod 8080:80
You should then see that forwarding is working (Figure C).
Leave that session as is.
To test the port forwarding, access a new session within your deployed container and use the curl command to test the forwarding like so:
You should see the NGINX welcome page print out and the original terminal window indicating that forwarding is in action (Figure D).
That’s it. You’ve set up port forwarding for a Kubernetes pod. With this technique, you can debug deployments by accessing ports you wouldn’t normally expose. From here you can build on this fundamental technique for tasks like database, application, or network debugging within your container deployments.
Source of Article