简体   繁体   English

流量的重新路由不适用于入口 controller。 我觉得我误解了重写注释?

[英]re-route of traffic is not working with ingress controller. I feel i mis-understood the re-write annotation?

Issue with re-write annotation.重写注释的问题。

Tried using re-write annotation.尝试使用重写注释。 Not able to get that work.无法得到那个工作。

apiVersion: networking.k8s.io/v1beta1 # for versions before 1.14 use extensions/v1beta1
kind: Ingress
metadata:
  name: hello-whale-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: hello.whale.info
    http:
      paths:
      - path: /blue|/(.+)
        backend:
          serviceName: hello-blue-whale-svc
          servicePort: 80
      - path: /green|/(.+)
        backend:
          serviceName: hello-green-whale-svc
          servicePort: 80

/Green is showing perfect result, but why not /blue. /Green 显示出完美的结果,但为什么不是 /blue。

You should see in your logs that the ingress always hits the same backend.您应该在日志中看到入口总是命中相同的后端。 Maybe this doc may help you.也许这个文档可以帮助你。

I tested myself with two nginx servers as backends, one returning blue and the other green:我用两台 nginx 服务器作为后端对自己进行了测试,一台返回蓝色,另一台返回绿色:

apiVersion: networking.k8s.io/v1beta1 # for versions before 1.14 use extensions/v1beta1
kind: Ingress
metadata:
  name: hello-whale-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.org/rewrites: "serviceName=green rewrite=/;serviceName=blue rewrite=/"
spec:
  rules:
  - host: stardust
    http:
      paths:
      - path: /green
        backend:
          serviceName: green
          servicePort: 80
      - path: /blue
        backend:
          serviceName: blue
          servicePort: 80

Then, using curl I can reach the right target:然后,使用 curl 我可以达到正确的目标:

$ curl stardust/green
green
$ curl stardust/blue
blue

And here are the logs of the ingress controller:以下是入口 controller 的日志:

10.32.0.1 - - [12/Oct/2019:14:56:12 +0000] "GET /green HTTP/1.1" 200 6 "-" "curl/7.64.1" 132 0.008 [default-green-80] [] 10.32.0.28:80 6 0.008 200 b5ac38db9dd6a7e53d316dc48e9401aa
10.32.0.1 - - [12/Oct/2019:14:56:16 +0000] "GET /blue HTTP/1.1" 200 5 "-" "curl/7.64.1" 131 0.011 [default-blue-80] [] 10.32.0.12:80 5 0.012 200 c438d22db0e80a5206ca8885a48d72f9

I hope it can help you.我希望它可以帮助你。

can you try like this: path: /blue/(.*) – Vasily Angapov 2 days ago你可以这样尝试吗:路径:/blue/(.*) – Vasily Angapov 2 天前

I'd like to add some words of explanation to Vasily Angapov's correct answer posted in question comments so everyone can understand why /blue/(.*) actually works and why /blue|/(.+) will not work and shouldn't be used in this context.我想为Vasily Angapov 在问题评论中发布的正确答案添加一些解释性的话,以便每个人都能理解为什么/blue/(.*)实际有效以及为什么/blue|/(.+)不起作用并且不应该在这种情况下使用。

Let's use regexp online interpreter which contains detailed explanation of every character used in our expression and let us experiment with matching different strings to the given regular expression:让我们使用正则表达式在线解释器,它包含对表达式中使用的每个字符的详细解释,并让我们尝试将不同的字符串与给定的正则表达式匹配:

/blue|/(.+) basically means we want to match string /blue or any other non-zero string. /blue|/(.+)基本上意味着我们要匹配字符串/blue或任何其他非零字符串。 . means any character except of line termination and + sign is a quantifier which tells us how many times previous character may occur in our string.表示除行终止符之外的任何字符, +号是一个量词,它告诉我们前一个字符在我们的字符串中可能出现多少次。 + denotes one or more occurrences of preceding character so the expression .+ matches a string consisting of one or more different characters (excluding line break). +表示出现一个或多个前面的字符,因此表达式.+匹配由一个或多个不同字符组成的字符串(不包括换行符)。

If you construct an ingress path to be matched with specific string you typically don't want to use |如果您构建一个与特定字符串匹配的入口路径,您通常不想使用| . . In this particular context it would mean that you want to match /blue or anything else like /green , /yellow , /red ... which doesn't make much sense in this context.在这个特定的上下文中,这意味着您想要匹配/blueanything else例如/green/yellow/red ...这在这种情况下没有多大意义。 You are rather interested to match paths only starting from /blue like /blue/index.html , /blue/second.html or blue/third.php or just /blue itself.您很感兴趣只匹配从/blue开始的路径,例如/blue/index.html/blue/second.htmlblue/third.php或只是/blue本身Your path may look like this:您的路径可能如下所示:

/blue/(.+)

and it will match /blue/index.html , /blue/1 but not /blue alone as .+ matches one or more arbitrary characters .它将匹配/blue/index.html/blue/1但不单独匹配/blue因为.+匹配一个或多个任意字符

.* on the other hand matches zero or more arbitrary characters so expression /blue/(.*) will match: .*另一方面匹配零个或多个任意字符,因此表达式/blue/(.*)将匹配:

/blue , /blue/index.html , /blue/ , /blue/1 etc. /blue , /blue/index.html , /blue/ , /blue/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM