Redirect trafic from one app (workload) to other if there is prefix?

3/31/2021

I have two apps (workloads): app1 and app2. My goal is to redirect trafic from app1 to app2 if there is the diona prefix. For example:

app1/diona > app2/<api>

Now I have such rules:

rules:
  - host: host
    http:
      paths:
      - backend:
          serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
          servicePort: 80
        path: /diona

When I check app1/diona endpoint in logs of app2 there is an error:

Not Found: /diona

How can I redirect trafic correctly without realizing diona prefix in app2?

-- Альберт Александров
kubernetes
kubernetes-ingress

1 Answer

3/31/2021

You can achieve it using nginx ingress controller. You code will look like this

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: rewrite
spec:
  rules:
    - host: host
      http:
        paths:
        - backend:
            serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
            servicePort: 80
          path: /diona

If you want to redirect app1/diona/* to app2/*, change annotations to

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: rewrite
spec:
  rules:
    - host: host
      http:
        paths:
        - backend:
            serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
            servicePort: 80
          path: /diona/(.*)

Reference

-- vishalsaugat
Source: StackOverflow