我有两个服务
服务老
服务新

我想达到以下效果

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


我尝试了多种方法,但未能实现目标。谁能帮我?

最佳答案

您可以在Ingress Rewrite Docs中找到有关重写annotationpath的所有信息。

根据网站上的示例:

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(/|$)(.*)

它将重定向:
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

在您的情况下,它应类似于:
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

关于nginx - 入口多径不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61078357/

10-15 21:48