Skip to main content

Lab 8.2 - Path-based Routing

Lab Objectives​

By the end of this lab, you will be able to:

  • Configure path-based routing.
  • Route traffic to different applications based on the path.
  • Understand the different pathTypes.
  • Test routing with multiple paths.

Estimated Duration​

45-60 minutes

Prerequisites​

  • kubectl installed and configured.
  • Functional local Kubernetes cluster with Ingress Controller.
  • Knowledge of Ingress (Lab 8.1).

Part 1: Deploying Multiple Applications​

We will deploy two different applications to test path-based routing.

Step 1.1: Deploy the Frontend Application​

Create frontend-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 2
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
ports:
- port: 80
targetPort: 80

Step 1.2: Deploy the Backend Application​

Create backend-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
spec:
replicas: 2
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: httpd
image: httpd:2.4
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- port: 80
targetPort: 80

Apply the deployments:

kubectl apply -f frontend-deployment.yaml
kubectl apply -f backend-deployment.yaml

Part 2: Configuring Ingress with Path-based Routing​

Step 2.1: Create the Ingress with Multiple Paths​

Create ingress-path-based.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: path-based-ingress
spec:
ingressClassName: nginx
rules:
- host: app.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-service
port:
number: 80
- path: /api
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 80
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 80

Explanation:

  • /: Routes to the frontend (home page).
  • /api: Routes to the backend.
  • /backend: Alternative route to the backend.

Apply the Ingress:

kubectl apply -f ingress-path-based.yaml

Part 3: Customizing Applications for Testing​

To better visualize routing, let's customize the application responses.

Step 3.1: Customize the Frontend​

Create a ConfigMap for the frontend:

apiVersion: v1
kind: ConfigMap
metadata:
name: frontend-config
data:
index.html: |
<!DOCTYPE html>
<html>
<head><title>Frontend Application</title></head>
<body>
<h1>Frontend Application</h1>
<p>You are on the home page (/)</p>
<p><a href="/api">Access the API</a></p>
</body>
</html>

Apply and mount the ConfigMap in the frontend (modify the Deployment if necessary).

Step 3.2: Customize the Backend​

Create a ConfigMap for the backend:

apiVersion: v1
kind: ConfigMap
metadata:
name: backend-config
data:
index.html: |
<!DOCTYPE html>
<html>
<head><title>Backend API</title></head>
<body>
<h1>Backend API</h1>
<p>You are on the API (/api or /backend)</p>
<p>This is the backend service</p>
</body>
</html>

Part 4: Testing the Routing​

Step 4.1: Configure the Hostname​

Add app.local to your /etc/hosts file (or Windows equivalent) pointing to the Ingress Controller IP.

Step 4.2: Test the Different Paths​

Test the routing:

# Home page (should go to frontend)
curl http://app.local/

# API (should go to backend)
curl http://app.local/api

# Alternative backend (should go to backend)
curl http://app.local/backend

You should see different responses depending on the path.


Part 5: Understanding pathTypes​

pathType determines how paths are compared:

  • Exact: Exact path match.
  • Prefix: Prefix match (most common).
  • ImplementationSpecific: Depends on the Ingress Controller implementation.

Example with Exact​

- path: /api/v1
pathType: Exact
backend:
service:
name: api-v1-service
port:
number: 80

Part 6: Cleanup​

Delete the resources:

kubectl delete ingress path-based-ingress
kubectl delete -f frontend-deployment.yaml
kubectl delete -f backend-deployment.yaml
kubectl delete configmap frontend-config backend-config

Lab Summary​

In this lab, you configured path-based routing with Ingress. You learned how to route traffic to different applications based on the URL path, use different pathTypes, and test the routing.


Next Steps​

The next lab will show you how to configure TLS/SSL with cert-manager to secure HTTPS communications.

Lab 8.3: TLS/SSL with Cert-Manager


Lab created: December 2024