Kubernetes ingress conditional routing
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Kubernetes has revolutionized the way we deploy, manage, and scale containerized applications. One of the key features that makes Kubernetes a robust choice for cloud-native applications is its networking capabilities, particularly the Ingress Controller. Within this framework, conditional routing allows for sophisticated traffic management, enabling users to dynamically route requests based on specific criteria. In this article, we delve into Kubernetes Ingress with an emphasis on conditional routing, detailing how it operates, and providing examples along with a useful summarization table.
Understanding Kubernetes Ingress
In Kubernetes, an Ingress is an API object that manages external access to the services within a cluster, typically HTTP. It provides the ability to route traffic to specific services based on the request's URL path, host header, or other HTTP attributes.
Components of Kubernetes Ingress
- Ingress Resource: This is a collection of rules that allow inbound connections to reach the cluster services. It provides HTTP and HTTPS routing to backend services based on defined criteria.
- Ingress Controller: A daemon that implements the Ingress resource configuration by listening for changes and updating a proxy (such as NGINX or HAProxy) that routes traffic into the cluster.
Conditional Routing with Ingress
Conditional routing is the practice of directing HTTP requests to different backends based on specific request attributes. This mechanism allows for granular control over which application version or microservice processes a request.
Conditional Routing Techniques
- Path-Based Routing: Routes traffic to backend services according to the requested URL path. For instance, `/api/` could route to one service, while `/static/` routes to another.
- Host-Based Routing: Different domains or subdomains might be served by distinct services. For example, `api.example.com` could route to a separate service than `www.example.com`.
- Header-Based Routing: Using custom HTTP headers to route traffic. This can be useful for A/B testing or to route traffic from specific user agents.
- Cookie-Based Routing: Utilizes cookies to direct traffic, often used in canary deployments where you want only a subset of users to see a new feature.
- Geographical Routing: Though less common in traditional Ingress, some advanced setups include geographical routing by integrating with external geographical databases.
Example Configuration
Below is an example of a Kubernetes Ingress configuration that demonstrates path-based and host-based conditional routing:
- host: www.example.com
- path: /api/
- path: /static/
- host: api.example.com
- path: /
- Traffic for `www.example.com/api/` is directed to `api-service`.
- Traffic for `www.example.com/static/` goes to `static-service`.
- Any request to `api.example.com/` is routed to `api-service`.
- Flexibility: Easily route traffic based on various attributes, allowing deployment strategies like blue/green deployments, canary releases, or A/B testing.
- Efficiency: Directs traffic without need for changing backend services, reducing latency and processing overhead.
- Scalability: Allows diverse applications or application components to run in the same cluster, promoting efficient resource usage.
- Ingress controllers might implement features differently, hence ensure compatibility and be equipped to access relevant documentation.
- Conditional routing often relies on additional backend and proxy configurations, requiring a deep understanding of the chosen Ingress controller.
- NGINX Ingress Controller: Highly configurable, supports path and header-based routing natively.
- HAProxy Ingress: Known for performance, highly flexible in terms of custom routing logic.
- Traefik: Provides a rich interface for dynamic configurations, integrating with external APIs for advanced routing.
- Istio: Though primarily a service mesh, it extends Ingress capability for more advanced routing.
Related reading
- Kubernetes Ingress controllers for wildcard url mapping
- Kubernetes Ingress GCE keeps returning 502 error
- Kubernetes Ingress network deny some paths
- Kubernetes ingress nginx redirect to https
- Kubernetes Ingress same with with master-slave architecture
- kubernetes ingress service annotations
- Kubernetes Ingress Path only works with /
- Kubernetes ingress rules How to use wildcard and specific subdomain together

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.