Path-based routing allows a single domain to serve multiple apps based on the URL path โ e.g.,
myapp.com/ โ frontendmyapp.com/api โ backend APImyapp.com/docs โ documentation siteโ This is essential for microservices, monorepos, and cost-efficient hosting.
Like a shopping mall directory:
- โShoesโ โ Floor 1
- โElectronicsโ โ Floor 2
- Same entrance (
myapp.com), different destinations
Assume you have:
frontend Service (app: frontend)api Service (app: api)docs Service (app: docs)Step 1: Create an Ingress with Path Rules
# path-routing.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
meta
name: app-ingress
spec:
rules:
- host: myapp.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend
port:
number: 80
- path: /api
pathType: Prefix
backend:
service:
name: api
port:
number: 80
- path: /docs
pathType: Prefix
backend:
service:
name: docs
port:
number: 80
Apply it:
kubectl apply -f path-routing.yaml
Step 2: Test the Paths
# Get Ingress IP (Traefik in k3s)
INGRESS_IP=$(kubectl get svc -n kube-system traefik -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
# Add to /etc/hosts (for local testing)
echo "$INGRESS_IP myapp.com" | sudo tee -a /etc/hosts
# Test paths
curl <http://myapp.com/> # โ frontend
curl <http://myapp.com/api> # โ api
curl <http://myapp.com/docs> # โ docs