Kubernetes Ingress is an extremely flexible solution to expose services. In the previous post, we saw how to use Kubernetes Ingress to expose a Node Port service.
But, in that post, we focused on a single host and a single path.
However, with Kubernetes Ingress, you can configure multiple paths as well as multiple hosts.
Let’s look at both the options:
1 – Kubernetes Ingress Multiple Paths
Let’s say you have a high-level host test.example.path
.
Now, you want to have two separate paths /demo
and /admin
under that high-level host.
Both of these sub-paths are catered by different Kubernetes services.
Here’s how you can do it using a Kubernetes Ingress.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-demo
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: test.example.com
http:
paths:
- path: /demo
pathType: Prefix
backend:
service:
name: nodeport-demo
port:
number: 80
- path: /admin
pathType: Prefix
backend:
service:
name: admin-demo
port:
number: 80
Basically, the paths
section within the Ingress resource can take multiple individual path
sections.
In the above example, you define one path section for /demo
and the other for /admin
. Both of these paths are backed by different Kubernetes services.
In other words, clients can now reach two different services through a single IP address belonging to the Ingress Controller.
By the way, if you love Kubernetes and the Cloud Native world, you can also subscribe to the Progressive Coder newsletter where I explain software concepts in a fun & interesting way. You’d love it.
2 – Kubernetes Ingress Multiple Hosts
The second option Kubernetes Ingress provides is the ability to route to multiple hosts.
Think of it as routing to multiple domains.
Let’s say you have two domains test.example.com
and demo.example.com
.
Here’s how you can setup a Kubernetes Ingress resource to map to different services based on the host in the HTTP request.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-demo
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: test.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test
port:
number: 80
- host: demo.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: demo
port:
number: 80
Basically, this is made possible because the rules
section can contain a list of host
sections.
Each host
section takes care of a particular host value. In other words, requests received by the Ingress controller are forwarded to either test
or demo
service depending on the Host
header in the request.
Of course, at the DNS level, both test.example.com
and demo.example.com
should point to the IP address of the Ingress Controller.
Conclusion
That’s all for this post.
You now know two different ways of utilizing Kubernetes Ingress resources:
- With multiple paths
- With multiple hosts
In fact, you can achieve more flexibility by playing around with these configuration settings depending on your exact requirement.
Anyways, before we end this post, a quick reminder about the Progressive Code Newsletter where I explain System Design & Cloud concepts in a fun & interesting manner so that you never forget what you’ve learned.
I’m 100% sure you’d love it.
Subscribe now and see you over there.
0 Comments