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實戰公眾號。