NodePort service

NodePort service enables the external access to pod running inside the node.

Let’s say we have nginx pod running inside a single node cluster and we want to access it, we would need to ssh inside the node and then do the curl. If we want to curl without sshing into the node, we would a service.

How it works?

Service with NodePort type takes the request coming to a specified node port and then forwards the request to the particular pod.

curl http://node-ip:node-port
        |
        |
        v
    node-port
        |
        |
        v
      service
        |
        |
        v
       pod

Resource file of a service can be as shown below.

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    type: router-service
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30008
  selector:
    type: router

Where port is the port on the service, targetPort is the pod port and nodePort is node port where the external request will come.

It matches the pod to map to using selector/labels.

Info

We can only use nodePort in range