Node port, target port, and port these terms and their use cases always make me confused.
The longer I live, the more I realize that I am never wrong about anything, and that all the pains I have so humbly taken to verify my notions have only wasted my time!
Let’s start with an example –
Let’s assume you have written a simple server in Golang which is running on port – 3000
.
Now we can deploy this server in Kubernetes with the help of containerization. We can use mentioned command to expose the port of the server.
EXPOSE 3000
That means our container is exposing this port as our server is running on 3000.
Ok cool. Now you have deployed the application and defined a service.yml file to create a service let A logically abstract the pods of your application.
Now if you want to expose your app to the outside world you need a port defined for your node because there may be several services running so you need to define a unique port in which the node will listen for your service A. This port is NodePort. And whenever the node port is hit by a request from the outside world, the traffic will be routed to the service’s Port. So then the service port will route the traffic to the pod’s Target Port. The Port is basically to be used as an internal port of our service. So if another pods of another services want to communicate with you service A’s pod they will call the service as A:Port.
Summary:
- NodePort: The port on the node where external traffic will come in
- Port: The port of the service
- TargetPort: The port of the pod(s) to forward traffic to.