Istio实现非侵入压缩,微服务之间如何实现压缩

开发 架构
微服务相互通信时,特别是用了rest协议,即用http协议通信,配置压缩和解压,可以有效加快数据传输速度,减少网路延迟

[[442125]]

1使用场景

1.1gateway网关

用户浏览器访问网页时,在gateway网关配置压缩,减少传输数据,加快网页打开速度。

1.2mesh内部

微服务相互通信时,特别是用了rest协议,即用http协议通信,配置压缩和解压,可以有效加快数据传输速度,减少网路延迟

这个很有用,比如如果我们的rpc协议是http,启用压缩就可以提高传输效率。

2实操

2.1网关配置压缩

2.1.1示例1

  1. cat << EOF > ef-ingressgateway-http-filter-compression.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   namespace: istio-system 
  6.   name: apply-to 
  7. spec: 
  8.   workloadSelector: 
  9.     labels: 
  10.       istio: ingressgateway 
  11.   configPatches: 
  12.     - applyTo: HTTP_FILTER 
  13.       match: 
  14.         context: GATEWAY 
  15.         listener: 
  16.           filterChain: 
  17.             filter: 
  18.               name: envoy.filters.network.http_connection_manager 
  19.               subFilter: 
  20.                 name: envoy.filters.http.router 
  21.       patch: 
  22.         operation: INSERT_BEFORE 
  23.         value: 
  24.           name: envoy.filters.http.compressor 
  25.           typed_config: 
  26.             "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor 
  27.             response_direction_config: 
  28.               common_config: 
  29.                 min_content_length: 100 
  30.                 content_type: 
  31.                 - 'text/html' 
  32.             compressor_library: 
  33.               name: text_optimized 
  34.               typed_config: 
  35.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip 
  36.                 memory_level: 3 
  37.                 window_bits: 10 
  38.                 compression_level: BEST_COMPRESSION 
  39.                 compression_strategy: DEFAULT_STRATEGY 
  40. EOF 
  41.  
  42. kubectl apply -f ef-ingressgateway-http-filter-compression.yaml  -n istio-system 

配置参数说明:

作用在http_filter上,type_url是固定的。response_direction_config对响应做配置,min_content_length最小启用压缩大小,content_type对哪些类型启用压缩。compressor_library压缩库配置,

window_bits:

窗口位大小,值从9到15,大的值会有更好的压缩,但内存消耗更大,默认是12,将产生4096字节窗口

compression_level

压缩级别,将影响压缩速度和压缩大小。BEST,高压缩,高延迟;SPEED低压缩,低延迟;DEFAULT优化的压缩,将介于BEST和SPEED之间。默认没设置是DEFAULT.

memory_level

内存级别,从1到9,控制压缩库内存的使用量,值越高内存用的多,但是更快,压缩结果更好。默认值是5.

compression_strategy:

DEFAULT , FILTERED , HUFFMAN , RLE

content_type:

默认值 “application/javascript”, “application/json”, “application/xhtml+xml”, “image/svg+xml”, “text/css”, “text/html”, “text/plain”, “text/xml”

没启用压缩前:

传输大小是4.6k

启用压缩后:

content-encoding为gzip,说明启用了gzip压缩

大小由4.6k降到了1.9k

2.1.2提高压缩参数

  1. cat << EOF > ef-ingressgateway-http-filter-compression-2.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   namespace: istio-system 
  6.   name: apply-to 
  7. spec: 
  8.   workloadSelector: 
  9.     labels: 
  10.       istio: ingressgateway 
  11.   configPatches: 
  12.     - applyTo: HTTP_FILTER 
  13.       match: 
  14.         context: GATEWAY 
  15.         listener: 
  16.           filterChain: 
  17.             filter: 
  18.               name: envoy.filters.network.http_connection_manager 
  19.               subFilter: 
  20.                 name: envoy.filters.http.router 
  21.       patch: 
  22.         operation: INSERT_BEFORE 
  23.         value: 
  24.           name: envoy.filters.http.compressor 
  25.           typed_config: 
  26.             "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor 
  27.             response_direction_config: 
  28.               common_config: 
  29.                 min_content_length: 100 
  30.                 content_type: 
  31.                 - 'text/html' 
  32.             compressor_library: 
  33.               name: text_optimized 
  34.               typed_config: 
  35.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip 
  36.                 memory_level: 9 
  37.                 window_bits: 15 
  38.                 compression_level: BEST_COMPRESSION 
  39.                 compression_strategy: DEFAULT_STRATEGY 
  40. EOF 
  41.  
  42. kubectl apply -f ef-ingressgateway-http-filter-compression-2.yaml  -n istio-system 

提高参数后传输数据从1.9k下降到1.8k

2.1.3最快压缩速度

  1. cat << EOF > ef-ingressgateway-http-filter-compression-3.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   namespace: istio-system 
  6.   name: apply-to 
  7. spec: 
  8.   workloadSelector: 
  9.     labels: 
  10.       istio: ingressgateway 
  11.   configPatches: 
  12.     - applyTo: HTTP_FILTER 
  13.       match: 
  14.         context: GATEWAY 
  15.         listener: 
  16.           filterChain: 
  17.             filter: 
  18.               name: envoy.filters.network.http_connection_manager 
  19.               subFilter: 
  20.                 name: envoy.filters.http.router 
  21.       patch: 
  22.         operation: INSERT_BEFORE 
  23.         value: 
  24.           name: envoy.filters.http.compressor 
  25.           typed_config: 
  26.             "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor 
  27.             response_direction_config: 
  28.               common_config: 
  29.                 min_content_length: 100 
  30.                 content_type: 
  31.                 - 'text/html' 
  32.             compressor_library: 
  33.               name: text_optimized 
  34.               typed_config: 
  35.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip 
  36.                 memory_level: 9 
  37.                 window_bits: 15 
  38.                 compression_level: BEST_SPEED 
  39.                 compression_strategy: DEFAULT_STRATEGY 
  40. EOF 
  41.  
  42. kubectl apply -f ef-ingressgateway-http-filter-compression-3.yaml  -n istio-system 

BEST_SPEED传输大小从1.8k提升到1.9k

2.1.4请求启用压缩

  1. cat << EOF > ef-ingressgateway-http-filter-compression-4.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   namespace: istio-system 
  6.   name: apply-to 
  7. spec: 
  8.   workloadSelector: 
  9.     labels: 
  10.       istio: ingressgateway 
  11.   configPatches: 
  12.     - applyTo: HTTP_FILTER 
  13.       match: 
  14.         context: GATEWAY 
  15.         listener: 
  16.           filterChain: 
  17.             filter: 
  18.               name: envoy.filters.network.http_connection_manager 
  19.               subFilter: 
  20.                 name: envoy.filters.http.router 
  21.       patch: 
  22.         operation: INSERT_BEFORE 
  23.         value: 
  24.           name: envoy.filters.http.compressor 
  25.           typed_config: 
  26.             "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor 
  27.             response_direction_config: 
  28.               common_config: 
  29.                 min_content_length: 100 
  30.                 content_type: 
  31.                 - 'text/html' 
  32.             request_direction_config: 
  33.               common_config: 
  34.                 enabled: 
  35.                   default_value: true 
  36.                   runtime_key: request_compressor_enabled 
  37.             compressor_library: 
  38.               name: text_optimized 
  39.               typed_config: 
  40.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip 
  41.                 memory_level: 9 
  42.                 window_bits: 15 
  43.                 compression_level: BEST_SPEED 
  44.                 compression_strategy: DEFAULT_STRATEGY 
  45. EOF 
  46.  
  47. kubectl apply -f ef-ingressgateway-http-filter-compression-4.yaml  -n istio-system 

request_direction_config配置请求压缩

2.1.5禁用响应压缩,只用请求压缩

  1. cat << EOF > ef-ingressgateway-http-filter-compression-5.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   namespace: istio-system 
  6.   name: apply-to 
  7. spec: 
  8.   workloadSelector: 
  9.     labels: 
  10.       istio: ingressgateway 
  11.   configPatches: 
  12.     - applyTo: HTTP_FILTER 
  13.       match: 
  14.         context: GATEWAY 
  15.         listener: 
  16.           filterChain: 
  17.             filter: 
  18.               name: envoy.filters.network.http_connection_manager 
  19.               subFilter: 
  20.                 name: envoy.filters.http.router 
  21.       patch: 
  22.         operation: INSERT_BEFORE 
  23.         value: 
  24.           name: envoy.filters.http.compressor 
  25.           typed_config: 
  26.             "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor 
  27.             response_direction_config: 
  28.               common_config: 
  29.                 enabled: 
  30.                   default_value: false 
  31.                   runtime_key: response_compressor_enabled 
  32.                 min_content_length: 100 
  33.                 content_type: 
  34.                 - 'text/html' 
  35.             request_direction_config: 
  36.               common_config: 
  37.                 enabled: 
  38.                   default_value: true 
  39.                   runtime_key: request_compressor_enabled 
  40.             compressor_library: 
  41.               name: text_optimized 
  42.               typed_config: 
  43.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip 
  44.                 memory_level: 9 
  45.                 window_bits: 15 
  46.                 compression_level: BEST_SPEED 
  47.                 compression_strategy: DEFAULT_STRATEGY 
  48. EOF 
  49.  
  50. kubectl apply -f ef-ingressgateway-http-filter-compression-5.yaml  -n istio-system 

2.2mesh内部配置压缩

reviews,ratings之间启用压缩

  1. cat << EOF > ef-ratings-http-filter-compression.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   name: ratings 
  6. spec: 
  7.   workloadSelector: 
  8.     labels: 
  9.       app: ratings 
  10.   configPatches: 
  11.     - applyTo: HTTP_FILTER 
  12.       match: 
  13.         context: SIDECAR_INBOUND 
  14.         listener: 
  15.           filterChain: 
  16.             filter: 
  17.               name: envoy.filters.network.http_connection_manager 
  18.               subFilter: 
  19.                 name: envoy.filters.http.router 
  20.       patch: 
  21.         operation: INSERT_BEFORE 
  22.         value: 
  23.           name: envoy.filters.http.compressor 
  24.           typed_config: 
  25.             "@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor 
  26.             response_direction_config: 
  27.               common_config: 
  28.                 enabled: 
  29.                   default_value: true 
  30.                   runtime_key: response_compressor_enabled 
  31.                 min_content_length: 10 
  32.                 content_type: 
  33.                 - 'application/json' 
  34.             request_direction_config: 
  35.               common_config: 
  36.                 enabled: 
  37.                   default_value: true 
  38.                   runtime_key: request_compressor_enabled 
  39.             compressor_library: 
  40.               name: text_optimized 
  41.               typed_config: 
  42.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip 
  43.                 memory_level: 9 
  44.                 window_bits: 12 
  45.                 compression_level: BEST_SPEED 
  46.                 compression_strategy: DEFAULT_STRATEGY 
  47. EOF 
  48.  
  49. kubectl apply -f ef-ratings-http-filter-compression.yaml  -n istio 

raings启用了压缩

reviews启用解压缩

  1. cat << EOF > ef-reviews-http-filter-compression.yaml  
  2. apiVersion: networking.istio.io/v1alpha3 
  3. kind: EnvoyFilter 
  4. metadata: 
  5.   name: reviews 
  6. spec: 
  7.   workloadSelector: 
  8.     labels: 
  9.       app: reviews 
  10.   configPatches: 
  11.     - applyTo: HTTP_FILTER 
  12.       match: 
  13.         context: SIDECAR_OUTBOUND 
  14.         listener: 
  15.           filterChain: 
  16.             filter: 
  17.               name: envoy.filters.network.http_connection_manager 
  18.               subFilter: 
  19.                 name: envoy.filters.http.router 
  20.       patch: 
  21.         operation: INSERT_BEFORE 
  22.         value: 
  23.           name: envoy.filters.http.decompressor 
  24.           typed_config: 
  25.             "@type": type.googleapis.com/envoy.extensions.filters.http.decompressor.v3.Decompressor 
  26.             response_direction_config: 
  27.               common_config: 
  28.                 enabled: 
  29.                   default_value: true 
  30.                   runtime_key: response_decompressor_enabled 
  31.             request_direction_config: 
  32.               common_config: 
  33.                 enabled: 
  34.                   default_value: false 
  35.                   runtime_key: request_decompressor_enabled 
  36.             decompressor_library: 
  37.               name: text_optimized 
  38.               typed_config: 
  39.                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.decompressor.v3.Gzip 
  40.                 chunk_size: 4096 
  41.                 window_bits: 15 
  42. EOF 
  43.  
  44. kubectl apply -f ef-reviews-http-filter-compression.yaml  -n istio 
  • window_bits

窗口位大小,值从9到15,解压的窗口位大小需要大于等于压缩的窗口位大小。默认值是15

  • chunk_size

块大小,用于输出缓存,默认值是4096

value must be inside range [4096, 65536]

本文转载自微信公众号「k8s实战」,可以通过以下二维码关注。转载本文请联系k8s实战公众号。

 

责任编辑:武晓燕 来源: k8s实战
相关推荐

2021-07-27 06:51:53

Istio 微服务Service Mes

2023-11-09 09:48:16

数据压缩微服务

2020-04-10 10:36:20

网络通信框架

2020-05-07 09:45:16

前端JS图片压缩

2016-04-01 10:34:29

APK压缩Android

2018-08-27 10:54:30

C++压缩存储

2024-03-18 08:48:52

Spring多端认证微服务

2020-11-15 23:48:57

服务网格微服务网络网络技术

2020-04-29 16:24:55

开发iOS技术

2011-05-20 14:03:31

哈夫曼

2011-07-27 16:26:42

iPhone 解压 gzip

2022-01-13 09:54:58

微服务 Istio 通信

2024-06-20 08:09:24

2020-10-20 11:12:11

Nodejs

2011-12-30 11:14:41

Javazip

2020-06-30 07:58:39

微服务Spring BootCloud

2023-06-05 08:00:00

mTLSIstio安全

2023-11-27 07:29:05

2018-09-14 16:18:26

Linux压缩文件应用程序

2009-11-10 12:27:04

VB.NET远程控制
点赞
收藏

51CTO技术栈公众号