In this lesson, you will learn how to direct incoming HTTP traffic to different microservices by evaluating the URL path, allowing you to host multiple applications behind a single entry point.
The Concept of Path-Based Routing
In a microservices architecture, you rarely want to deploy a separate Load Balancer for every service. Instead, you use an Ingress controller to act as a "smart traffic cop." By defining path-based rules, the controller inspects the request URI and forwards it to the appropriate backend service. For instance, you can route /api to your backend service and /shop to your frontend service, even if they are entirely different pods in your cluster.
Configuring the Ingress Resource
To implement this, you define an Ingress resource with a list of paths. The controller uses these paths to match against the request URL.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: main-ingress
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 8080
- path: /shop
pathType: Prefix
backend:
service:
name: frontend-service
port:
number: 80
How it Works
When a request arrives at your-domain.com/api/users, the Ingress controller sees the /api prefix, matches it to the backend-service, and routes the traffic accordingly. The pathType: Prefix configuration is crucial here; it tells the controller to match the path based on the slash-separated prefix of the URL.
This approach is highly efficient for cluster resource management because it consolidates your public-facing entry points, simplifies DNS management, and allows you to add or modify routing rules without changing your infrastructure setup. By leveraging the Ingress controller's internal routing table, you ensure that requests are terminated and forwarded with minimal overhead, maintaining high performance while keeping your application architecture organized.
Key Takeaway: Path-based load balancing allows you to consolidate multiple microservices under a single entry point by routing traffic based on URL path prefixes defined in your Ingress manifest.