Istio實(shí)現(xiàn)非侵入壓縮,微服務(wù)之間如何實(shí)現(xiàn)壓縮
1使用場(chǎng)景
1.1gateway網(wǎng)關(guān)
用戶瀏覽器訪問(wèn)網(wǎng)頁(yè)時(shí),在gateway網(wǎng)關(guān)配置壓縮,減少傳輸數(shù)據(jù),加快網(wǎng)頁(yè)打開(kāi)速度。
1.2mesh內(nèi)部
微服務(wù)相互通信時(shí),特別是用了rest協(xié)議,即用http協(xié)議通信,配置壓縮和解壓,可以有效加快數(shù)據(jù)傳輸速度,減少網(wǎng)路延遲
這個(gè)很有用,比如如果我們的rpc協(xié)議是http,啟用壓縮就可以提高傳輸效率。
2實(shí)操
2.1網(wǎng)關(guān)配置壓縮
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
配置參數(shù)說(shuō)明:
作用在http_filter上,type_url是固定的。response_direction_config對(duì)響應(yīng)做配置,min_content_length最小啟用壓縮大小,content_type對(duì)哪些類型啟用壓縮。compressor_library壓縮庫(kù)配置,
window_bits:
窗口位大小,值從9到15,大的值會(huì)有更好的壓縮,但內(nèi)存消耗更大,默認(rèn)是12,將產(chǎn)生4096字節(jié)窗口
compression_level
壓縮級(jí)別,將影響壓縮速度和壓縮大小。BEST,高壓縮,高延遲;SPEED低壓縮,低延遲;DEFAULT優(yōu)化的壓縮,將介于BEST和SPEED之間。默認(rèn)沒(méi)設(shè)置是DEFAULT.
memory_level
內(nèi)存級(jí)別,從1到9,控制壓縮庫(kù)內(nèi)存的使用量,值越高內(nèi)存用的多,但是更快,壓縮結(jié)果更好。默認(rèn)值是5.
compression_strategy:
DEFAULT , FILTERED , HUFFMAN , RLE
content_type:
默認(rèn)值 “application/javascript”, “application/json”, “application/xhtml+xml”, “image/svg+xml”, “text/css”, “text/html”, “text/plain”, “text/xml”
沒(méi)啟用壓縮前:
傳輸大小是4.6k
啟用壓縮后:
content-encoding為gzip,說(shuō)明啟用了gzip壓縮
大小由4.6k降到了1.9k
2.1.2提高壓縮參數(shù)
- 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
提高參數(shù)后傳輸數(shù)據(jù)從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請(qǐng)求啟用壓縮
- 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配置請(qǐng)求壓縮
2.1.5禁用響應(yīng)壓縮,只用請(qǐng)求壓縮
- 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內(nèi)部配置壓縮
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,解壓的窗口位大小需要大于等于壓縮的窗口位大小。默認(rèn)值是15
- chunk_size
塊大小,用于輸出緩存,默認(rèn)值是4096
value must be inside range [4096, 65536]
本文轉(zhuǎn)載自微信公眾號(hào)「k8s實(shí)戰(zhàn)」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系k8s實(shí)戰(zhàn)公眾號(hào)。