Ingress routing to different services and with different paths to same service - kubernetes

I have the following ingress config:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /
pathType: Exact
backend:
serviceName: web-service
servicePort: 5000
paths:
- path: /api/tasks/.*
pathType: Prefix
backend:
serviceName: tasks-service
servicePort: 5004
paths:
- path: /api/.*
pathType: Prefix
backend:
serviceName: um-service
servicePort: 5001
I'm intending to load the frontend by default then, use the other paths for loading other services. I would like to get the total tasks count from /api/tasks/total_count and raise new task from /api/tasks/raise. At the same time, I would like to login using /api/auth/login/ and view other users with /api/users/list both served with the um-service.
The above configuration only returns the default path of the last service which is the um-service.
How do I configure so that web loads by default, then either /api/auth/login or /api/users/list are routed to the um-service and /api/tasks/ is also routed to the tasks service? Kindly advice

If I understand you correctly, you want to achieve that result:
$ curl <MY_DOMAIN>/api/auth/login
um-service
$ curl <MY_DOMAIN>/api/users/list
um-service
$ curl <MY_DOMAIN>/api/tasks/
tasks-service
$ curl <MY_DOMAIN>/
web-service
You almost did everything right, but paths should only be given once
Try this configuration:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: web-service
servicePort: 5000
- path: /api/tasks/.*
backend:
serviceName: tasks-service
servicePort: 5004
- path: /api/.*
backend:
serviceName: um-service
servicePort: 5001

Related

Ingress making react app not to route correctly

I have a react app that works fine. However after putting it in k8s and installing ingress, whenever I refresh a page i get the following although it just refreshed the page. How do I correct this?
Here is a snippet of my yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- http:
paths:
- path: /api/auth/*
backend:
serviceName: um-service
servicePort: 5001
- path: /api/profile/*
backend:
serviceName: um-service
servicePort: 5001
- path: /api/pass/*
backend:
serviceName: um-service
servicePort: 5001
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: admin-ingress
annotations:
kubernetes.io/ingress.class: nginx
# nginx.ingress.kubernetes.io/use-regex: "true"
# nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /admin
# pathType: Exact
backend:
serviceName: admin-service
servicePort: 4000
I must say I have challenges hitting the admin backend. It just doesn't load i.

not able to load static content in ingress resource

I am facing a wierd issue... I am trying to invoke active mq webui using ingress resources... page is loading but static content is not loading and it is saying not found....
Here is my ingress resource.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- http:
paths:
- path: /(.*)\.(jpg|jpeg|png|gif|ico|js|css|csv|json)$
backend:
serviceName: nginx-static
servicePort: 80
- path: "/"
backend:
serviceName: service1
servicePort: 8233
- path: "/test"
backend:
serviceName: server2
servicePort: 9002
http:
paths:
- path: "/admin"
backend:
serviceName: activemq-server
servicePort: 80
Kindly help how static content will be loaded....
Add to your ingress configuration file following annotations:
nginx.ingress.kubernetes.io/add-base-url: "true"
nginx.ingress.kubernetes.io/rewrite-target: "specify rewriting rule here"
Similar problem you can find here: ingress-static-contetnt.

Route traffic using ingress

I had a working example of a project a year back, which is not working anymore.
It's basically related to change in the behavior of nginx.ingress.kubernetes.io/rewrite-target property mentioned here - https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite
I have 3 application and I want to route based on conditions.
/* to frontend-cluster-ip-service
/api/battleship/* to battleship-cluster-ip-service
/api/connect4/* to connect-four-cluster-ip-service
The working example that was working an year back was
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: frontend-cluster-ip-service
servicePort: 3000
- path: /api/connect4/
backend:
serviceName: connect-four-cluster-ip-service
servicePort: 8080
- path: /api/battleship/
backend:
serviceName: battleship-cluster-ip-service
servicePort: 8080
However, this is not working anymore and only routing to / , i.e to frontend-cluster-ip-service is working. Routing to other serives fails and I get 404.
Then I came to know about the change in nginx.ingress.kubernetes.io/rewrite-target.
I tried following then
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: frontend-cluster-ip-service
servicePort: 3000
- path: /api/connect4(/|$)(.*)
backend:
serviceName: connect-four-cluster-ip-service
servicePort: 8080
- path: /api/battleship(/|$)(.*)
backend:
serviceName: battleship-cluster-ip-service
servicePort: 8080
Now the routing to connect-four-cluster-ip-service and battleship-cluster-ip-service is working but frontend-cluster-ip-service is not working and few js files loads are showing error:
I had the same issue with a bit more complicated rewrite (it was only for one different path).
Making multiple Ingresses for each path worked for me but might not be the cleanest solution.
My ingress definition:
https://github.com/FORTH-ICS-INSPIRE/artemis/blob/master/artemis-chart/templates/ingresses.yaml

Rewriting Kubernetes services in an Nginx ingress

I have multiple APIs all listening on '/api' and a web front end listening on '/'.
Is there a way which I can write my ingress definition to rewrite the paths to something like the following?
/api/ -> /api/ on service1
/api2/api/ -> /api/ on service2
/api3/api/ -> /api/ on service3
/ -> / on service4
I know I can change the APIs to listen to something else but don't want to do that. I know I can also just rewrite all to /api/ and let service3 act as the default but there may be other services which need routing elsewhere in the future.
I've heard that you could use multiple ingresses but I'm not sure how that would affect performance and if it's best practice to do so.
Also, is there any way to debug which route goes to which service?
Thanks,
James
With help from #Rahman - see other answer. I've managed to get this to work with a single ingress.
I've had to post this as an additional answer because of the character limit.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-name
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- secretName: tls-secret
rules:
- host: localhost
http:
paths:
- path: /(api/.*)
backend:
serviceName: service1
servicePort: 80
- path: /api2/(api.*)
backend:
serviceName: service2
servicePort: 80
- path: /api3/(api.*)
backend:
serviceName: service3
servicePort: 80
- path: /(.*)
backend:
serviceName: service4
servicePort: 80
Just for context for anyone else stumbling upon this in the future, service 1 is a main API, service 2 and 3 are other APIs under another subdomain and service 4 is a web frontend.
If you are using Nginx, you should be able to configure your Ingress for path matching like this:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: test.com
http:
paths:
- path: (/api/.*)
backend:
serviceName: service1
servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress-2
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: test.com
http:
paths:
- path: /api2/.*
backend:
serviceName: service2
servicePort: 80
- path: /api3/.*
backend:
serviceName: service3
servicePort: 80
- path: /.*
backend:
serviceName: service4
servicePort: 80
More info

Dynamic redirect using ingress

I have 2 questions:
1) I have a kubernetes cluster with multiple services and I want to use ingress to dynamically redirect the traffic to the cluster.
I expect the configuration to look something like this:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /service1/*
backend:
serviceName: service1
servicePort: 80
path: /*
- path: /service2/*
backend:
serviceName: service2
servicePort: 80
path:/*
So basically I want all the traffic to /service1/endpoint to be redirected to s1:80/endpoint dynamically.
2) Let's say I have 2 web services - service1 & service2.
I want the users to work with the following URL in their browser:
kube/serviceN/endpoint
Is there a way to do that without having my users redirected to service1/endpoint?
Thanks!
I believe your ingress definition to be almost correct:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /service1
backend:
serviceName: service1
servicePort: 80
- path: /service2
backend:
serviceName: service2
servicePort: 80
This should work if you have an ingress correctly deployed!
I hope I have understood your question properly but if so, what you have provided as an example is pretty close to the mark. The below config should work as described.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /service1/
backend:
serviceName: service1
servicePort: 80
- path: /service2/
backend:
serviceName: service2
servicePort: 80
Good luck :)