nginx-ingress path based angular application - kubernetes

I have an angular application which is dockerized using nginx-alpine as base image. My infra is hosted on AKS cluster version 1.18.14 . And nginx-ingress to route traffic to pods k8s.gcr.io/ingress-nginx/controller:v0.44.0 . Below is my Ingress
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /?(.*)
backend:
serviceName: portal
servicePort: 80
When I hit the application with ip-address/ application loads .
But If I want to add path to it with below nginx-ingress app doesn't load.
- path: /myportal(/|$)(.*)
backend:
serviceName: my-portal
servicePort: 80
WHen I hit ip-address/myportal app doesn't load. What changes should I make

It looks like there is a mistake in your yaml config.
It should be:
paths:
- backend:
serviceName: my-portal
servicePort: 80
path: /myportal(/|$)(.*)
instead of:
- path: /myportal(/|$)(.*)
backend:
serviceName: my-portal
servicePort: 80
Make sure the indentation are in place and syntax is correct. Use this example as a reference.

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.

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

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

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.

Kubernetes nginx ingress rewrite issue

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $2
spec:
rules:
- host: hostname.com
http:
paths:
- path: /
backend:
serviceName: frontend
servicePort: 80
- path: /api/v1(/|$)(.*)
backend:
serviceName: backend
servicePort: 80
What I am trying to accomplish here is:
hostname.com/api/v1/anyurl should become hostname.com/anyurl when it goes to the backend.
hostname.com/anyurl should remain hostname.com/anyurl and go to the frontend.
The /api/v1 rewrite seems to work, but any urls going to the frontend gets rewrited to /.
What I need is the rewrite rule to only apply to the /api/v1 path
I guess this should work for you -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $1
spec:
rules:
- host: hostname.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 80
- path: /api/(.*)
backend:
serviceName: backend
servicePort: 80
I have just edited this, it works for me, please check for this. I guess we can troubleshoot

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