Skip to main content

Services & Networking

How to Configure Path-Based Routing with Kubernetes Ingress

The DevOpscube team is deploying an e-commerce application composed of multiple microservices. Each service handles a different part of the application and must be accessible through a single hostname using URL path-based routing.

The application resources have already been defined in a manifest and will be deployed to the ecommerce namespace.

Your task is to deploy the provided e-commerce application and configure an Ingress resource to route requests to the appropriate backend services.

Use the following command to apply the manifest:

$ k apply -f https://raw.githubusercontent.com/techiescamp/cka-certification-guide/refs/heads/main/ingress/scenarios/ecommerce.yaml

The application has three services:

  • homepage available at /
  • product available at /products
  • user available at /users

Create an Ingress for the host to ecommerce.devopscube.com

Configure path-based routing so that requests to each path are forwarded to the corresponding Service in the ecommerce namespace.

After configuring the Ingress, verify that:

  • The application Deployments and Services are running.
  • The Ingress resource has been created successfully.
  • Requests to / reach the homepage service.
  • Requests to /products reach the product service.
  • Requests to /users reach the user service.
  • The Ingress controller successfully routes traffic for ecommerce.devopscube.com.

Use the Ingress controller's ClusterIP together with curl and DNS/Host-header simulation to test each route.

🤔
If you don't already have an NGINX Ingress controller installed in the cluster, install one before running the connectivity tests.
$ k apply -f https://raw.githubusercontent.com/techiescamp/cka-certification-guide/main/ingress/nginx-controller.yaml
💡
For more details on Ingress, see the official Kubernetes documentation.

Deploy the E-Commerce Application

Apply the provided manifest to create the application resources in the ecommerce namespace:

kubectl apply -f https://raw.githubusercontent.com/techiescamp/cka-certification-guide/refs/heads/main/ingress/scenarios/ecommerce.yaml

The manifest creates the required resources in the ecommerce namespace.

Verify the Deployments and Services:

$ kubectl get deployments,services -n ecommerce

NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/frontend-deployment   1/1     1            1           13s
deployment.apps/products-deployment   1/1     1            1           13s
deployment.apps/users-deployment      1/1     1            1           13s

NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/frontend-service   ClusterIP   10.98.143.65    <none>        80/TCP    13s
service/products-service   ClusterIP   10.111.17.132   <none>        80/TCP    13s
service/users-service      ClusterIP   10.102.37.29    <none>        80/TCP    13s

You should see 3 Deployments and their corresponding ClusterIP Services.

You can also check the Pods:

$ kubectl get pods -n ecommerce

NAME                                   READY   STATUS    RESTARTS   AGE
frontend-deployment-755b8f8788-m8zbv   1/1     Running   0          58s
products-deployment-74d69fdc96-bndzv   1/1     Running   0          58s
users-deployment-58444dd46-qmmhq       1/1     Running   0          58s

Make sure all application Pods are in the Running state before testing the Ingress.

Ensure the NGINX Ingress Controller Is Installed

Check whether the Ingress Controller is already installed:

$ kubectl get pods -n ingress-nginx

ingress-nginx-controller-5b8f94dbf8-gzgzp   1/1     Running     0             56m

If it is not installed, apply the provided manifest:

kubectl apply -f https://raw.githubusercontent.com/techiescamp/cka-certification-guide/main/ingress/nginx-controller.yaml

Create the Ingress Resource

Create a file named ecommerce-ingress.yaml and configure path-based routing:

e-commerce-ingress.yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: e-commerce-ingress namespace: ecommerce spec: ingressClassName: nginx rules: - host: ecommerce.devopscube.com http: paths: - path: / pathType: Prefix backend: service: name: frontend-service port: number: 80 - path: /products pathType: Prefix backend: service: name: products-service port: number: 80 - path: /users pathType: Prefix backend: service: name: users-service port: number: 80

With pathType: Prefix, Kubernetes matches requests based on the beginning of the URL path.

Apply the Ingress:

$ kubectl apply -f ecommerce-ingress.yaml

ingress.networking.k8s.io/e-commerce-ingress created

Verify the Ingress

Check the Ingress resource:

$ kubectl get ingress -n ecommerce

NAME                 CLASS   HOSTS                      ADDRESS          PORTS   AGE
e-commerce-ingress   nginx   ecommerce.devopscube.com   10.102.157.150   80      2m43s

Verify that the expected rules and backend Services are listed.

Test Path-Based Routing

Because the Ingress uses the host: ecommerce.devopscube.com

your request needs to include that hostname.

First, get the NGINX Ingress Controller Service:

$ kubectl get service -n ingress-nginx

kubectl get service -n ingress-nginx
NAME                                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort    10.102.157.150   <none>        80:32000/TCP,443:32001/TCP   73m
ingress-nginx-controller-admission   ClusterIP   10.96.131.202    <none>        443/TCP 

Find the ClusterIP of the Ingress Controller Service.

Use the ClusterIP with the Host header to simulate DNS resolution.

Test the Homepage

$  curl -H "Host: ecommerce.devopscube.com" \
  http://10.102.157.150/
  
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to the E-commerce Application Homepage</h1>
</body>
</html>

The request should be routed to frontend-service.

Test the Products Path

$ curl -H "Host: ecommerce.devopscube.com" \
  http://10.102.157.150/products
  
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to the Products Page</h1>
</body>
</html>

This should be routed to products-service.

Test the Users Path

$ curl -H "Host: ecommerce.devopscube.com" \
  http://10.102.157.150/users
  
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to the Users Page</h1>
</body>
</html>

This should be routed to users-service.

Replace 10.102.157.150 with the actual ClusterIP of the NGINX Ingress Controller Service in your cluster.

Why the Host Header Is Needed

The Ingress rule is configured for the hostname ecommerce.devopscube.com. When you send a request directly to the Ingress Controller's ClusterIP, the IP address does not match this host rule.

By adding the Host header, you tell the Ingress Controller which hostname you are requesting, allowing you to test host-based and path-based routing without configuring DNS.

CKA Exam Tips

These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.

1. Use the Stable Ingress API

Use the stable Ingress API:

apiVersion: networking.k8s.io/v1

2. Make Sure the Ingress Controller Is Installed

The Ingress Controller must be installed. Creating an Ingress resource alone does not route traffic.

3. Verify Path-Based Routing

For path-based routing, verify both the path and backend Service:

/          → frontend-service
/products  → products-service
/users     → users-service

4. Use the Correct Host Header

When an Ingress uses a hostname, testing with the controller IP requires the appropriate Host header.

5. Use kubectl describe ingress

kubectl describe ingress is useful during the exam for quickly checking the configured hosts, paths, and backend Services.