kubernetes Single service definition with multiple pod selectors
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction to Kubernetes
Kubernetes is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. At its core, Kubernetes provides a robust framework for running distributed systems resiliently. It takes care of scaling, failover, deployment patterns, and more, making it a preferred choice for managing production workloads in both cloud and on-premise environments.
Understanding Services in Kubernetes
In Kubernetes, a Service is a logical abstraction that represents a set of Pods, allowing these Pods to be accessed through a stable network endpoint. This enduring endpoint remains unchanged despite lifecycle changes of the Pods, facilitating seamless communication across microservices.
Key Components of a Service
- Label Selector: A filter mechanism that determines which Pods belong to the Service.
- ClusterIP: The internal IP of the Service, which remains constant throughout its life.
- Service Types: Define how the Service is exposed (e.g., ClusterIP, NodePort, LoadBalancer).
Single Service with Multiple Pod Selectors
Typically, a Service is associated with a single label selector to group Pods operating under a unified role or purpose. However, advanced use cases might necessitate a single Service to select multiple groups of Pods based on varying criteria. While Kubernetes does not natively support multiple selectors in a single Service specification, there are strategies to achieve similar outcomes.
Implementing Multiple Pod Selectors
Method 1: Using Multiple Services
One approach is to create multiple Services, each with its unique label selector. They can then target distinct groups of Pods. This method provides more flexibility and manages different traffic routes while maintaining a clean separation of responsibilities.
Example:
- protocol: TCP
- protocol: TCP
- host: myapp.example.com
- path: /prod
- path: /stage
- Affinity and Anti-affinity Policies: Provide fine-grained controls over how Pods are scheduled, ensuring optimal resource utilization and resilience.
- Network Policies: Enforce network segmentation and enhance security alongside Services.
Related reading
- Kubernetes size definitions What's the difference of Gi and G?
- kubernetes slaves cannot register to jenkins master
- Kubernetes sort pods by age
- Kubernetes spread pods across nodes using podAntiAffinity vs topologySpreadConstraints
- Kubernetes StatefulSet pod startup error recovery
- Kubernetes stop CloudSQL-proxy sidecar container in multi container Pod/Job
- Kubernetes support for Internal Load Balancers in AWS
- Kubernetes version installed by minikube

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.