Understanding and Using NodePort Services in Kubernetes

Berkay HELVACIOGLU
2 min readFeb 3, 2024

--

Kubernetes, as a container orchestration platform, provides various ways to expose services within a cluster to the external world. One of these methods is the NodePort service type. In this article, we’ll explore the concept of NodePort services in Kubernetes, understand how they work, and learn how to use them effectively to expose applications to the outside world.

What is a NodePort Service?

In Kubernetes, services are typically used for communication between different parts of an application. However, if your application needs to communicate with the external world, NodePort services come in handy. A NodePort service is a type of service that binds a service to a specific port number on each Kubernetes node.

How Does NodePort Work?

When a NodePort service is created, each Kubernetes node exposes the service on a specified port number. This port number is usually chosen randomly within the range of 30000–32767. This port number is then used to provide access to the service from the outside world.

For example, the following YAML creates a NodePort service:

apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
selector:
app: my-app

This YAML creates a NodePort service named “my-nodeport-service.” The port number to be used for external access is dynamically assigned. The service targets all pods with the label “app: my-app.”

Using a NodePort Service

Once a NodePort service is created, you can access it from outside the cluster using the IP address of any Kubernetes node and the assigned port number. For instance:

http://<Kubernetes_Node_IP>:<Assigned_Port_Number>

You can access the service using the IP of any node in the Kubernetes cluster and the specified port number.

Advantages of NodePort Services:

  1. Simplicity: NodePort services are easy to set up and provide a straightforward way to expose services externally.
  2. Flexibility: NodePort services allow external access to services without the need for additional load balancers.
  3. Visibility: Since NodePort services use a specific port number, it’s easy to see which port to use for external access.

NodePort services in Kubernetes offer a simple and effective way to expose your services to the external world. By understanding how NodePort services work and how to use them, you can enhance the accessibility of your applications within your Kubernetes cluster. In summary, NodePort services are a valuable tool in the Kubernetes toolbox, providing an uncomplicated method for external communication in your containerized environment.

--

--