How does k8s service route the traffic to mulitiple endpoints
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
Kubernetes (K8s) offers a robust service discovery mechanism that manages the intricate task of routing traffic to a group of backend pods. The key component responsible for this task is the Kubernetes Service. Services abstract the complexities of networking within a cluster by providing a stable endpoint (a virtual IP) that clients can use to communicate with the pods, even as these pods scale or relocate on different nodes.
Understanding Kubernetes Services
There are several types of Kubernetes Services:
- ClusterIP (default): Exposes the service on an internal IP within the cluster, making it accessible only from within.
- NodePort: Exposes the service on each Node's IP at a static port, enabling external accessibility.
- LoadBalancer: Provisions a cloud-based load balancer to expose the service externally.
- ExternalName: Maps a service to an external DNS name.
This article will focus primarily on the ClusterIP
type, which is integral to the internal traffic routing within a K8s cluster.
DNS Service Discovery
Kubernetes automatically assigns a DNS name to each service, facilitating service discovery. For instance, if a service named my-service
exists within the namespace my-namespace
, it can be resolved via DNS as my-service.my-namespace.svc.cluster.local
.
Traffic Routing Mechanism
Endpoints
Kubernetes Services direct traffic to a dynamic set of backend pods through endpoints. An endpoint specifies the IPs and ports of pods associated with a service. The endpoint object is automatically updated in response to pod lifecycle events (e.g., creation, deletion) facilitated via the selectors
.
Selectors
Selectors are labels that specify the pod set a service targets. Here's an example of how you might define a service for a set of labeled pods:
- protocol: TCP
- Performance: Built on the Linux Kernel's networking stack for higher efficiency.
- Load Balancing Algorithms: Supports multiple algorithms like RR (Round Robin), WRR (Weighted RR), LC (Least Connections), etc.
- Scalability: Can handle a larger number of concurrent connections efficiently.

