Kubernetes Services Creation — ClusterIP, NodePort and LoadBalancer

Mohammed NadeemMohammed Nadeem
3 min read

Kubernetes often abbreviated as K8s, is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. Containers are a lightweight and portable way to package and run applications and their dependencies, making it easier to develop and deploy software consistently across different environments.

Introduction

Overview of Kubernetes Services, explaining what they are, why they are essential, and detailing the different types of services available in Kubernetes.

What is a Kubernetes Service?

A Kubernetes Service is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable communication between various components within a Kubernetes cluster, providing a way to expose an application running on a set of pods as a network service.

Key Features of Kubernetes Services:

  • Stable IP Address: Services provide a stable IP address that remains constant despite changes to the underlying pods.

  • Load Balancing: Services automatically distribute traffic across the set of pods.

  • Service Discovery: Services help in discovering and communicating with the pods, even if their IP addresses change.

  • Decoupling: Services decouple the frontend from the backend, allowing for independent scaling and management.

Types of Kubernetes Services:

NodePort:

  • Exposes the service on each node’s IP at a static port.

  • The NodePort service routes traffic to the pods based on the port specified.

  • Accessible from outside the cluster by requesting <NodeIP>:<NodePort>.

  • NodePort Services expose the Service on a static port on each node in the cluster. This means that the Service can be accessed externally (from outside the cluster) by connecting to any node’s IP address at the specified port. NodePort Services are often used when you need to expose a Service to external clients.

ClusterIP:

  • The default type of service.

  • Exposes the service on a cluster-internal IP

  • Makes the service accessible only within the cluster [typically used for inter-service communication].

  • Used for internal communication between different parts of an application.

  • It provides a stable internal IP address within the cluster, allowing other Pods within the cluster to access the Service using this IP and a defined port.

LoadBalancer:

  • Creates an external load balancer that routes traffic to the service.

  • Requires a cloud provider to support the external load balancer.

  • Automatically creates a NodePort and ClusterIP service to which the external load balancer will route.

  • LoadBalancer Services are similar to NodePort Services, but they leverage cloud provider-specific load balancers (e.g., AWS ELB, GCP Load Balancer) to distribute incoming traffic across the nodes running the Pods of the Service. This is useful when you want to expose a Service to the public internet or distribute traffic across multiple nodes for high availability.

ExternalName:

  • Maps the service to a DNS name.

  • Allows the service to act as an alias to an external service outside the cluster.

  • Used for external services that need to be accessed by pods within the cluster.

Services are defined using Kubernetes manifests (YAML or JSON files) and can be created and managed using the kubectl command-line tool or by applying the manifests directly to the Kubernetes cluster.

Services work in conjunction with Labels and Selectors. Pods are labeled with key-value pairs, and Services use label selectors to determine which Pods to include in their load balancing or routing rules. This decouples the Service from the individual Pod IPs and allows Pods to come and go while maintaining consistent access to the Service.

In summary, Kubernetes Services provide a way to expose and manage network connectivity to sets of Pods within a cluster, enabling reliable communication between different parts of your application and allowing external clients to access your services. They are a crucial component for building scalable and resilient microservices architectures on Kubernetes.

0
Subscribe to my newsletter

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

Written by

Mohammed Nadeem
Mohammed Nadeem

Cloud & DevOps Engineer | Azure | Azure DevOps | Kubernetes | Docker | GitHub Actions | Terraform | Jenkins | Azure Certified