Forward traffic to kubernetes services

2 min read

When we want to access services that run inside a kubernetes cluster that are not supposed to be normally accessed we can temporally run kubectl port-forward to forward traffic to our workstation. To be able to use it, the node must have socat installed.

There are plenty of ways of using kubectl port-forward, most commonly we will want to reach a kubernetes service. To tell kubectl we want to route all traffic from our workstation port 8080 to the port 80 to the service called demo we would execute:

kubectl port-forward service/demo 8080:80

We can do the same for a pod:

kubectl port-forward pod/demo 8080:80

Or even use the deployment selectors to reach one of the pods that this deployment creates:

kubectl port-forward deployment/mongo 8080:08

We can also use a named port instead of a number:

kubectl port-forward service/myservice 8443:https

Or even listen all interfaces using --address option:

kubectl port-forward --address pod/mypod 8888:80

Posted on 13/05/2021