NodePort and Headless Service for Kubernetes
Civo Academy - NodePort and Headless Service in Kubernetes
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.
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 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.