Andrey
Andrey

Reputation: 6194

How do you setup k8s ingress for React app + Express on single domain?

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:

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!

Upvotes: 2

Views: 4714

Answers (3)

Omambia M. Dauglous
Omambia M. Dauglous

Reputation: 21

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

Upvotes: 1

Joel
Joel

Reputation: 184

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!

Upvotes: 1

doe1331
doe1331

Reputation: 181

I can't explain why /foo is not working

But

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

Upvotes: 2

Related Questions