In this lesson, you will learn how to configure Ingress resources to direct incoming web traffic to specific backend services using hostname filtering and path-based routing.
Once you have your Ingress Controller installed, the Ingress resource acts as a layer-7 traffic cop. Instead of exposing every individual service via a LoadBalancer (which can be expensive), you define a set of rules that tell the controller how to distribute requests based on the information inside the HTTP request.
Host-Based Routing
You can route traffic to different services based on the host header. For example, if you have a frontend service and an api service, you can direct traffic coming from app.example.com to the frontend and api.example.com to your backend.
Path-Based Routing
Within a single domain, you can further refine traffic by inspecting the URL path. A common pattern is routing /api traffic to one service while keeping / traffic on the main application.
Consider this manifest:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: myapp.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 8080
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
In this configuration, when a request hits the controller for myapp.com, the controller checks the path. If the request starts with /api, it is forwarded to the api-service. Any other traffic is sent to the web-service.
The pathType: Prefix ensures that the rule matches based on the path structure rather than an exact string match, which is ideal for routing subdirectories. By combining hostnames and path patterns, you can host dozens of microservices behind a single public-facing IP address, maintaining clean architecture while optimizing your resource footprint.
Key Takeaway: Ingress rules allow you to consolidate traffic management by routing requests to specific backend services based on unique hostnames and URL path patterns.