Creating an Ingress Resource with Default Backend
Learn to define and create an Ingress resource with a default backend.
Non-matching requests#
In some cases, we might want to define a default backend. We might want to forward requests that do not match any of the Ingress rules.
See the following example.
So far, we have two sets of Ingress rules in our cluster. One accepts all requests with the base path /demo
. The other forwards all requests coming from the devopstoolkitseries.com
domain. The request we just sent does not match either of those rules, so the response was once again 404 Not Found
.
Default backend Ingress resource#
Let’s first define and then create a default backend resource.
Looking into the definition#
Let’s imagine that it would be a good idea to forward all requests with the wrong domain to the devops-toolkit
application. Of course, by “wrong domain”, I mean one of the domains we own, and not one of those that are already included in Ingress rules.
Let us look at the definition of default-backend.yml
.
There’s no Deployment, nor is there a Service. This time, we’re creating only an Ingress resource.
The spec
has no rules, but only a single backend
.
When an Ingress
spec
is without rules, it is considered a default backend. As such, it will forward all requests that do not match paths and/or domains set as rules in the other Ingress resources.
We can use the default backend as a substitute for the default 404
pages or for any other occasion that is not covered by other rules.
You’ll notice that the serviceName
is devops-toolkit
. The example would be much better if we created a separate application for this purpose but it does not matter for this example.
All we want, at the moment, is to see something other than 404 Not Found
response.
Creating the resource#
We created the Ingress resource with the default backend, and now we can test whether it truly works.
This time, the output is different. We got 200 OK
instead of the 404 Not Found
response.
Destroying Everything#
Now that another chapter is finished, we’ll destroy the cluster and let your machine rest for a while. It deserves a break.
Try it yourself#
A list of all the commands used in the lesson is given below.
You can practice the commands in the following code playground by pressing the Run button and waiting for the cluster to set up.
/
- default-backend.yml