When microservices communicate, response payloads can consume significant bandwidth, especially for text-based content like JSON, HTML, and CSS. ASMCompressor lets you add compression filters at the proxy layer so that all services share a single, consistent compression configuration instead of implementing compression separately in each language or framework. This topic describes how to use ASMCompressor in ASM to define compression settings for inter-service calls.
Prerequisites
An ACK cluster has been added to an ASM instance that is version 1.21 or later. For more information, see Add a cluster to an ASM instance and Upgrade an ASM instance.
The Bookinfo sample application is deployed to the ASM instance.
Istio resources are configured to route traffic between different service versions.
Feature overview
The proxy supports two compression algorithms: Gzip and Brotli.
Gzip is a widely used compression algorithm supported by many web servers and browsers, known for its high compression ratio and fast compression speed. It is typically used to compress both static and dynamic content.
Brotli is an advanced compression algorithm that provides a higher compression ratio and faster decompression speed, and is widely used on the web. Brotli is typically used to compress static content, such as HTML, CSS, and JavaScript files.
Brotli offers a higher compression ratio than Gzip but may compress more slowly.
Enable and verify Gzip compression
Step 1: Record the baseline response size
Obtain the ingress gateway address.
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
On the Ingress Gateway page, obtain the Service address.
-
In your browser's address bar, enter
http://{ASM_GATEWAY_ADDRESS}/productpageto access the Bookinfo application. Replace {ASM_GATEWAY_ADDRESS} with the address you obtained.Open your browser's developer tools and check the size of the response for the
productpagerequest.The response size is 5.5 KB.
Step 2: Enable Gzip compression
Create a file named
ingressgateway-gzip.yamlwith the following content.For a description of the fields, see ASMCompressor CRD reference.
Run the following command to enable Gzip compression:
kubectl apply -f ingressgateway-gzip.yaml
Step 3: Verify Gzip compression
In your browser's address bar, enter
http://{ASM_GATEWAY_ADDRESS}/productpageto access the Bookinfo application.Open your browser's developer tools and check the size of the response for the
productpagerequest.The response size drops from 5.5 KB to 1.8 KB. This confirms that Gzip compression is enabled.
Step 4: Verify Brotli compression
Modify the
bookinfoVirtualService.Add the following content under
spec.httpin the bookinfo.yaml file.- directResponse: body: string: >- Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! status: 200 match: - uri: prefix: /helloworldThe updated YAML content is as follows:
Run the following command to apply the VirtualService:
kubectl apply -f bookinfo.yaml
Enable Brotli compression.
Create a file named
ingressgateway-brotli.yamlwith the following content.For a description of the fields, see ASMCompressor CRD reference.
Run the following command to enable Brotli compression:
kubectl apply -f ingressgateway-brotli.yaml
Verify that Brotli compression is enabled.
Run the following command to save the response from the
/helloworldpath to theout.brfile:curl -I -H "Accept-Encoding: br" http://{ASM_GATEWAY_ADDRESS}/helloworld > out.brInstall Brotli.
macOS: Run the
brew install brotlicommand to install.On Windows, see the installation instructions at brotli.
Run the
brotlicommand to decompress theout.brfile:brotli -d out.brThe creation of a decompressed
outfile with the correct content confirms that Brotli compression is enabled.
Step 5: Verify per-route configuration
After the configuration in Step 4, compression is enabled for responses on all paths. You can set the gateway IP as an environment variable and run the following commands:
export ASM_GATEWAY_IP=<your_gateway_ip> curl ${ASM_GATEWAY_IP}/helloworld > helloworld_no_compress.html curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/helloworld > helloworld_compress.html curl ${ASM_GATEWAY_IP}/productpage > productpage_no_compress.html curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/productpage > productpage_compress.htmlRun the
lscommand to compare the response sizes for these two paths before and after compression:ls -lh total 40 -rw-r--r--@ 1 user staff 37B 5 28 18:16 helloworld_compress.html -rw-r--r--@ 1 user staff 324B 5 28 18:15 helloworld_no_compress.html -rw-r--r--@ 1 user staff 1.2K 5 28 18:17 productpage_compress.html -rw-r--r--@ 1 user staff 5.2K 5 28 18:16 productpage_no_compress.htmlIn the VirtualService, add a name to the helloworld route. The modified
bookinfo.yamlfile is as follows:Add a per-route configuration to the ASMCompressor and disable compression on the helloworld route. The modified ASMCompressor is as follows:
apiVersion: istio.alibabacloud.com/v1beta1 kind: ASMCompressor metadata: name: ingressgateway-gzip namespace: istio-system spec: compressor_library: brotli: quality: 5 window_bits: 15 isGateway: true portNumber: 80 request_direction_config: # Disable request compression. common_config: enabled: default_value: false runtime_key: NOT_EXISTENT_KEY response_direction_config: common_config: content_type: - application/json - text/plain - text/html min_content_length: 100 disable_on_etag_header: true per_route_configs: # Added per-route configuration. - disabled: true route_match: vhost: route: name: helloworld-route # Enter the route name configured in the VirtualService here. workloadSelector: labels: istio: ingressgatewayRun the following command to verify that compression is disabled for responses from the
/helloworldpath:curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/helloworld > helloworld_route_disabled.htmlRun the
lscommand again to check the current response size of the /helloworld path.ls -lh total 48 -rw-r--r--@ 1 user staff 37B 5 28 18:16 helloworld_compress.html -rw-r--r--@ 1 user staff 324B 5 28 18:15 helloworld_no_compress.html -rw-r--r--@ 1 user staff 324B 5 28 19:29 helloworld_route_disabled.html -rw-r--r--@ 1 user staff 1.3K 5 28 19:30 productpage_compress.html -rw-r--r--@ 1 user staff 5.2K 5 28 18:16 productpage_no_compress.htmlThis confirms that route-level compression settings work as expected.