用于附加路径以匹配的 Kubernetes 重写目标路径
Kubernetes rewrite-target paths for appending path to match
我正在使用 OSS ingress-nginx
入口控制器并尝试创建一个重写目标规则,以便我可以在我的字符串匹配之前附加一个路径字符串。
如果我想使用匹配 /matched/path
的正则表达式创建一个重写规则并将其重写为 /prefix/matched/path
,我该怎么做?
我试过类似下面的方法,但效果不佳,我只是对这个入口定义的语法感到困惑:
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- path: /(/prefix/)(/|$)(/matched/path)(.*)
backend:
serviceName: webapp1
If I wanted to create a rewrite rule with regex that matches
/matched/path
and rewrites that to /prefix/matched/path
, how might
I be able to do that?
为了实现这一点,您已将 /prefix
添加到您的 rewrite-target
中。
这是一个使用 k8s v1.18 入口语法的工作示例:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: example-ingress-v118
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /prefix/
spec:
rules:
http:
paths:
- path: /(matched/path/?.*)
backend:
serviceName: test
servicePort: 80
由于新入口的语法在 1.19 中发生了变化(参见 release notes 和末尾的一些小信息)我也放置了一个示例:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress-v119
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /prefix/
spec:
rules:
- http:
paths:
- path: /(matched/path/?.*)
pathType: Prefix
backend:
service:
name: test
port:
number: 80
这是一个使用 http echo 服务器的测试:
➜ ~ curl 172.17.0.4/matched/path
{
"path": "/prefix/matched/path",
"headers": {
"host": "172.17.0.4",
"x-request-id": "011585443ebc6adcf913db1c506abbe6",
"x-real-ip": "172.17.0.1",
"x-forwarded-for": "172.17.0.1",
"x-forwarded-host": "172.17.0.4",
"x-forwarded-port": "80",
"x-forwarded-proto": "http",
"x-scheme": "http",
"user-agent": "curl/7.52.1",
"accept": "*/*"
},
这条规则也会忽略请求末尾的/
:
➜ ~ curl 172.17.0.4/matched/path/
{
"path": "/prefix/matched/path/",
"headers": {
"host": "172.17.0.4",
"x-request-id": "0575e9022d814ba07457395f78dbe0fb",
"x-real-ip": "172.17.0.1",
"x-forwarded-for": "172.17.0.1",
"x-forwarded-host": "172.17.0.4",
"x-forwarded-port": "80",
"x-forwarded-proto": "http",
"x-scheme": "http",
"user-agent": "curl/7.52.1",
"accept": "*/*"
},
值得一提的是新入口语法中一些值得注意的 differences/changes:
spec.backend
-> spec.defaultBackend
serviceName
-> service.name
servicePort
-> service.port.name
(对于字符串值)
servicePort
-> service.port.number
(对于数值)pathType
在 v1 中不再有默认值; “精确”、“前缀”或
必须指定“ImplementationSpecific” 其他入口 API 更新
- 后端现在可以是资源或服务后端
path
不再需要是有效的正则表达式(#89778,
@cmluciano) [SIG API 机器、应用程序、CLI、网络和测试]
我正在使用 OSS ingress-nginx
入口控制器并尝试创建一个重写目标规则,以便我可以在我的字符串匹配之前附加一个路径字符串。
如果我想使用匹配 /matched/path
的正则表达式创建一个重写规则并将其重写为 /prefix/matched/path
,我该怎么做?
我试过类似下面的方法,但效果不佳,我只是对这个入口定义的语法感到困惑:
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- path: /(/prefix/)(/|$)(/matched/path)(.*)
backend:
serviceName: webapp1
If I wanted to create a rewrite rule with regex that matches
/matched/path
and rewrites that to/prefix/matched/path
, how might I be able to do that?
为了实现这一点,您已将 /prefix
添加到您的 rewrite-target
中。
这是一个使用 k8s v1.18 入口语法的工作示例:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: example-ingress-v118
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /prefix/
spec:
rules:
http:
paths:
- path: /(matched/path/?.*)
backend:
serviceName: test
servicePort: 80
由于新入口的语法在 1.19 中发生了变化(参见 release notes 和末尾的一些小信息)我也放置了一个示例:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress-v119
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /prefix/
spec:
rules:
- http:
paths:
- path: /(matched/path/?.*)
pathType: Prefix
backend:
service:
name: test
port:
number: 80
这是一个使用 http echo 服务器的测试:
➜ ~ curl 172.17.0.4/matched/path
{
"path": "/prefix/matched/path",
"headers": {
"host": "172.17.0.4",
"x-request-id": "011585443ebc6adcf913db1c506abbe6",
"x-real-ip": "172.17.0.1",
"x-forwarded-for": "172.17.0.1",
"x-forwarded-host": "172.17.0.4",
"x-forwarded-port": "80",
"x-forwarded-proto": "http",
"x-scheme": "http",
"user-agent": "curl/7.52.1",
"accept": "*/*"
},
这条规则也会忽略请求末尾的/
:
➜ ~ curl 172.17.0.4/matched/path/
{
"path": "/prefix/matched/path/",
"headers": {
"host": "172.17.0.4",
"x-request-id": "0575e9022d814ba07457395f78dbe0fb",
"x-real-ip": "172.17.0.1",
"x-forwarded-for": "172.17.0.1",
"x-forwarded-host": "172.17.0.4",
"x-forwarded-port": "80",
"x-forwarded-proto": "http",
"x-scheme": "http",
"user-agent": "curl/7.52.1",
"accept": "*/*"
},
值得一提的是新入口语法中一些值得注意的 differences/changes:
spec.backend
->spec.defaultBackend
serviceName
->service.name
servicePort
->service.port.name
(对于字符串值)servicePort
->service.port.number
(对于数值)pathType
在 v1 中不再有默认值; “精确”、“前缀”或 必须指定“ImplementationSpecific” 其他入口 API 更新- 后端现在可以是资源或服务后端
path
不再需要是有效的正则表达式(#89778, @cmluciano) [SIG API 机器、应用程序、CLI、网络和测试]