istio实现非侵入压缩,微服务之间如何实现压缩
1使用场景
1gateway网关
用户浏览器访问网页时,在gateway网关配置压缩,减少传输数据,加快网页打开速度。
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最快压缩速度
compression_level: best_speed
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]