What’s the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes?

What's the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes?

What are Kubernetes Services?

A service is just another Kubernetes object just like a pod (Pod is the smallest unit that can be deployed in the Kubernetes cluster), Which enables communication within and outside of the cluster. It can also be defined as an abstraction on top of a Pod, Which can help to provide a single IP address by which the pod can be accessed from anywhere

It’s a tool to expose your application to the Kubernetes world (containerization)

For Example:

Let’s say you have a Web application (Application is about booking an online ticket), Which has been deployed in the Kubernetes cluster and it has 2 pods with one container running on each pod. So these containers are responsible to process the User request like Booking, Cancelling, and Enquire and updating the details in the Database

So, We are missing the Obvies, Because, in this current setup, You can access the container only within the Kubernetes cluster. To expose these containers to the outside world, We need to do a few more extra things

Here comes the Services, Which help to expose this web application to the outside world. It listens to a port on a node and communicates with other pods or the external world. Based on the user case, We can choose any one of the services.

In our web application example, We can use Load balancer services, Which help to get a public IP and can help to communicate with external Users.

In this way, Kubernetes services allow us to easily expose our Web application to external users, Without even worrying about the underlying process carried out.

There are four types of services and will see the differences below

  • NodePort
  • ClusterIp
  • LoadBalancer
  • ExternalName

NodePort

This service enables us to expose the application on each Node static port. This allows us to access the application from outside of the cluster, Below are the example and the illustration of using the Node port

For Example:

– A web-application pod created on Node1 which listens to a port 80

– Now, We can easily communicate to that prod from the node

SSH to Node1 -> curl http://webapp:80 — This will work

What's the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes?

– But, If we want to access this web app from an external “Laptop or desktop” and currently it is not possible in the current setup

Services would help us to resolve the above issue, It will map a port on the host to the port on the pod.

What's the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes?

– It makes the pod to available on the “Node1” port (example: 30007), So that Users can able to access the web app from their local machine.

– Let’s see, How to create Service definition files for the above scenarios

apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  type: NodePort
  selector:
    app: webApp
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30007

-The Different types of ports and how to identify them (Check the example screenshot and compare it with the definition file)

What's the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes?

ClusterIP:

– This type of service will expose your application to other objects in the same Kubernetes cluster. Basically, This service will be assigned with a ClusterIP address that can be only reachable from within the cluster

– Reason for the use of this service is when you access the pod directly, it will have a downside – If the pod goes down it will come up with new IP, To avoid these types of issues “Cluster IP” is used. This will map your pod to another pod on a cluster-internal Ip and it will not cause any issue even if you are recreating or your pod goes down (As it is assigned with a Cluster IP which will not be changed)

What's the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes?

– As shown in the screenshot, multiple pods will be connected using ClusterIp service and the ClusterIP will remain static

Let’s see, How to create a Cluster Ip Service and a Example

apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    selector:
       app: webApp
    ports:
       -  name: http
          port: 80
          targetPort: 80
    type: ClusterIP
    clusterIP: 10.0.0.91

In the above example YAML file

The service name “my-service” will be associated with the pod/app which has the same label.

So, Service and the targetPod will be connected with port:targetport 80:80 and it has been assigned with a ClusterIp : 10.0.0.91

Now, One Pod can able to communicate with other pods using the cluster ip 10.0.0.91 and it will be diverted by the service to its corresponding associated pod.

In this way, An web application pod can able to access another Database pod within the cluster using the clusterIP services

Load Balancer:

— This Service will help us to expose an application to an external world/users. If you have a web application, You can create a Load Balancer from your cloud providers and they will assign a public IP address.

– User trying to access the LB – Public IP would be directed to the running pod – corresponding to their application and it also loads the balance the user requests across the pods. (If you have multiple pod running for your web application)

Let’s see, How to create a LoadBalancer Service and an Example:

apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    selector:
        app: webApp
    ports: 
      - name: http
        port: 80
    targetPort: 80
    type: LoadBalancer

We can create a LoadBalancer service with the above YAML file,

Once created, Kubernetes will automatically assign a public IP address to the Load Balancer and it will internally distribute the traffic to the pods which have same label “app: webApp”

Now, Any external user Can able to access the Web application via LoadBalancer Ip address. this service also ensures the traffic has been diverted to all other pods associated with the Service

ExternalName

– This Service helps us to map a service to an external DNS name. Which helps to access any external services which are not part of the Kubernetes cluster without knowing the IP address

Let’s see, How to create an ExternalName Service and an Example:

apiVersion: v1
kind: Service
metadata:
    name: mysql-db
spec:
    type: ExternalName
    externalName: database.example.com

We can create an ExternalName service with the above YAML file

In this example, externalName -> Will have the DB host – database.example.com and it will be resolved in the cluster DNS

Now, Whenever your web application wants to connect with an external Database, it can use the DNS name in the service-mysql-db and Kubernetes will redirect the traffic to database.example.com

By this way, ExternalName would help us to communicate with external DB

Commands to Know

We can create the service with the kubectl command

kubectl create -f service.yaml

To view the Service status and its definition

kubectl get svc

Conclusion

Kubernetes Services provides an abstract layer that helps us to expose the running application in the Kubernetes world. By setting the Service definition, It helps us to provide a stable IP address and DNS name for an application and allow it to distribute load across the pod.

It is essential to choose the right service based on the application type and its Use Case.

For reference check here

Check here for more Kubernetes topics

Check here for Python learning series articles

Good Luck with your Leanings !!

Similar Posts