【问题标题】:Add header with EnvoyFilter does not work使用 EnvoyFilter 添加标头不起作用
【发布时间】:2021-10-01 00:19:54
【问题描述】:

我正在测试 istio 1.10.3 以使用 minikube 添加标头,但我无法这样做。

Istio 安装在 istio-system 命名空间中。 部署部署的命名空间标有istio-injection=enabled

config_dump 中,只有当上下文设置为ANY 时,我才能看到LUA 代码。当我将其设置为SIDECAR_OUTBOUND 时,代码未列出:

"name": "envoy.lua",
"typed_config": {
"@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua",
"inline_code": "function envoy_on_request(request_handle)\n  request_handle:headers():add(\"request-body-size\", request_handle:body():length())\nend\n\nfunction envoy_on_response(response_handle)\n  response_handle:headers():add(\"response-body-size\", response_handle:body():length())\nend\n"
}

有人可以给我一些建议吗?

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: headers-envoy-filter
  namespace: nginx-echo-headers
spec:
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: SIDECAR_OUTBOUND
      listener:
        filterChain:
          filter:
            name: envoy.filters.network.http_connection_manager
            subFilter:
              name: envoy.filters.http.router
    patch:
      operation: INSERT_BEFORE
      value:
       name: envoy.lua
       typed_config:
         '@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
         inline_code: |
           function envoy_on_request(request_handle)
             request_handle:headers():add("request-body-size", request_handle:body():length())
           end

           function envoy_on_response(response_handle)
             response_handle:headers():add("response-body-size", response_handle:body():length())
           end
  workloadSelector:
    labels:
      app: nginx-echo-headers
      version: v1

以下是我的部署和 Istio 配置:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-echo-headers-v1
  namespace: nginx-echo-headers
  labels:
    version: v1
spec:
  selector:
    matchLabels:
      app: nginx-echo-headers
      version: v1
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx-echo-headers
        version: v1
    spec:
      containers:
      - name: nginx-echo-headers
        image: brndnmtthws/nginx-echo-headers:latest
        ports:
          - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-echo-headers-svc
  namespace: nginx-echo-headers
  labels:
    version: v1
    service: nginx-echo-headers-svc
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    targetPort: 8080
  selector:
    app: nginx-echo-headers
    version: v1
---
# ISTIO GATEWAY
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: nginx-echo-headers-gateway
  namespace: istio-system
spec:
  selector:
    app: istio-ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "api.decchi.com.ar"

# ISTIO VIRTUAL SERVICE
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-echo-headers-virtual-service
  namespace: nginx-echo-headers
spec:
  hosts:
  - 'api.decchi.com.ar'
  gateways:
  - istio-system/nginx-echo-headers-gateway
  http:
  - route:
      - destination:
          # k8s service name
          host: nginx-echo-headers-svc
          port:
            # Services port
            number: 80
          # workload selector
          subset: v1

## ISTIO DESTINATION RULE
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: nginx-echo-headers-dest
  namespace: nginx-echo-headers
spec:
  host: nginx-echo-headers-svc
  subsets:
    - name: "v1"
      labels:
        app: nginx-echo-headers
        version: v1

仅当我在GATEWAY 中配置上下文时它才有效。 envoyFilteristio-system 命名空间中运行,workloadSelector 配置如下:

workloadSelector:
    labels:
      istio: ingressgateway

但我的想法是在SIDECAR_OUTBOUND中配置。

【问题讨论】:

  • 问题到底出在哪里,您的预期结果是什么?
  • 我的意图是配置使用上下文 SIDECAR_OUTBOUND 代替 ANY 或 GATEWAY。然后在为标签 app=xxx 设置工作负载选择器时使用它,其中 xxx 是我的应用程序 pod 的名称。
  • 你有哪个特定版本的 istio? 1.10.0 还是 1.10.3?
  • 我的 istio 版本是 1.10.3
  • 所以寻找答案。它是为 1.10.3 版本提供的,应该可以解决您的问题。

标签: kubernetes istio envoyproxy


【解决方案1】:

只有当我在 GATEWAY 中配置上下文时它才有效,envoyFilter 正在 istio-system 命名空间中运行

没错!您应该在配置根命名空间istio-system- 中应用您的EnvoyFilter

最重要的部分,在匹配你的configPatches 时,只需省略context 字段,以便这适用于边车和网关。用法示例见this Istio Doc

【讨论】:

  • 关于文件和example 只能使用context SIDECAR_OUTBOUND 并使用另一个命名空间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 2014-04-22
  • 2015-06-21
相关资源
最近更新 更多