k8s---使用ingress配置域名转发时的traefik路径规则详解

ingress中traefik的使用方式如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: spark-client-test
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: PathPrefix
spec:
  rules:
  -
    host: api-beta.test.com
    http:
      paths:
      - path: /api/spark-client-test
        backend:
          serviceName: spark-client-test
          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

则配置如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: spark-client-test
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: PathPrefix
spec:
  rules:
  -
    host: api-beta.test.com
    http:
      paths:
      - path: /api/spark-client-test
        backend:
          serviceName: spark-client-test
          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

具体例子可以参考

hadoop组件—spark实战----spark on k8s模式k8s原生方式spark2.4.4 通过web UI界面查看任务运行情况

发布了817 篇原创文章 · 获赞 911 · 访问量 532万+

猜你喜欢

转载自blog.csdn.net/q383965374/article/details/104503755