A NodePort service is the most primitive way to get external traffic directly to your service. NodePort, as the name implies, opens a specific port on all the Nodes (the VMs), and any traffic that is sent to this port is forwarded to the service.

Exposes the Service on each Node's IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You'll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort>.

to expose NodePort use the below command

kubecl expose [pod podname] --type=NodePort --port=8000 --target-port=80 --name my-nodeport-service

The YAML for a NodePort service looks like this:

apiVersion: v1
kind: Service
  name: my-nodeport-service
    app: my-app
  type: NodePort
    - name: http
      port: 80
      targetPort: 80
      nodePort: 30036
      protocol: TCP

What is the difference between clusterIP Service and node port Service?

Basically, a NodePort service has two differences from a normal “ClusterIP” service.

First, the type is “NodePort.” There is also an additional port called the nodePort that specifies which port to open on the nodes. If you don’t specify this port, it will pick a random port. Most of the time you should let Kubernetes choose the port.

When would you use this?

There are many downsides to this method:

  1. You can only have one service per port
  2. You can only use ports 30000–32767
  3. If your Node/VM IP address change, you need to deal with that

For these reasons, It is not recommended to use this method in production to directly expose your service. If you are running a service that doesn’t have to be always available, or you are very cost-sensitive, this method will work for you. A good example of such an application is a demo app or something temporary.