kubernetes ingress with multiple target-rewrite

对着背影说爱祢 提交于 2019-12-06 02:33:59

问题


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.


回答1:


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 /coffee/).

/coffee/ -> /beans/
/coffee/abc -> /beans/abc


来源:https://stackoverflow.com/questions/49514702/kubernetes-ingress-with-multiple-target-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!