Rewriting issue with ingress nginx

4/5/2019

I am a newbie and trying to deploy the whole application with kubernetes locally using minikube and ingress-nginx. Facing below issue with rewriting

Below is my ingress-service.yml configuartion file

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: client-cluster-ip-service
              servicePort: 3000
          - path: /api/
            backend:
              serviceName: server-cluster-ip-service
              servicePort: 5000

My express routing code is

app.get('/', (req, res) => {
  res.send('index');
});

app.get('/values/all', async (req, res) => {
  res.send('all values');
});

app.get('/values/current', async (req, res) => {
  res.send('current values');
});

Through my react application for all the routes, it goes to index routes only.

What I need is, when my react application calls the route "/api/values/all", I should get the response from the corresponding route i.e.

app.get('/values/all', async (req, res) => {
  res.send('all values');
});

How do I fix this ???

-- user11315633
kubernetes

1 Answer

4/5/2019

Your rewrite-target term seems to be the issue. Take a look at the documentation on rewrite-target. Try creating 2 ingress resources:

Front.yaml:

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: client-cluster-ip-service
              servicePort: 3000

Back.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /api  
spec:
  rules:
    - http:
        paths:
          - path: /api/*
            backend:
              serviceName: server-cluster-ip-service
              servicePort: 5000
-- cookiedough
Source: StackOverflow