NodePort is a Kubernetes Service type that exposes an application running inside a cluster through a specific port on each cluster node. It allows users or external systems to access an application using a node's IP address and the assigned NodePort. This is a simple way to make a Kubernetes application accessible from outside the cluster without requiring a separate external load balancer.
How Does NodePort Work?
When you create a Service with the NodePort type, Kubernetes allocates a port, typically from the range 30000–32767 by default. Incoming traffic sent to that port on a node is forwarded to the Service and then routed to one of the available application Pods. Kubernetes manages this routing so traffic can reach the appropriate backend even when Pods are distributed across multiple nodes.
For example, if your application runs on port 8080 inside a container and the NodePort is 30080, you may access it using http://NODE_IP:30080, provided the node is reachable and firewall rules allow the connection.
Example of a NodePort Service
The following YAML configuration exposes an application named my-app through NodePort:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: NodePort
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
nodePort: 30080
In this example, port is the Service's internal port, targetPort is the port used by the application Pods, and nodePort is the port exposed on cluster nodes. The Pods must have matching labels, such as app: my-app, for the Service to route traffic correctly.
Advantages and Limitations of NodePort
NodePort is easy to configure and useful for development, testing, demonstrations, and environments where a simple external access method is sufficient. However, it exposes a port on every node, requires network connectivity to those nodes, and may need additional firewall configuration. It also does not automatically provide the advanced routing, TLS termination, or hostname-based routing commonly available through an Ingress controller or an external load balancer.
NodePort vs. ClusterIP vs. LoadBalancer
- ClusterIP: Exposes a Service internally within the cluster. It is the default Service type.
- NodePort: Makes a Service accessible through a designated port on each node.
- LoadBalancer: Requests an external load balancer, when supported by the underlying infrastructure, to expose the Service.
Conclusion
NodePort is a useful Kubernetes Service type for exposing applications through node IP addresses and a designated port. It helps developers understand how external traffic reaches Services and Pods, making it valuable for learning and basic testing. For production environments, teams should evaluate whether an Ingress controller or LoadBalancer Service offers better traffic management, security, and scalability for their requirements.