Port fowarding in kubernetes

2/4/2020

I know that in kubernetes, we can't use a Service Node Port below 30000, because these ports are used by kubernetes. Can I use "kubectl port-forward svc/someservice 80:80" for instance... without causing conflict with the kubernetes ports below 30000?

-- Clarencio
kubectl
kubernetes

2 Answers

2/4/2020

In short - yes, you can.

In your question though it's clear that you're missing the understanding of NodePort type of service and the what the kubectl port-forward essentially does.

kubectl port-forward doesn't send the traffic through the port defined in the .spec.type: NodePort stanza in the Service resource. In fact using kubectl port-forward you're able to target a ClusterIP type of service (which doesn't have a .spec.type: NodePort stanza by definition).

-- Bernard Halas
Source: StackOverflow

2/5/2020

Could you please describe what is the reason to have such a setup?

kubectl port-forward svc/someservice 80:80 merely forwards your local_machine:80 to port:80 of endpoints for someservice .

In other words, connections made to local port 80 are forwarded to port 80 of the pod that is running your app. With this connection in place you can use your local workstation to debug the app that is running in the pod.

Due to known limitations, port forward today only works for TCP protocol. The support to UDP protocol is being tracked in issue 47862.

As of now (Feb-2020) the issue is still open.

Node Port is used for totally different stuff. It is used for cases when you shall reach pods by sending traffic to particular port on any node in your cluster.

That is why the answer for your question is "Definitely you can do that"; however, as I said before, it is not clear why you shall do that. Without that inf it is hard to provide a guidance on "what is the best way to achieve the required functionality"

Hope that helps.

-- Nick
Source: StackOverflow