What is NodePort in Kubernetes?

When you deploy an application in Kubernetes, by default it’s only accessible inside the cluster. To make it available outside, you need to expose it.
One of the simplest ways to do this is with a NodePort service.


How NodePort Works

  • Kubernetes opens a port (30000–32767) on each node of the cluster.
  • Any request sent to <NodeIP>:<NodePort> is forwarded to the service and then to the pods.
  • Under the hood, it still uses ClusterIP, but adds an external entry point.

So you can access your app like:

http://:


Example: NodePort Service

apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080
  • port → inside cluster service port (80).
  • targetPort → container port (8080).
  • nodePort → external port on nodes (30080).

Pros and Cons

✅ Advantages

  • Simple to set up.
  • Works in any Kubernetes cluster (no cloud dependency).
  • Good for dev/test environments.

⚠️ Disadvantages

  • Limited port range (30000–32767).
  • Requires knowing node IPs.
  • Not production-friendly compared to LoadBalancer or Ingress.

When to Use NodePort

  • Local clusters (like Minikube or kind).
  • Development and testing.
  • Small setups where a cloud LoadBalancer is not available.

Summary

  • NodePort is the simplest way to expose a Kubernetes service outside the cluster.
  • It maps a fixed port on every node to your service.
  • Great for learning and dev, but in production, LoadBalancer or Ingress are usually better.