简体   繁体   English

基于路径的路由问题 Traefik 作为入口控制器

[英]Path based routing issues Traefik as Ingress Controller

I'm running through what looks like a configuration issue!我正在解决看起来像配置问题的问题! I am using traefik as ingress controller within kubernetes and I have an ingress to route some URLs to route some frontends to various backends.我在 kubernetes 中使用 traefik 作为入口控制器,并且我有一个入口来路由一些 URL,以将一些前端路由到各种后端。 Let's say I have something like this:假设我有这样的事情:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: ReplacePathRegex
spec:
  rules:
  - host: foo.io
    http:
      paths:
      - path: /api/authservice/(.*) /$1
        backend:
          serviceName: auth
          servicePort: 8901
      - path: /api/svcXXX/v1/files/cover/(.*) /v1/files/cover/$1
        backend:
          serviceName: files
          servicePort: 8183
      - path: /api/svcXXX/v1/files/image/(.*) /v1/files/image/$1
        backend:
          serviceName: files
          servicePort: 8183

Using Postman (or any other client), if I POST a request on http://foo.io/api/authservice/auth/oauth/token , while looking in the access logs, it seems that it is routed to http://foo.io/api/svcXXX/v1/files/image/(.*) /v1/files/image/$1 .使用邮递员(或任何其他客户端),如果我在http://foo.io/api/authservice/auth/oauth/token请求,在查看访问日志时,它似乎被路由到http://foo.io/api/svcXXX/v1/files/image/(.*) /v1/files/image/$1 I'm seeing this in the access logs:我在访问日志中看到了这一点:

[03/Jul/2018:12:57:17 +0000] "POST /api/authservice/auth/oauth/token HTTP/1.1" 401 102 "-" "PostmanRuntime/7.1.5" 15 "foo.io/api/svcXXX/v1/files/image/(.*) /v1/files/image/$1" 37ms

Am I doing something wrong ?难道我做错了什么 ?

Note: since the documentation is changed, I've updated the links, but content on the documentation pages would be different.注意:由于文档已更改,我已更新链接,但文档页面上的内容会有所不同。

ReplacePathRegex is a modifier rule. ReplacePathRegex是一个修饰符规则。 According to documentation :根据文档

Modifier rules only modify the request.修饰符规则只修改请求。 They do not have any impact on routing decisions being made.它们对正在做出的路由决策没有任何影响。

Following is the list of existing modifier rules:以下是现有修饰符规则的列表:

  • AddPrefix : /products : Add path prefix to the existing request path prior to forwarding the request to the backend. AddPrefix : /products : 在将请求转发到后端之前,向现有请求路径添加路径前缀。
  • ReplacePath : /serverless-path : Replaces the path and adds the old path to the X-Replaced-Path header. ReplacePath : /serverless-path : 替换路径并将旧路径添加到 X-Replaced-Path 标头。 Useful for mapping to AWS Lambda or Google Cloud Functions.用于映射到 AWS Lambda 或 Google Cloud Functions。
  • ReplacePathRegex : ^/api/v2/(.*) /api/$1 : Replaces the path with a regular expression and adds the old path to the X-Replaced-Path header. ReplacePathRegex : ^/api/v2/(.*) /api/$1 : 用正则表达式替换路径并将旧路径添加到 X-Replaced-Path 标头。 Separate the regular expression and the replacement by a space.用空格分隔正则表达式和替换。

To route requests, you should use matchers :要路由请求,您应该使用matchers

Matcher rules determine if a particular request should be forwarded to a backend.匹配器规则确定是否应将特定请求转发到后端。

Separate multiple rule values by , (comma) in order to enable ANY semantics (ie, forward a request if any rule matches).用 ,(逗号)分隔多个规则值以启用任何语义(即,如果任何规则匹配,则转发请求)。 Does not work for Headers and HeadersRegexp.不适用于 Headers 和 HeadersRegexp。

Separate multiple rule values by ;用 ; 分隔多个规则值(semicolon) in order to enable ALL semantics (ie, forward a request if all rules match). (分号)以启用所有语义(即,如果所有规则匹配,则转发请求)。

###Path Matcher Usage Guidelines This section explains when to use the various path matchers. ###Path 匹配器使用指南 本节说明何时使用各种路径匹配器。

Use Path if your backend listens on the exact path only.如果您的后端仅侦听确切路径,请使用Path For instance, Path: /products would match /products but not /products/shoes .例如, Path: /products将匹配/products但不匹配/products/shoes

Use a *Prefix* matcher if your backend listens on a particular base path but also serves requests on sub-paths.如果您的后端侦听特定的基本路径,但也为子路径上的请求提供服务,请使用*Prefix*匹配器。 For instance, PathPrefix: /products would match /products but also /products/shoes and /products/shirts .例如, PathPrefix: /products将匹配/products但也匹配/products/shoes/products/shirts Since the path is forwarded as-is, your backend is expected to listen on /products.由于路径按原样转发,因此您的后端应该侦听 /products。

Use a *Strip matcher if your backend listens on the root path (/) but should be routable on a specific prefix.如果您的后端侦听根路径 (/) 但应该可以在特定前缀上路由,请使用*Strip匹配器。 For instance, PathPrefixStrip: /products would match /products but also / products/shoes and /products/shirts .例如, PathPrefixStrip: /products将匹配/products但也匹配 / products/shoes/products/shirts Since the path is stripped prior to forwarding, your backend is expected to listen on / .由于路径在转发之前被剥离,因此您的后端应该监听/ If your backend is serving assets (eg, images or Javascript files), chances are it must return properly constructed relative URLs.如果您的后端正在提供资产(例如,图像或 Javascript 文件),则很有可能它必须返回正确构造的相对 URL。 Continuing on the example, the backend should return /products/shoes/image.png (and not /images.png which Traefik would likely not be able to associate with the same backend).继续这个例子,后端应该返回/products/shoes/image.png (而不是/images.png可能无法与同一个后端关联)。 The X-Forwarded-Prefix header (available since Traefik 1.3) can be queried to build such URLs dynamically.可以查询X-Forwarded-Prefix标头(自 Traefik 1.3 起可用)以动态构建此类 URL。

Instead of distinguishing your backends by path only, you can add a Host matcher to the mix.您可以将主机匹配器添加到混合中,而不是仅通过路径来区分后端。 That way, namespacing of your backends happens on the basis of hosts in addition to paths.这样,除了路径之外,后端的命名空间也是基于主机进行的。

Full list of matchers and their descriptions can be found here可以在此处找到匹配器的完整列表及其描述

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

相关问题 使用 ALB 入口控制器的基于路径的路由 - Path Based routing using ALB ingress controller Nginx 入口控制器 - 基于路径的路由 - Nginx Ingress controller- Path based routing 根据路径的文件类型路由入口控制器流量 - Routing ingress controller traffic based upon file type of path 使用 Traefik Ingress Controller 将流量路由到外部 Kubernetes - Routing traffic to outside Kubernetes using Traefik Ingress Controller 无法在 Kubernetes Ingress 中配置基于路径的路由 - Unable to configure path based routing in Kubernetes Ingress 基于 Kubernetes Ingress 路径的路由未按预期工作 - Kubernetes Ingress path based routing not working as expected NGINX Ingress 控制器后端协议注释如何在基于路径的路由中工作? - How NGINX Ingress controller back-end protocol annotation works in path based routing? nginx 入口 controller 和基本 django 应用程序的路由问题 - Routing issues with nginx ingress controller and basic django app 询问路径路由上的 traefik 入口似乎不起作用。 仅 / 有效 - traefik ingress on ask path routing dose not seems to work. only / works 使用 nginx 入口进行基于路径的路由时重写路径 - Rewriting path when using nginx ingress for path based routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM