基于路径的路由问题 Traefik 作为 Ingress Controller
Path based routing issues Traefik as Ingress Controller
我正在 运行 解决看起来像是配置问题的问题!我在 kubernetes 中使用 traefik 作为入口控制器并且我有一个入口来路由一些 URL 以将一些前端路由到各种后端。假设我有这样的事情:
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/(.*) /
backend:
serviceName: auth
servicePort: 8901
- path: /api/svcXXX/v1/files/cover/(.*) /v1/files/cover/
backend:
serviceName: files
servicePort: 8183
- path: /api/svcXXX/v1/files/image/(.*) /v1/files/image/
backend:
serviceName: files
servicePort: 8183
使用 Postman(或任何其他客户端),如果我在 http://foo.io/api/authservice/auth/oauth/token
上 POST 请求,在查看访问日志时,它似乎被路由到 http://foo.io/api/svcXXX/v1/files/image/(.*) /v1/files/image/
。我在访问日志中看到了这个:
[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/" 37ms
我是不是做错了什么?
注意:由于文档已更改,我已更新链接,但文档页面上的内容会有所不同。
ReplacePathRegex
是一个 修饰符 规则。根据 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.
ReplacePath
: /serverless-path
: Replaces the path and adds the old path to the X-Replaced-Path header. Useful for mapping to AWS Lambda or Google Cloud Functions.
ReplacePathRegex
: ^/api/v2/(.*) /api/
: Replaces the path with a regular expression and adds the old path to the X-Replaced-Path header. Separate the regular expression and the replacement by a space.
要路由请求,您应该使用 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 (i.e., forward a request if any rule matches). Does not work for Headers and HeadersRegexp.
Separate multiple rule values by ; (semicolon) in order to enable ALL semantics (i.e., forward a request if all rules match).
###Path Matcher Usage Guidelines
This section explains when to use the various path matchers.
Use Path
if your backend listens on the exact path only. For instance,
Path: /products
would match /products
but not /products/shoes
.
Use a *Prefix*
matcher if your backend listens on a particular base
path but also serves requests on sub-paths. For instance, PathPrefix: /products
would match /products
but also /products/shoes
and
/products/shirts
. Since the path is forwarded as-is, your backend is
expected to listen on /products.
Use a *Strip
matcher if your backend listens on the root path (/) but
should be routable on a specific prefix. For instance,
PathPrefixStrip: /products
would match /products
but also
/products/shoes
and /products/shirts
. Since the path is stripped prior
to forwarding, your backend is expected to listen on /
. If your
backend is serving assets (e.g., images or Javascript files), chances
are it must return properly constructed relative URLs. 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). The X-Forwarded-Prefix
header (available since
Traefik 1.3) can be queried to build such URLs dynamically.
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.
可以找到匹配器的完整列表及其描述here
我正在 运行 解决看起来像是配置问题的问题!我在 kubernetes 中使用 traefik 作为入口控制器并且我有一个入口来路由一些 URL 以将一些前端路由到各种后端。假设我有这样的事情:
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/(.*) /
backend:
serviceName: auth
servicePort: 8901
- path: /api/svcXXX/v1/files/cover/(.*) /v1/files/cover/
backend:
serviceName: files
servicePort: 8183
- path: /api/svcXXX/v1/files/image/(.*) /v1/files/image/
backend:
serviceName: files
servicePort: 8183
使用 Postman(或任何其他客户端),如果我在 http://foo.io/api/authservice/auth/oauth/token
上 POST 请求,在查看访问日志时,它似乎被路由到 http://foo.io/api/svcXXX/v1/files/image/(.*) /v1/files/image/
。我在访问日志中看到了这个:
[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/" 37ms
我是不是做错了什么?
注意:由于文档已更改,我已更新链接,但文档页面上的内容会有所不同。
ReplacePathRegex
是一个 修饰符 规则。根据 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.ReplacePath
:/serverless-path
: Replaces the path and adds the old path to the X-Replaced-Path header. Useful for mapping to AWS Lambda or Google Cloud Functions.ReplacePathRegex
:^/api/v2/(.*) /api/
: Replaces the path with a regular expression and adds the old path to the X-Replaced-Path header. Separate the regular expression and the replacement by a space.
要路由请求,您应该使用 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 (i.e., forward a request if any rule matches). Does not work for Headers and HeadersRegexp.
Separate multiple rule values by ; (semicolon) in order to enable ALL semantics (i.e., forward a request if all rules match).
###Path Matcher Usage Guidelines This section explains when to use the various path matchers.
Use
Path
if your backend listens on the exact path only. For instance,Path: /products
would match/products
but not/products/shoes
.Use a
*Prefix*
matcher if your backend listens on a particular base path but also serves requests on sub-paths. For instance,PathPrefix: /products
would match/products
but also/products/shoes
and/products/shirts
. Since the path is forwarded as-is, your backend is expected to listen on /products.Use a
*Strip
matcher if your backend listens on the root path (/) but should be routable on a specific prefix. For instance,PathPrefixStrip: /products
would match/products
but also /products/shoes
and/products/shirts
. Since the path is stripped prior to forwarding, your backend is expected to listen on/
. If your backend is serving assets (e.g., images or Javascript files), chances are it must return properly constructed relative URLs. 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). TheX-Forwarded-Prefix
header (available since Traefik 1.3) can be queried to build such URLs dynamically.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.
可以找到匹配器的完整列表及其描述here