gpt4 book ai didi

kubernetes - 基于路径的路由问题 Traefik 作为入口 Controller

转载 作者:行者123 更新时间:2023-12-02 07:53:28 25 4
gpt4 key购买 nike

我正在解决看起来像是配置问题的问题!我使用 traefik 作为 kubernetes 中的入口 Controller ,并且我有一个入口来路由一些 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/(.*) /$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

使用 Postman(或任何其他客户端),如果我在 http://foo.io/api/authservice/auth/oauth/token 上发布请求,同时查看访问日志,它似乎被路由到http://foo.io/api/svcXXX/v1/files/image/(.*)/v1/files/image/$1。我在访问日志中看到了这一点:

[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

我做错了什么吗?

最佳答案

注意:由于文档已更改,我更新了链接,但文档页面上的内容会有所不同。

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/$1: 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 GuidelinesThis 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 basepath 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 isexpected to listen on /products.

Use a *Strip matcher if your backend listens on the root path (/) butshould 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 priorto forwarding, your backend is expected to listen on /. If yourbackend is serving assets (e.g., images or Javascript files), chancesare it must return properly constructed relative URLs. Continuing onthe example, the backend should return /products/shoes/image.png (andnot /images.png which Traefik would likely not be able to associatewith the same backend). The X-Forwarded-Prefix header (available sinceTraefik 1.3) can be queried to build such URLs dynamically.

Instead of distinguishing your backends by path only, you can add aHost matcher to the mix. That way, namespacing of your backendshappens on the basis of hosts in addition to paths.

可以找到匹配器及其描述的完整列表 here

关于kubernetes - 基于路径的路由问题 Traefik 作为入口 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51156406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com