Understanding kubernetes services: clusterIP, nodeport, and loadbalancer

Divyanshi SinghDivyanshi Singh
4 min read

In Kubernetes, services play an imp role in making sure your application is accessible, manageable, and scalable. Service ensures that your application components (pods) can be accessed reliably, without changes in their lifecycle. In this article, we'll dive into kubernetes services, their different types, and when to use them.

Why Kubernetes Services?

Kubernetes services come into play to solve two main problems:

  1. Service Discovery – Automatically detect and route traffic to the right pods, even if their IP addresses change.

  2. Load Balancing – Distribute traffic among multiple pods efficiently.

Pods in Kubernetes are ephemeral in nature, meaning they can be created and destroyed frequently. Without a service, we have to track their constantly changing IP addresses manually—a tedious and error-prone task. kubernetes services handle this by abstracting the pods behind a single, stable IP.

Kubernetes services not only help route traffic internally but can also expose your application to the outside world. Depending on your use case, you can use one of the three types of services: ClusterIP, NodePort, or LoadBalancer.

Types of Kubernetes Services

1. ClusterIP (Default)

The ClusterIP service is the default service type in kubernetes. This type exposes your application only within the kubernetes cluster.

  • Usage: When you want your application accessible only by other services or pods inside your cluster.

  • Access: No external access. The service creates an internal IP, making it available only to applications running in the same cluster.

This service still provides service discovery and load balancing, meaning traffic can be distributed evenly in multiple pods.

Example: Microservices within a cluster communicating with each other.

2. NodePort

The NodePort service type opens a specific port on every node in the kubernetes cluster, allowing external access to your application. However, access is restricted to users who know the IP of the node and the port number.

  • Usage: When you need to expose the application internally, but outside of kubernetes. It allows access to users within your organization's network.

  • Access: Only people who have access to the nodes’ IP addresses and ports can access the application.

Example: A development or staging environment within an organization where employees or teams need access to the application.

3. LoadBalancer

The LoadBalancer service type provides a publicly accessible IP address for your application. It’s integrated with cloud service providers like AWS, GCP, or Azure, which create a cloud-based load balancer (such as AWS's Elastic Load Balancer) and assign a public IP address.

  • Usage: When you want to expose your application to the outside world.

  • Access: Anyone with access to the public IP can reach your application.

The cloud provider's load balancer then distributes traffic across your pods, ensuring both reliability and high availability.

Example: A public-facing web application like amazon, which must be accessible to everyone over the internet.

How It Works: The Service Lifecycle

Let’s break down what happens when you deploy an application with different service types:

  1. ClusterIP:

    • The service acts as an internal proxy and load balancer, directing traffic to the appropriate pod.

    • Your end users won’t be able to access this from the outside.

  2. NodePort:

    • The service opens a static port on each node in the cluster.

    • If users know the node’s IP and the node port, they can access the application.

  3. LoadBalancer:

    • The service interacts with the cloud provider’s load balancer to assign a public IP address to the application.

    • Traffic is routed through this public IP to the service, which then forwards it to the pods.

Visualizing the Access Flow

  1. ClusterIP:

    • Internal users only: Your application is accessible within the cluster.
  2. NodePort:

    • Users with node access: If the user has access to the IP of any node in the cluster and knows the node port, they can access the application.
  3. LoadBalancer:

    • Public access: Anyone with internet access can reach the application via the cloud provider's load balancer.

Kubernetes services provide flexibility to expose your application, whether you’re dealing with internal microservices or want to make your application publicly accessible. The type of service you choose depends on the level of accessibility you want—whether it's internal cluster communication (ClusterIP), limited internal network access (NodePort), or full public exposure (LoadBalancer).

Understanding these service types and their use cases will help you better manage traffic within your kubernetes cluster. Sums up my day 5 of kubernetes series:).

0
Subscribe to my newsletter

Read articles from Divyanshi Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Divyanshi Singh
Divyanshi Singh