如何在Kubernetes Traefik入口中去除路径前缀



我在Ubuntu 20.04上使用k3s v1.22.7。我希望/bar/xyz成为吊舱的/xyz。在没有中间件的情况下,我被正确地路由到pod,有了中间件,我从Traefik获得404,就像从replacePathRegex/stripPrefix剥离发生在Ingress路径评估之前一样。网上的例子都是这样的。。。

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-prefix
spec:
#replacePathRegex:
#  regex: ^/(?:[^/]+)/(.*)
#  replacement: /$1
stripPrefix:
prefixes:
- /bar
- /baz
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: foo-ingress
annotations:
kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/router.middlewares: strip-prefix@kubernetescrd
spec:
rules:
- host: example.org
http:
paths:
- path: /bar
pathType: Prefix
backend:
service:
name: foo-service
port:
number: 5001
- path: /baz
pathType: Prefix
backend:
service:
name: foo-service
port:
number: 5002

看起来中间件需要前缀名称空间,所以

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-prefix
# No namespace defined
spec:
stripPrefixRegex:
regex:
- ^/[^/]+
---
kind: Ingress
metadata:
annotations:
traefik.ingress.kubernetes.io/router.middlewares: default-strip-prefix@kubernetescrd

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-prefix
namespace: example # Namespace defined
spec:
stripPrefixRegex:
regex:
- ^/[^/]+
---
kind: Ingress
metadata:
annotations:
traefik.ingress.kubernetes.io/router.middlewares: example-strip-prefix@kubernetescrd

应该起作用。

(来源(

相关内容

  • 没有找到相关文章

最新更新