Services in Kubernetes
Introduction#
We all know that Kubernetes has revolutionized container orchestration, making it easier to manage, scale, and deploy applications. However, one of the biggest challenges in Kubernetes is handling communication between different application components, especially since pods are ephemeral. This is where Kubernetes Services come into play.
Why Do We Need Services?#
Pods in Kubernetes are constantly created and destroyed due to scaling events, updates, or failures. This means their IP addresses are dynamic they keep on changing, this makes direct communication unreliable. Kubernetes Services provide a stable endpoint for communication, ensuring that requests are always routed to the appropriate pods.
A Kubernetes Service acts as an abstraction layer over a set of pods, allowing seamless communication between them. It provides critical functionalities such as:
- Load Balancing: Distributing traffic across multiple pod replicas.
- Service Discovery: Enabling pods to find and communicate with each other.
- Zero Downtime Deployments: Ensuring new pod instances can seamlessly replace old ones.
- External Exposure: Allowing applications to be accessed outside the cluster.
Understanding Kubernetes Service Ports#

A Kubernetes Service has three main port-related attributes:
- TargetPort: The port on the pod that the service forwards requests to.
- Port: The internal port within the Kubernetes cluster that the service listens on.
- NodePort: An optional port that exposes the service externally on each node.
For example:
Types of Kubernetes Services#
Kubernetes provides different types of services depending on how you want to expose your application.
1. ClusterIP (Default)#
A ClusterIP service is only accessible within the Kubernetes cluster. This is ideal for internal communication between different services.
2. NodePort#
A NodePort service exposes the application on a static port on each node. This allows external users to access the application via <NodeIP>:<NodePort>
.
3. LoadBalancer#
A LoadBalancer service integrates with cloud provider load balancers (AWS ELB, GCP LB, Azure LB) to distribute external traffic.
Managing Kubernetes Services#
Here are some essential kubectl
commands for managing services:
- List all services in the current namespace:
- Get detailed information about a specific service:
- Create a service from a deployment:
- Apply a service from a YAML file:
- Delete a service:
Service Discovery and DNS Resolution#
Kubernetes has a built-in DNS service (CoreDNS or kube-dns) that allows services to be discovered by name. Each service gets an internal DNS name in the format:
For example, if you have a service named my-app
in the default
namespace, pods can access it using:
Conclusion#
Kubernetes Services play a crucial role in enabling reliable communication between pods. By understanding the different service types and their use cases, you can design scalable and resilient Kubernetes applications. Whether you're exposing an internal microservice or making an application publicly accessible, Kubernetes Services provide the necessary abstraction and stability.