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

1使用场景

1.1gateway网关

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

1.2mesh内部

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

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

2实操

2.1网关配置压缩

2.1.1示例1

cat << EOF > ef-ingressgateway-http-filter-compression.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   namespace: istio-system   name: apply-to spec:   workloadSelector:     labels:       istio: ingressgateway   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             response_direction_config:               common_config:                 min_content_length: 100                 content_type:                 - text/html             compressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip                 memory_level: 3                 window_bits: 10                 compression_level: BEST_COMPRESSION                 compression_strategy: DEFAULT_STRATEGY EOF 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提高压缩参数

cat << EOF > ef-ingressgateway-http-filter-compression-2.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   namespace: istio-system   name: apply-to spec:   workloadSelector:     labels:       istio: ingressgateway   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             response_direction_config:               common_config:                 min_content_length: 100                 content_type:                 - text/html             compressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip                 memory_level: 9                 window_bits: 15                 compression_level: BEST_COMPRESSION                 compression_strategy: DEFAULT_STRATEGY EOF kubectl apply -f ef-ingressgateway-http-filter-compression-2.yaml  -n istio-system 

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

2.1.3最快压缩速度

cat << EOF > ef-ingressgateway-http-filter-compression-3.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   namespace: istio-system   name: apply-to spec:   workloadSelector:     labels:       istio: ingressgateway   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             response_direction_config:               common_config:                 min_content_length: 100                 content_type:                 - text/html             compressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip                 memory_level: 9                 window_bits: 15                 compression_level: BEST_SPEED                 compression_strategy: DEFAULT_STRATEGY EOF kubectl apply -f ef-ingressgateway-http-filter-compression-3.yaml  -n istio-system 

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

2.1.4请求启用压缩

cat << EOF > ef-ingressgateway-http-filter-compression-4.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   namespace: istio-system   name: apply-to spec:   workloadSelector:     labels:       istio: ingressgateway   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             response_direction_config:               common_config:                 min_content_length: 100                 content_type:                 - text/html             request_direction_config:               common_config:                 enabled:                   default_value: true                   runtime_key: request_compressor_enabled             compressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip                 memory_level: 9                 window_bits: 15                 compression_level: BEST_SPEED                 compression_strategy: DEFAULT_STRATEGY EOF kubectl apply -f ef-ingressgateway-http-filter-compression-4.yaml  -n istio-system 

request_direction_config配置请求压缩

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

cat << EOF > ef-ingressgateway-http-filter-compression-5.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   namespace: istio-system   name: apply-to spec:   workloadSelector:     labels:       istio: ingressgateway   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             response_direction_config:               common_config:                 enabled:                   default_value: false                   runtime_key: response_compressor_enabled                 min_content_length: 100                 content_type:                 - text/html             request_direction_config:               common_config:                 enabled:                   default_value: true                   runtime_key: request_compressor_enabled             compressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip                 memory_level: 9                 window_bits: 15                 compression_level: BEST_SPEED                 compression_strategy: DEFAULT_STRATEGY EOF kubectl apply -f ef-ingressgateway-http-filter-compression-5.yaml  -n istio-system 

2.2mesh内部配置压缩

reviews,ratings之间启用压缩

cat << EOF > ef-ratings-http-filter-compression.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   name: ratings spec:   workloadSelector:     labels:       app: ratings   configPatches:     - applyTo: HTTP_FILTER       match:         context: SIDECAR_INBOUND         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             response_direction_config:               common_config:                 enabled:                   default_value: true                   runtime_key: response_compressor_enabled                 min_content_length: 10                 content_type:                 - application/json             request_direction_config:               common_config:                 enabled:                   default_value: true                   runtime_key: request_compressor_enabled             compressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip                 memory_level: 9                 window_bits: 12                 compression_level: BEST_SPEED                 compression_strategy: DEFAULT_STRATEGY EOF kubectl apply -f ef-ratings-http-filter-compression.yaml  -n istio 

raings启用了压缩

reviews启用解压缩

cat << EOF > ef-reviews-http-filter-compression.yaml  apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata:   name: reviews spec:   workloadSelector:     labels:       app: reviews   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.filters.http.decompressor           typed_config:             "@type": type.googleapis.com/envoy.extensions.filters.http.decompressor.v3.Decompressor             response_direction_config:               common_config:                 enabled:                   default_value: true                   runtime_key: response_decompressor_enabled             request_direction_config:               common_config:                 enabled:                   default_value: false                   runtime_key: request_decompressor_enabled             decompressor_library:               name: text_optimized               typed_config:                 "@type": type.googleapis.com/envoy.extensions.compression.gzip.decompressor.v3.Gzip                 chunk_size: 4096                 window_bits: 15 EOF 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实战公众号。

人工智能
上一篇:把握历史性时刻,谷歌决意投资GPU计算
下一篇:联想创新开放日:计算引领+AI赋能,联想超十项绿色技术重磅亮相​