【问题标题】:simple envoy filter not being used未使用简单的特使过滤器
【发布时间】:2020-11-10 17:02:43
【问题描述】:

您好,我是 Envoy 和 Istio 的新手。我正在尝试编写一个特使过滤器来重写/重定向 HTTP(s) 请求。下面是我的配置(是的,一个玩具示例),但它不起作用。

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: lua-filter
spec:
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: SIDECAR_OUTBOUND
      listener:
        filterChain:
          filter:
            name: "envoy.http_connection_manager"
            subFilter:
              name: "envoy.router"
    patch:
      operation: INSERT_BEFORE
      value:
       name: envoy.lua
       typed_config:
         "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
         inlineCode: |
            function envoy_on_request(request_handle)
                request_handle:headers():add("authorization", "it works!")
            end
            function envoy_on_response(response_handle)
                  filter_name = "ENVOY"
                  response_handle:headers():add("my_Filter", filter_name)
            end

在我部署应用程序(在端口 443 上侦听 https,在 Cloudflare 后面)和特使过滤器后,我会使用 curl -v <my_app>。我没有看到添加的请求标头或响应标头。我还尝试添加其他答案建议的xff_num_trusted_hops: 2,但无济于事。我做错了什么?

【问题讨论】:

    标签: istio envoyproxy


    【解决方案1】:

    要将过滤器应用于单个 pod,您必须为您的应用添加 workloadSelector

      workloadSelector:
        labels:
          xxx: xxx
    

    例如,有一个 nginx 部署,并且您的 envoy 过滤器带有适当的 workloadSelector

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx1
    spec:
      selector:
        matchLabels:
          run: nginx1
      replicas: 1
      template:
        metadata:
          labels:
            run: nginx1
            app: frontend
        spec:
          containers:
          - name: nginx1
            image: nginx
            ports:
            - containerPort: 80
    
    
    apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
      name: lua-filter
      namespace: default
    spec:
      workloadSelector:
        labels:
          run: nginx1
      configPatches:
      - applyTo: HTTP_FILTER
        match:
          context: SIDECAR_INBOUND
          listener:
            filterChain:
              filter:
                name: "envoy.http_connection_manager"
                subFilter:
                  name: "envoy.router"
        patch:
          operation: INSERT_BEFORE
          value:
           name: envoy.lua
           typed_config:
             "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
             inlineCode: |
                function envoy_on_request(request_handle)
                    request_handle:headers():add("authorization", "it works!")
                end
                function envoy_on_response(response_handle)
                      filter_name = "ENVOY"
                      response_handle:headers():add("my_Filter", filter_name)
                end
    

    将过滤器应用于通过您的 istio 入口网关的所有请求。

    1.将更改上下文从SIDECAR_INBOUND更改为GATEWAY

    2.设置workloadSelector

      workloadSelector:
        labels:
          istio: ingressgateway
    

    3.设置 istio-system 命名空间。

      namespace: istio-system
    

    4.经过几次修改后,您的示例就出来了。

    apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
      name: lua-filter
      namespace: istio-system
    spec:
      workloadSelector:
        labels:
          istio: ingressgateway
      configPatches:
      - applyTo: HTTP_FILTER
        match:
          context: GATEWAY
          listener:
            filterChain:
              filter:
                name: "envoy.http_connection_manager"
                subFilter:
                  name: "envoy.router"
        patch:
          operation: INSERT_BEFORE
          value:
           name: envoy.lua
           typed_config:
             "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
             inlineCode: |
                function envoy_on_request(request_handle)
                    request_handle:headers():add("authorization", "it works!")
                end
                function envoy_on_response(response_handle)
                      filter_name = "ENVOY"
                      response_handle:headers():add("my_Filter", filter_name)
                end
    

    5.我用curl检查过

    curl -s -I -X HEAD xx.xx.xx.xx/productpage

    HTTP/1.1 200 OK
    content-type: text/html; charset=utf-8
    content-length: 5179
    server: istio-envoy
    date: Tue, 10 Nov 2020 08:28:30 GMT
    x-envoy-upstream-service-time: 60
    my_filter: ENVOY    <---
    

    我在 istio ingress-gateway pod 中使用 config_dump 检查了它。

    我在那里执行

    kubectl exec -ti istio-ingressgateway-86f88b6f6-2tv64 -n istio-system -- /bin/bash
    

    config_dump 的结果

    curl 0:15000/config_dump | grep my_Filter
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  140k    0  140k    0     0   9.7M      0 --:--:-- --:--:-- --:--:--  9.7M
                   "inline_code": "function envoy_on_request(request_handle)\n    request_handle:headers():add(\"authorization\", \"it works!\")\nend\nfunction envoy_on_response(response_handle)\n      filter_name = \"ENVOY\"\n      response_handle:headers():add(\"my_Filter\", filter_name)\nend\n"
    

    其他资源:

    【讨论】:

    • 谢谢!我想我的错误是我没有将它应用于GATEWAY。我正在使用 custom-istiogateway,所以我对 namespacelabel 字段进行了一些额外的更改,但您的回答在这里解决了我的问题!
    • 还有一个我在谷歌上没有找到的问题:我在哪里可以找到日志?我使用了envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/…,但是当我查看custom-ingressgateway-* pods 时没有找到任何东西
    • @user2309838 很高兴它对您有用。您到底在寻找什么日志?如果您的意思是特使过滤器的工作日志,我总是使用 curl 或配置转储检查它是否应用正确。
    • 是的,我可以通过 curl 或增加日志级别看到它,例如request_handle:logCritical
    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 2015-02-07
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多