Difference between kubectl port-forwarding and NodePort service

12.3k views Asked by At

Whats the difference between kubectl port-forwarding (which forwards port from local host to the pod in the cluster to gain access to cluster resources) and NodePort Service type ?

3

There are 3 answers

0
Fauzan On

If you use port forwarding kubectl port forward svc/{your_service} -n {service_namespace} you just need a clusterIP, kubectl will handle the traffic for you. Kubectl will be the proxy for your traffic

If you use nodeport for access your service means you need to open port on the worker nodes.

0
Mark Watney On

You are comparing two completely different things. You should compare ClusterIP, NodePort, LoadBalancer and Ingress.

The first and most important difference is that NodePort expose is persistent while by doing it using port-forwarding, you always have to run kubectl port-forward ... and kept it active.

kubectl port-forward is meant for testing, labs, troubleshooting and not for long term solutions. It will create a tunnel between your machine and kubernetes so this solution will serve demands from/to your machine.

NodePort can give you long term solution and it can serve demands from/to anywhere inside the network your nodes reside.

0
Yilmaz On

when you use port forwarding, that is going to cause our cluster to essentially behave like it has a node port service running inside of it without creating a service. This is strictly for the development setting. with one command you will have a node port service.

 // find the name of the pod that running nats streaming server
kubectl get pods

kubectl port-forward nats-Pod-5443532542c8-5mbw9 4222:4222

kubectl will set up proxy that will forward any trafic on your local machine to a port on that specific pod.

however, to create a node port you need to write a YAML config file to set up a service. It will expose the port permanently and performs load balancing.