Search Tutorials


Spring Boot + Kubernetes Tutorial - Difference between ClusterIP, NodePort and LoadBalancer Service | JavaInUse



Spring Boot + Kubernetes Tutorial - Difference between ClusterIP, NodePort and LoadBalancer Service

In previous tutorial we deployed spring boot application to kubernetes pod. In this tutorial we will be looking at what are services. Using services we will be able to access the application deployed on pods. In Kubernetes, services are a fundamental concept that allows applications to communicate with each other within the cluster. Think of services as an abstraction layer that acts as a single entry point, providing a stable and reliable way for different components of your application to find and interact with each other.
Kubernetes Spring Boot 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-
  1. 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.
    Kubernetes Spring Boot Service microservice-consumer
    In such a scenario when we deploy these applications as kubernetes pods,we do not want the employee-producer pod exposed outside the cluster, but just internally for employee-consumer pod. So we will define a service of type clusterip for employee-producer.
    Kubernetes Spring Boot Service microservice-consumer

    In the below diagram I have a scenario where we have 3 replicas of the employee-producer and single instance of employee-consumer. When the employee-consumer calls the service, the request can be forwarded to any of the employee-producer pod by the clusterip service.
    kubernetes ClusterIP architecture
    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.
  2. 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.
    kubernetes NodePort architecture
    With NodePort services, a specific port on every node in the cluster is allocated to your application. This port should be above 30000.
    kubernetes NodePort 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.
  3. 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.
    kubernetes LoadBalancer
    Unlike the nodeport service there are no port restrictions for loadbalancer service.
    kubernetes LoadBalancer NodePort
    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.