Spring Boot + Kubernetes Tutorial - Difference between ClusterIP, NodePort and LoadBalancer Service
Video
This tutorial is explained in the below Youtube Video.Spring Boot + Kubernetes Tutorial
What is Kubernetes? Need for it? Install Ubuntu on Windows using WSL Installing Kubectl, Minikube and Docker on Ubuntu Create Docker Image Deploy to Minikube Pods Difference between ClusterIP, NodePort and LoadBalancer Service Service Hello World Example
Services Overview
In the kubernetes documentation they have given sample service file. We will be making use of this sample file as a reference. There are 3 types of services--
ClusterIP - In Kubernetes, Clusterip services help
applications within the cluster communicate with each other.
For example we have microservice-consumer consuming the REST endpoint exposed by microservice-producer programatically.
apiVersion: v1 kind: Service metadata: name: employee-producer-service spec: selector: app: boot-jar ports: - protocol: TCP port: 80 targetPort: 8080 type: ClusterIP
Clusterip provides loadbalancing at the pod level and not the node level. -
NodePort - NodePort services in Kubernetes are a way to expose your application to the outside world or make it accessible
from outside the cluster.
Again consider the employee-producer service which has exposed some rest endpoints. This is deployed as a kubernetes pod. The exposed REST endpoints are now to be consumed not by an internal client within the kubernetes but external client outside the cluster. So in this scenario for the employee producer pod we define a service of type nodeport.
apiVersion: v1 kind: Service metadata: name: employee-producer-service spec: selector: app: boot-jar ports: - protocol: TCP port: 80 targetPort: 8080 nodePort: 30080 type: NodePort
Within the cluster nodeport works similar to the way clusterip works. If any external client wants to access the application running on pod then it will need to make a call to the node directly on the nodeport. The main disadvantage of using nodeport is exposing the nodes directly to the external client. Also the unusual port numbers may cause firewall issues. So this type of service is mostly used for development purpose. -
LoadBalancer -
LoadBalancer services provide a public IP address and port that act as an entry point for external traffic.
Again consider the employee-producer service which has exposed some rest endpoints. This is deployed as a kubernetes pod. The exposed REST endpoints are to be consumed not by an internal client within the kubernetes but external client outside the cluster. So in this scenario for the employee producer pod we define a service of type loadbalancer.
apiVersion: v1 kind: Service metadata: name: employee-producer-service spec: selector: app: boot-jar ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer
Loadbalancer works similar to nodeport in exposing the deployed pod applications to external clients. So if nodeport already exposes the pod application to the external client then what is the need for loadbalancer. Loadbalancer creates an abstraction layer for the external client which redirect their requests to the appropriate node. With loadbalancer the client is not aware of the internal node structure and also if the request is being forwarded to which of the available nodes. As the name suggests loadbalancer balances the load between different nodes by distributing the requests between the nodes. Cloud platforms like Azure, AWS, GCP provide out of the box loadbalancer which can be used. The major downside of using loadbalancer is the additional cloud billing cost associated with using them.