Remember that Ingress rules take a while to propagate across the cluster, so if you receive an error initially when you curl the endpoint, wait for 5 minutes, and you should get the response back.
Let’s curl this IP and see what we get using the following command:
$ curl 34.120.27.34
Hi there! This page was last visited on 2023-06-26, 09:28:26.
Now, let’s clean up the Ingress resource using the following command:
$ kubectl delete ingress flask-app
The simple Ingress rule is counterproductive as it routes all traffic to a singleService resource. The idea of Ingress is to use a single load balancer to route traffic to multiple targets. Let’s look at two ways to do this—path-based and name-based routing.
Path-based routing
Let’s consider an application with two versions, v1 and v2, and you want both to co-exist on a single endpoint. You can use path-based routing for such a scenario.
Let’s create the two application versions first using the imperative method by running the following commands:
$ kubectl run nginx-v1 –image=bharamicrosystems/nginx:v1 $ kubectl run nginx-v2 –image=bharamicrosystems/nginx:v2
Now, expose the two pods as ClusterIP Service resources using the following commands:
$ kubectl expose pod nginx-v1 –port=80
$ kubectl expose pod nginx-v2 –port=80
We will then create an Ingress resource using the following manifest file, nginx-app-path-ingress.yaml, which will expose two endpoints—<external-ip>/v1, which routes to the v1 Service resource, and <external-ip>/v2, which routes to the v2 Service resource:
…
spec:
rules:
http:
paths:
path: /v1
pathType: Prefix backend:
service:
name: nginx-v1 port:
number: 80
path: /v2
pathType: Prefix backend:
service:
name: nginx-v2
port:
number: 80
The Ingress manifest contains several rules. The http rule has two paths—/v1 and /v2, having the pathType value set to Prefix. Therefore, any traffic arriving on a URL that starts with/v1 is routed to the nginx-v1 Service resource on port 80, and traffic arriving on/v2 is routed to the nginx-v2 Service resource on port 80.
Let’s apply the manifest by using the following command:
$ kubectl apply -f nginx-app-path-ingress.yaml
Now, let’s list the Ingress resources by running the following command:
$ kubectl get ingress nginx-app -w
NAME
CLASS
HOSTS
ADDRESS
PORTS
AGE
nginx-app
*
34.120.27.34
80
114s
Now that we have the external IP, we can curl both endpoints to see what we get using the following commands:
$ curl 34.120.27.34/v1/
This is version 1
$ curl 34.120.27.34/v2/
This is version 2
Sometimes, a path-based route is not always feasible, as you might not want your users to remember the path of multiple applications. However, you can still run multiple applications using a single Ingress endpoint—that is, by using name-based routing.