Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes ingress with multiple target-rewrite

Usually ingress rewrite target works as follows:

nginx.ingress.kubernetes.io/rewrite-target: /

This will rewrite the target of your service names as they are in the root directory. So if I have this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: demo-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  rules:
    http:
      paths:
      - path: /
        backend:
          serviceName: front-main
          servicePort: 80
      - path: /api
        backend:
          serviceName: back-main
          servicePort: 80

My services are going to receive data as they are in /. However, I would like for my service front-main to send root / and for the server back-main to send /someotherpath/. How can I do this?

Is there something like the following line?

nginx.ingress.kubernetes.io/rewrite-target: "front-main: / ; back-main: /someotherpath"

I don't seem to find the answer in the documentation.

like image 848
silgon Avatar asked Mar 27 '18 13:03

silgon


3 Answers

Unfortunately, Ingress based on free version of Nginx do not have that feature.

But, if you can use Nginx Plus based Ingress, you can do it by annotation.

Here is an example from official repo:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea/
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee/
        backend:
          serviceName: coffee-svc
          servicePort: 80

Below are the examples of how the URI of requests to the tea-svc are rewritten (Note that the /tea requests are redirected to /tea/).

/tea/ -> /
/tea/abc -> /abc

Below are the examples of how the URI of requests to the coffee-svc are rewritten (Note that the /coffee requests are redirected to /beans/).

/coffee/ -> /beans/
/coffee/abc -> /beans/abc
like image 63
Anton Kostenko Avatar answered Oct 17 '22 08:10

Anton Kostenko


Another solution is create two ingress yaml files

Each one using different annotations. It works!

like image 32
Juan Enciso Avatar answered Oct 17 '22 06:10

Juan Enciso


The new syntax for the rewrite annotation allows you to use capture groups to define the rewrite-target, which can be used in some situations to achieve what you're looking for.

For instance, if you want one of your services to keep the matching url in its rewrite but not the other, you could use matching groups as follow:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: demo-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1$2  # Here we concatenate the two matched groups
spec:
  rules:
    - http:
        paths:
        - path: /front()(.*)  # Here the first matching group is always empty
          pathType: Prefix
          backend:
            service:
              name: front-main
              port:
                number: 80
        - path: /(back)(.*)  # Here the first matching group matches 'back'
          pathType: Prefix
          backend:
            service:
              name: back-main
              port:
                number: 80

So /back/foo would redirect to the back-service on /back/foo, but /front/foo would redirect to the front-service simply to /foo.

As far as I know, this can't be used for the more general case you asked for, like rewriting /back/foo to /some-completely-different-path.

like image 1
benterris Avatar answered Oct 17 '22 08:10

benterris