NodePort and Headless Service for Kubernetes

Anaïs Urlichs avatar
By Anaïs Urlichs

Description

Use NodePorts to connect to nodes within a cluster and route external traffic, and utilize Headless Service for advanced and custom routing solutions in Kubernetes.


Transcription

Introduction to NodePort and headless service

Welcome back to another Kubernetes Service video. In this video, we're going to look at the type NodePort. Before, we looked at the type ClusterIP. Now, if we have our Kubernetes cluster and different nodes running within this cluster, they all will have different endpoints that we can connect to. So now, in the case of ClusterIP, our IP address will only be accessible from within the nodes within our cluster, they won't be accessible cluster-wide, and this is where type NodePort comes in.

We have the nodes and services. And then, we want to connect dynamically with the different nodes within the environment, and we want to have a standard way of connecting to those different nodes. And this is where we can specify NodePorts.

What is a NodePort?

With NodePorts, we can specify a range of ports between 30,000 to 32,000. So, it's like the port address. And then we have, for instance, a port NodePort of 31,520 or something, something like that, specified for our NodePort. Now, it's only accessible within that range. So, in most cases, you want Kubernetes to choose the NodePort for you within the different range, and then you can configure external traffic. So, let's say there is traffic from the outside world, and we want to route the traffic into our cluster. We can then set up our load-balancing traffic routing solution, and they will all connect to our NodePort here.

Now, NodePort is probably the type used least in this case. This is because you would want to use ClusterIP for most cases. However, if you want to set up a custom solution, you can use the type NodePort to route the traffic. So each of those nodes will have this specific port as they are NodePort defined. And this way, the service will know how to connect to those different nodes dynamically.

What is a headless service?

Now, we can also have something called a Headless Service. In the case of a Headless Service, we will not specify any port. So, in this case, use the command kubectl get all -n example. In this example, we can see, I've set up a type NodePort, and here is, within the range, I have the 32093 port specified as my NodePort port that always connects across the cluster to my port 3000, so I can access my deployment.

If we want to specify Headless Service, we will not specify any type. We would set it to type none, then spin up a Headless Service. Now, Headless Service has more advanced use cases and specific use cases that allow us to customize a lot and use resources that are not Kubernetes-specific to connect to the service. The services that we discussed within this video are specific to Kubernetes, but in some cases, you might want to set up custom solutions that are not dependent on a particular type.

So, summarizing, type NodePort and type Headless Service allow us to set up more customized routing solutions for our service running within our Kubernetes cluster.

Don't stop now, check out your next lesson