ClusterIP VS NodePort

ClusterIP VS NodePort

Kubernetes is a popular container orchestration system that allows you to deploy and manage containerized applications at scale. When deploying services in Kubernetes, you have the option to expose them with either a NodePort or ClusterIP service.

In this blog, we will explore the differences between NodePort and ClusterIP services, and when you might want to use one over the other. We’ll start by explaining what NodePort and ClusterIP services are, and how they work. Then, we’ll provide examples of when you might use each type of service, and how to create them in Kubernetes using YAML files.

By the end of this blog, you should have a clear understanding of the differences between NodePort and ClusterIP services, and be able to choose the right type of service for your needs when deploying services in Kubernetes.

What is a ClusterIP?

A ClusterIP service creates a virtual IP address (clusterIP) that is only accessible from within the cluster. This is the default type of service that is created when you don’t specify a type. With a ClusterIP service, you can reach your service from within the cluster by using the clusterIP and the service’s port. This is useful if you have multiple pods running the same service and you want to load balance traffic between them.

What is a NodePort?

A NodePort service, on the other hand, exposes the service on a static port on each node in the cluster. This means that you can access the service from outside the cluster by hitting any node’s IP address on the specified port. NodePort services are typically used for development and testing purposes, as well as for services that need to be accessible from outside the cluster.

When deploying a service in Kubernetes, you have the option to expose it with either a NodePort or ClusterIP.

Examples

Let’s take a look at some examples to better understand the differences between NodePort and ClusterIP services.

ClusterIP Example: Suppose you have a deployment with multiple replicas of a web server. You want to create a service that load balances traffic between these replicas. You can create a ClusterIP service for this deployment with the following YAML file:

apiVersion: v1
kind: Service
metadata:
  name: my-web-server
spec:
  selector:
    app: my-web-server
  ports:
  - name: http
    port: 80
    targetPort: 8080

This creates a ClusterIP service called my-web-server that listens on port 80 and routes traffic to the pods labelled with app: my-web-server. You can access this service from within the cluster by using clusterIP and port 80.

NodePort Example: Suppose you have a web server running on port 8080 that you want to expose to the outside world. You can create a NodePort service for this web server with the following YAML file:

apiVersion: v1
kind: Service
metadata:
  name: my-web-server
spec:
  type: NodePort
  selector:
    app: my-web-server
  ports:
  - name: http
    port: 80
    targetPort: 8080
    nodePort: 30080

This creates a NodePort service called my-web-server that listens on port 80 and routes traffic to the pods labelled with app: my-web-server. The service is exposed on port 30080 on each node in the cluster, so you can access it from outside the cluster by hitting any node’s IP address on port 30080.

Conclusion

NodePort services are a way to expose a Kubernetes service to the outside world, by exposing the service on a static port on each node in the cluster. This is useful for development and testing, as well as for services that need to be accessible from outside the cluster.

ClusterIP services, on the other hand, create a virtual IP address (clusterIP) that is only accessible from within the cluster. These services are used to load balance traffic between pods within the cluster, and are typically used for services that need to be accessed only within the cluster.

In summary, NodePort services are used for external access, while ClusterIP services are used for internal load balancing.