Kubernetes重写目标路径,以便附加要匹配的路径



我正在使用OSSingress-nginxIngress控制器,并试图创建一个重写目标规则,以便在字符串匹配之前附加一个路径字符串。

如果我想用regex创建一个与/matched/path匹配的重写规则,并将其重写为/prefix/matched/path,我该如何做到这一点?

我尝试过以下方法,但效果不佳,我只是对这个入口定义的语法感到困惑:

metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- path: /(/prefix/)(/|$)(/matched/path)(.*)
backend:
serviceName: webapp1

如果我想用匹配的regex创建重写规则/matched/path并将其重写为/prefix/matched/path我能做到吗?

为了实现这一点,您必须将/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/$1
spec:
rules:
http:
paths:
- path: /(matched/path/?.*)
backend:
serviceName: test
servicePort: 80   

由于新入口的语法在1.19中发生了变化(请参阅最后的发行说明和一些小信息(,我还放了一个例子:


apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress-v119
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /prefix/$1
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": "*/*"
},

值得一提的是,新入口语法中的一些显著差异/变化:

  • spec.backend->spec.defaultBackend
  • serviceName->service.name
  • servicePort->service.port.name(用于字符串值(
  • servicePort->service.port.number(对于数值(pathType在v1中不再具有默认值"精确"前缀";,或"具体实施";必须指定其他Ingress API更新
  • 后端现在可以是资源或服务后端
  • CCD_ 18不再需要是有效的正则表达式(#89778,@cmluciano([SIG API机械、应用程序、CLI、网络和测试]

相关内容

  • 没有找到相关文章

最新更新