ingress中traefik的使用方式如下:

    1. apiVersion: extensions/v1beta1
    2. kind: Ingress
    3. metadata:
    4. name: spark-client-test
    5. namespace: default
    6. annotations:
    7. kubernetes.io/ingress.class: traefik
    8. traefik.frontend.rule.type: PathPrefix
    9. spec:
    10. rules:
    11. -
    12. host: api-beta.test.com
    13. http:
    14. paths:
    15. - path: /api/spark-client-test
    16. backend:
    17. serviceName: spark-client-test
    18. servicePort: 4040

    这里注意traefik.frontend.rule.type的区别:

    Path: /products/, /articles/{category}/{id:[0-9]+}: Path 可以添加一个URL路径的匹配。它接受一个以{}包括起来的为空或更多url变量的模版。

    PathStrip: /products/ 和 Path 相同,但从请求的URL路径中去掉的给定的前缀。

    PathStripRegex: /articles/{category}/{id:[0-9]+} Match exact path and strip off the path prior to forwarding the request to the backend. It accepts a sequence of literal and regular expression paths.

    PathPrefix: /products/, /articles/{category}/{id:[0-9]+} PathPrefix 可以添加一个URL路径前缀的匹配。它匹配给定模版中的完整URL路径前缀。

    PathPrefixStrip: /products/ 和 PathPrefix 相同,但从请求的URL路径中去掉的给定的前缀。

    PathPrefixStripRegex: /articles/{category}/{id:[0-9]+} Match request prefix path and strip off the path prefix prior to forwarding the request to the backend. It accepts a sequence of literal and regular expression prefix paths. Starting with Traefik 1.3, the stripped prefix path will be available in the X-Forwarded-Prefix header.

    也就是说

    Path 和 PathPrefix 使用在 我们的web 服务中本身时带有路由的。

    比如 在web服务 本机 访问 使用的 链接时 : localhost:4040/api/spark-client-test

    则配置如下:

    1. apiVersion: extensions/v1beta1
    2. kind: Ingress
    3. metadata:
    4. name: spark-client-test
    5. namespace: default
    6. annotations:
    7. kubernetes.io/ingress.class: traefik
    8. traefik.frontend.rule.type: PathPrefix
    9. spec:
    10. rules:
    11. -
    12. host: api-beta.test.com
    13. http:
    14. paths:
    15. - path: /api/spark-client-test
    16. backend:
    17. serviceName: spark-client-test
    18. servicePort: 4040

    在浏览器中访问使用 api-beta.test.com/api/spark-client-test 就可以对应访问到。

    如果我们使用PathPrefixStrip参数,访问 api-beta.test.com/api/spark-client-test 时,会把 /api/spark-client-test去掉, 实际上访问的时 api-beta.test.com,也就是对应到 localhost:4040 。

    根据这样的特性 我们可以根据 自己的 需求来选用 使用 PathPrefix 还是 PathPrefixStrip。

    如果我们是想访问到 没有路径的入口 比如 localhost:4040 这种类型,则使用PathPrefixStrip,访问的时候会忽略到我们设置的path

    如果我们是想访问到 有路径的入口 比如 localhost:4040/api/spark-client-test 这种类型,则使用PathPrefix,访问的时候会带上到我们设置的path
    ————————————————
    版权声明:本文为CSDN博主「张小凡vip」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/zzq900503/article/details/104503755