kubernetes
k8s services
traffic routing
service endpoints
cloud computing

How does k8s service route the traffic to mulitiple endpoints

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.