ingress multiple path mismatch

4/7/2020

I have two service serviceOld serviceNew

I want to achieve the following effect

http://host/any         => http://serviceOld/any
http://host/any/aaa     => http://serviceOld/any/aaa
http://host/feature     => http://serviceNew/feature
http://host/feature/bbb => http://serviceNew/feature/bbb
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-by-header: new
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: v2
  namespace: api
spec:
  rules:
  - host: xxx.com
    http:
      paths:
      - path: /
        backend:
          serviceName: serviceOld
          servicePort: 80
      - path: /feature/*
        backend:
          serviceName: serviceNew
          servicePort: 8080

I tried multiple methods and failed to achieve the goal. Can anyone help me?

-- Wang Yang
kubernetes
nginx

2 Answers

4/19/2020

I found that my version is relatively old. Under the old version, if the ports are different, it will not work properly. Change to the same port or upgrade the ingress version

-- Wang Yang
Source: StackOverflow

4/7/2020

All you need about rewrite annotation and path can be found in Ingress Rewrite Docs.

As per example on the site:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)

It will redirect:

rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new

In your case it should looks like:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: v2
  namespace: api
spec:
  rules:
  - host: xxx.com
    http:
      paths:
      - path: /any(/|$)(.*)
        backend:
          serviceName: serviceOld
          servicePort: 80
      - path: /feature(/|$)(.*)
        backend:
          serviceName: serviceNew
          servicePort: 8080
-- PjoterS
Source: StackOverflow