When microservices communicate over HTTP, 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.
ASMCompressor supports two compression algorithms:
| Algorithm | Compression ratio | Speed | Best for |
|---|---|---|---|
| Gzip | Good | Fast compression and decompression | Both static and dynamic content. Widely supported by browsers and web servers. |
| Brotli | Higher than Gzip | Slower compression, faster decompression | Static content such as HTML, CSS, and JavaScript files. |
Prerequisites
A Container Service for Kubernetes (ACK) cluster added to a Service Mesh (ASM) instance version 1.21 or later. For more information, see Add a cluster to an ASM instance and Update an ASM instance.
The Bookinfo sample application deployed in the ASM instance. For more information, see Deploy an application in an ACK cluster that is added to an ASM instance.
Istio resources configured to route traffic to different service versions. For more information, see Use Istio resources to route traffic to different versions of a service.
Step 1: Record the baseline response size
Before enabling compression, record the uncompressed response size. This baseline lets you measure the compression ratio later.
Get the IP address of the ingress gateway.
Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose ASM Gateways > Ingress Gateway.
On the Ingress Gateway page, find the value of Service address.
Open
http://<ASM-gateway-address>/productpagein a browser.Open the browser developer tools and check the response size for the
productpagerequest. The response size is approximately 5.5 KB without compression.
Step 2: Enable Gzip compression
Create an ASMCompressor resource to enable Gzip compression on the ingress gateway.
Create a file named
ingressgateway-gzip.yamlwith the following content:For more information about the fields, see ASMCompressor field descriptions.
Apply the configuration:
kubectl apply -f ingressgateway-gzip.yaml
Step 3: Verify Gzip compression
Open
http://<ASM-gateway-address>/productpagein a browser.Open the browser developer tools and check the response size for the
productpagerequest. The response size drops from 5.5 KB to approximately 1.8 KB, confirming that Gzip compression is active.
Step 4: Enable and verify Brotli compression
This step adds a test route for the /helloworld path and applies Brotli compression to it.
Add a test route
Add the following
directResponseblock to thespec.httpsection of the Bookinfo virtual service (bookinfo.yaml):- 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 complete
bookinfo.yamlfile after modification:Apply the virtual service:
kubectl apply -f bookinfo.yaml
Create the Brotli ASMCompressor resource
Create a file named
ingressgateway-brotli.yamlwith the following content:For more information about the fields, see ASMCompressor field descriptions.
Apply the configuration:
kubectl apply -f ingressgateway-brotli.yaml
Verify Brotli compression
Send a request with the
Accept-Encoding: brheader and save the response:curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/helloworld > out.brInstall the Brotli command-line tool:
macOS:
brew install brotliWindows: Download the binary from the Brotli GitHub releases page.
Decompress the saved response:
brotli -d out.brIf decompression succeeds and the output file contains the expected content, Brotli compression is working correctly.
Step 5: Disable compression for a specific route
By default, the ASMCompressor resource applies compression to all matching responses. To disable compression on a specific route while keeping it enabled for others, use per_route_configs.
This example disables compression on the /helloworld route while keeping it active for /productpage.
Confirm that compression is active on all routes
Run the following commands to compare compressed and uncompressed response sizes for both paths:
curl http://<ASM-gateway-address>/helloworld > helloworld_no_compress.html
curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/helloworld > helloworld_compress.html
curl http://<ASM-gateway-address>/productpage > productpage_no_compress.html
curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/productpage > productpage_compress.htmlCheck the file sizes:
ls -lhExpected output:
-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.htmlBoth paths return smaller responses when compression is requested.
Add a route name to the virtual service
To target a specific route in the per-route configuration, assign a name to the route in the virtual service.
Add name: helloworld-route to the /helloworld route in bookinfo.yaml:
Add a per-route override to ASMCompressor
Update the ASMCompressor resource to disable compression on the helloworld-route:
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-direction 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: # Route-level overrides
- disabled: true
route_match:
vhost:
route:
name: helloworld-route # Must match the route name in the VirtualService
workloadSelector:
labels:
istio: ingressgatewayVerify the per-route override
Send a compressed request to the
/helloworldpath:curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/helloworld > helloworld_route_disabled.htmlCompare the file sizes:
ls -lhExpected output:
-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.htmlThe
/helloworldresponse is now 324 B (same as the uncompressed size), confirming that compression is disabled for this route. The/productpageresponse remains compressed at 1.3 KB.