更新 EnvoyFilter 压缩器配置

envoy.extensions.filters.http.compressor.v3.Compressor 过滤器的 API Surface 中的多个顶级字段在 Envoy 中已废弃(请参阅源定义)。 这些设置已移至专用 response_direction_config.common_configrequest_direction_config.common_config 代码块中。

本指南提供了将EnvoyFilter资源更新为支持的格式所需的背景信息和步骤。改用这种新式格式可确保您的配置与后续更新保持兼容,受益于结构清晰度的提升,并符合 Cloud Service Mesh 现代化最佳实践。

了解现代化改造的必要性

Envoy 压缩器过滤器可对 HTTP 正文进行即时压缩和解压缩,从而有助于减少带宽用量并提高应用性能。

使用 TRAFFIC_DIRECTOR 控制平面的 Cloud Service Mesh(请参阅检查控制平面实现)需要使用受支持的 EnvoyFilter API 版本。使用已弃用的顶级字段(例如 content_lengthcontent_typedisable_on_etag_headerremove_accept_encoding_headerruntime_enabled)的旧版部署将继续正常运行,但建议立即更新,以提高可靠性。

使用这些已弃用的字段时,验证会逐步在各个发布渠道(RapidRegularStable)中推出。控制平面验证会根据部署发生的时间应用:

部署类型 验证行为
旧版部署(在启用验证之前部署) 控制平面会在您的 EnvoyFilter 自定义资源 (CR) 上设置警告状态,内容为:found usage of unsupported fields: [...]。为了实现向后兼容性,系统仍会应用该配置,但您需要迁移到受支持的字段,以确保继续获得支持。
支持的部署(在启用验证后部署) 控制平面会严格禁止使用不受支持的字段。应用配置会导致 EnvoyFilter 资源出现错误,并显示消息:found usage of unsupported fields: [...],且不受支持的配置将被拒绝。

更新配置可解决资源状态中的这些警告和错误,并确保配置符合验证要求。

识别已弃用的配置

如果您直接在 typed_config 块下设置以下任何字段,则需要更新 EnvoyFilter 配置:

  • min_content_length
  • content_length
  • content_type
  • disable_on_etag_header
  • remove_accept_encoding_header

您可以使用以下命令列出 EnvoyFilter:

kubectl get envoyfilters --all-namespaces -o yaml

检查 EnvoyFilters 修补的输出 envoy.filters.http.compressor

配置格式

以下部分提供了 EnvoyFilter 资源中 Compressor Envoy 过滤器的已弃用配置和现代化配置的示例。

已弃用的配置示例

如果您的 EnvoyFilter 的补丁部分类似于以下代码段,则表示它使用的是已弃用的格式:

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: compressor-filter-update
  namespace: istio-system
spec:
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: GATEWAY
      listener:
        filterChain:
          filter:
            name: envoy.filters.network.http_connection_manager
            subFilter:
              name: envoy.filters.http.router
    patch:
      operation: INSERT_BEFORE
      value:
        name: envoy.filters.http.compressor
        typed_config:
          '@type': type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor
          # These top-level fields are DEPRECATED
          min_content_length: 1024
          content_type:
          - "application/javascript"
          - "application/json"
          disable_on_etag_header: true
          remove_accept_encoding_header: true
          compressor_library:
            name: gzip
            typed_config:
              '@type': type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip

现代化配置示例

已弃用的字段必须移至 response_direction_config 对象(或 request_direction_config,如果适用):

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: compressor-filter-update
  namespace: istio-system
spec:
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: GATEWAY
      listener:
        filterChain:
          filter:
            name: envoy.filters.network.http_connection_manager
            subFilter:
              name: envoy.filters.http.router
    patch:
      operation: INSERT_BEFORE
      value:
        name: envoy.filters.http.compressor
        typed_config:
          '@type': type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor
          compressor_library:
            name: gzip
            typed_config:
              '@type': type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip
          response_direction_config:
            disable_on_etag_header: true # MOVED
            remove_accept_encoding_header: true # MOVED
            common_config:
              min_content_length: 1024  # MOVED
              content_type:          # MOVED
              - "application/javascript"
              - "application/json"
              enabled:
                default_value: true
                runtime_key: "compressor.enabled"

支持的字段和迁移路径

如需查看支持的字段的完整列表,请参阅使用 EnvoyFilter 实现数据平面可扩展性指南。下表详细说明了迁移最常见的已弃用字段的映射。

已弃用的字段路径 现代字段路径 备注
typed_config.min_content_length typed_config.response_direction_config.common_config.min_content_length

typed_config.request_direction_config.common_config.min_content_length
分别设置触发压缩的最小响应大小或请求大小。
typed_config.content_length typed_config.response_direction_config.common_config.min_content_length

typed_config.request_direction_config.common_config.min_content_length
min_content_length 的早期别名。在新路径中重命名为 min_content_length
typed_config.content_type typed_config.response_direction_config.common_config.content_type

typed_config.request_direction_config.common_config.content_type
要压缩的内容类型的数组。
typed_config.disable_on_etag_header typed_config.response_direction_config.disable_on_etag_header 如果响应包含 ETag 标头,则停用压缩。
typed_config.remove_accept_encoding_header typed_config.response_direction_config.remove_accept_encoding_header 在调度到上游之前,从请求中移除 Accept-Encoding 标头。

迁移计划

我们建议您按照组织的标准部署和测试最佳实践更新 EnvoyFilter 配置。在迁移过程中,请考虑以下一般步骤:

  • 识别:使用包含已弃用字段的压缩器过滤器查找所有 EnvoyFilter 资源,如识别已弃用的配置中所述。
  • 测试:修改 EnvoyFilter 资源的 YAML,并在预生产环境中测试更改。验证压缩是否适用于预期内容类型和大小。
  • 监控和验证
    • 检查 EnvoyFilter 资源状态,确认 found usage of unsupported fields: [...] 警告或错误不再存在。
    • 监控关键指标:CPU 用量、延迟时间和带宽消耗。
    • 检查响应标头(例如 Content-Encoding: gzip),确认是否已压缩。
  • 部署:将支持的 EnvoyFilter 配置应用于生产工作负载。

现代化改造的优势

  • 资源状态清晰度:从压缩器 EnvoyFilter 资源状态中移除了 found usage of unsupported fields: [...] 警告和错误。
  • 标准化:与当前的 Envoy 配置最佳实践和 TRAFFIC_DIRECTOR 验证保持一致。
  • 未来兼容性:确保您的配置可与即将推出的 Envoy 和 Cloud Service Mesh 版本无缝搭配使用。

问题排查和支持

如果您遇到问题,请考虑以下事项:

  • 仔细检查 YAML 语法和字段放置位置。
  • 检查 Envoy 代理日志,了解详细的错误消息:kubectl logs -l app=your-app -c istio-proxy -n your-namespace
  • 如有必要,回滚到之前的 EnvoyFilter 配置。
  • 如需获取更多帮助,请与 Google Cloud 支持团队联系。