2

I have a frontend application built with React and backend on nodejs. Both have a separate Docker image and therefore a separate deployment on k8s (gce).

Each deployment has a corresponding k8s service, let's say fe-serice and be-service.

I am trying to setup an Ingress so that both services are exposed on a single domain in the following manner:

  • /api/* - are routed to be-service
  • everything else is routed to fe-service

Here is my yaml file:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: my-host
    http:
      paths:
      - path: /*
        backend:
          serviceName: fe-service
          servicePort: 80
      - path: /api/*
        backend:
          serviceName: be-service
          servicePort: 5000

Here is what I get with curl:

curl [ip] --header "Host: my-host" -> React app (as expected)

curl [ip]/foo --header "Host: my-host" -> nginx 404 (why?)

curl [ip]/api --header "Host: my-host" -> nginx 404 (why?)

curl [ip]/api/ --header "Host: my-host" -> nodejs app

curl [ip]/api/foo --header "Host: my-host" -> nodejs app

As far as I can see a part with api/ works fine, but I can't figure out everything else, I tried different combinations with/without wildcards, but it still does not work in the way I want it to work.

What am I missing? Is this even possible? Thanks in advance!

Andrey
  • 5,906
  • 4
  • 24
  • 30
  • As @doe1331 said the path /api/* covers calls to /api/ but not to /api Double check that /foo exist under fe-service, this could be the cause of the 404. – Pauloba Mar 08 '18 at 16:26
  • @Andrey did you find a solution? – stacksonstacks Aug 24 '18 at 00:44
  • @Andrey : I have the similar issue now with GKE ingress, have you solved the issue? – Santhosha Jul 17 '20 at 14:53
  • @Santhosha at that moment no, I didn't. But it was a while ago already :) Right now I use Traefik on a private cluster to do similar things and it works fine – Andrey Jul 18 '20 at 05:55
  • @Andrey: Thanks for the update, i am working on the similar topic using ingress-nginx / ingress-gce .. Just in case if you , have you used nginx-ingress controller any time on google kubernetes cluster? if yes, i wanted to know if you had to install it / how to enable it on the clusters – Santhosha Jul 18 '20 at 07:43

3 Answers3

2

I can't explain why /foo is not working

But

/api/* does not cover /api, it covers only anything after /api/

doe1331
  • 181
  • 1
  • 5
1

I don't think the issue here is your ingress, but rather your nginx setup (without having seen it!). Since React apps are Single Page Applications, you need to tell the nginx server to always look for index.html instead of going to e.g. /usr/share/nginx/html/foo where there's probably nothing to find.

I think you'll find relevant information e.g. here. I wish you good luck @Andrey, and let me know if this was at all helpful!

Joel
  • 184
  • 1
  • 10
1

I hope am not late to respond. Please use kubernetes use-regrex annotations to ensure that it maps to your services endpoints. Please make sure that the react app service mapping is done at the end of the reset. Use the following yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - host: my-host
    http:
      paths:
      - path: /api/?(.*)
        backend:
          serviceName: be-service
          servicePort: 5000
       - path: /?(.*)
        backend:
          serviceName: fe-service
          servicePort: 80