【问题标题】:Envoy route match based on environment variablesEnvoy 基于环境变量的路由匹配
【发布时间】:2019-08-20 00:07:31
【问题描述】:

在使用 envoy 进行条件路由匹配时是否可以考虑环境变量?这里,tag 是一个可用的环境变量。

这里,下面的例子不起作用,但可以使用tag 来做类似下面的事情吗?

          routes:
          - match: { prefix: "/some_path/", {name: {tag}, value: "a"}}
            route: { prefix_rewrite: "/", host_rewrite: {tag}.domain.com, cluster: a }
          - match: { prefix: "/some_path/", {name: {tag}, value: "b"}}
            route: { prefix_rewrite: "/", host_rewrite: {tag}.domain.com, cluster: b }

查看https://www.envoyproxy.io/docs/envoy/latest/api-v2/api/v2/route/route.proto#envoy-api-msg-route-routematch,似乎只能查看路由中的字段,如前缀而不是环境变量?或者,也欢迎任何替代建议!

谢谢!

【问题讨论】:

    标签: envoyproxy


    【解决方案1】:

    我使用元数据和Lua filter 的组合实现了类似的效果。

    将您的 tag 值和关联的集群作为元数据添加到您的路由中,并使用 cluster_header 选项提供要路由到的集群名称:

    routes:
     - match:
         prefix: "/"
       metadata:
         filter_metadata:
           envoy.lua:
             a: a
             b: b
       route:
         cluster_header: "upstream-cluster"
    

    然后您可以使用以下 Lua 过滤器读取环境变量 TAG 并将标头 upstream-cluster 设置为与该值匹配的集群。

    http_filters:
    - name: envoy.lua
            typed_config:
              "@type": type.googleapis.com/envoy.config.filter.http.lua.v2.Lua
              inline_code: |
                function envoy_on_request(request_handle)
    
                  local metadata = request_handle:metadata()
    
                  local cluster_name = metadata:get(os.getenv("TAG"))
    
                  if cluster_name then
                    request_handle:headers():replace("upstream-cluster", cluster_name)
                  else
                    request_handle:respond(
                      {[":status"] = "502",
                       ["upstream"] = "envoy"},
                       "{\"errorCode\":502,\"reason\":\"no backend cluster found\"}")
                  end
                end
    

    【讨论】:

    • 这很有帮助。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    相关资源
    最近更新 更多