[英]How do I set up a Kubernetes Ingress rule with a regex path?
I'd like to use regex in the path of an Ingress rule, but I haven't been able to get it to work. 我想在Ingress规则的路径中使用正则表达式,但我无法让它工作。 For example : 例如 :
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
spec:
tls:
- hosts:
- cafe.example.com
secretName: cafe-secret
rules:
- host: cafe.example.com
http:
paths:
- path: /tea
backend:
serviceName: tea-svc
servicePort: 80
- path: /coffee
backend:
serviceName: coffee-svc
servicePort: 80
I tried putting /t[az]a
for the first path, but then any path I tried that should match that regex took me to the default backend instead of the service I expected. 我尝试将/t[az]a
放在第一条路径上,但是我尝试的任何路径应该与该正则表达式相匹配,将我带到默认后端而不是我期望的服务。
Note: I'm using an nginx ingress controller, which should be able to support regex. 注意:我正在使用nginx入口控制器,它应该能够支持正则表达式。
Apparently this question is still getting traffic, so I feel like I should update it. 显然这个问题仍然是流量,所以我觉得我应该更新它。 I'm no longer using the nginx ingress, so I can't verify this works. 我不再使用nginx入口,所以我无法验证这是否有效。 According to https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/ : 根据https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/ :
The ingress controller supports case insensitive regular expressions in the
spec.rules.http.paths.path
field. 入口控制器支持spec.rules.http.paths.path
字段中不区分大小写的正则表达式。 This can be enabled by setting thenginx.ingress.kubernetes.io/use-regex
annotation totrue
(the default is false). 这可以通过将nginx.ingress.kubernetes.io/use-regex
注释设置为true
来启用(默认值为false)。
The example they provide on the page would cover it: 他们在页面上提供的示例将涵盖它:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress-3
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: test.com
http:
paths:
- path: /foo/bar/bar
backend:
serviceName: test
servicePort: 80
- path: /foo/bar/[A-Z0-9]{3}
backend:
serviceName: test
servicePort: 80
Original answer that no longer works. 原来的答案不再适用。
It appears that the solution is ridiculously simple (at least with an nginx ingress controller) - you just need to prepend the path with "~ "
: 似乎解决方案非常简单(至少使用nginx入口控制器) - 您只需要在路径前加上"~ "
:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
spec:
tls:
- hosts:
- cafe.example.com
secretName: cafe-secret
rules:
- host: cafe.example.com
http:
paths:
- path: ~ /t[a-z]a
backend:
serviceName: tea-svc
servicePort: 80
- path: /coffee
backend:
serviceName: coffee-svc
servicePort: 80
I don't think there is an option to use regexp in Ingress objects. 我不认为在Ingress对象中可以选择使用regexp。 Ingress is designed to work with multiple IngressController implementations, both provided by cloud services or by self-hosted ingress like nginx one from kubernetes/contrib (which I use on my setup). Ingress旨在与多个IngressController实现一起使用,这些实现既可以由云服务提供,也可以由kubernetes / contrib(我在我的设置中使用)的nginx之类的自托管入口提供。 Thus ingress should cover features that are commonly available on most common implementations, while specific, non-standard behaviours can be set using annotations (like ie. many nginx ingress features). 因此,入口应该涵盖在大多数常见实现中通常可用的特征,而特定的非标准行为可以使用注释来设置(例如,许多nginx入口特征)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.