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

3/6/2018

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 a want it tщ work.

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

-- Andrey
docker
google-cloud-platform
kubernetes
kubernetes-ingress

2 Answers

6/7/2018

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
Source: StackOverflow

3/7/2018

I can't explain why /foo is not working

But

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

-- doe1331
Source: StackOverflow