Different compression configurations are used in different programming languages and frameworks when compression is performed. You can better maintain and manage compression configurations by adding compression filters that use consistent compression configurations to your applications. The compression configurations include the minimum compressed bytes, compression level, and which responses will be compressed by default. This topic describes how to use ASMCompressor to define compression configurations for calls between application services.
Prerequisites
A Container Service for Kubernetes (ACK) cluster is added to an ASM instance whose version is 1.21 or later. For more information, see Add a cluster to an ASM instance and Update an ASM instance.
The Bookinfo application is 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 are used to route traffic to different versions of a service. For more information, see Use Istio resources to route traffic to different versions of a service.
Feature description
A waypoint proxy supports both Gzip and Brotli algorithms.
Gzip is a widely used compression algorithm that is supported by many web servers and browsers. The Gzip compression algorithm is known for its high compression ratio and fast compression speed. It is often used to compress static and dynamic content.
Brotli is an advanced compression algorithm that can provide a higher compression ratio and a faster decompression speed, and is widely used for web services. The Brotli compression algorithm is generally used to compress static content, such as HTML, Cascading Style Sheets (CSS), and JavaScript files.
Compared with Gzip, Brotli provides a higher compression ratio but may impact the compression speed. You can select a compression algorithm based on actual application scenarios and performance requirements.
Enable and verify Gzip compression
Step 1: Record the size of a response that is returned when compression is not enabled
Obtain the IP address of the ingress gateway.
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 value of Service address.
In the address bar of your browser, enter
http://{IP address of the ASM gateway}/productpage
to access the Bookinfo application.Turn on the developer mode in the browser and check the size of the response returned for the productpage request.
As shown in the following figure, the size of the returned response is 5.5 KB.
Step 2: Enable Gzip compression
Create an ingressgateway-gzip.yaml file with the following content.
For more information about the fields, see Description of ASMCompressor fields.
Run the following command to enable Gzip compression:
kubectl apply -f ingressgateway-gzip.yaml
Step 3: View the results obtained after Gzip compression is enabled
In the address bar of your browser, enter
http://{IP address of the ASM gateway}/productpage
to access the Bookinfo application.Turn on the developer mode of the browser and check the size of the response returned for the productpage request.
As shown in the following figure, the size of the returned response changes from 5.5 KB to 1.8 KB, which indicates that the Gzip compression configuration takes effect.
Step 4: Verify Brotli compression
Modify the virtual service named bookinfo.
Add the following content to the
spec.http
field of 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: /helloworld
The following code shows the content of the modified YAML file:
Run the following command to deploy the virtual service:
kubectl apply -f bookinfo.yaml
Enable Brotli compression.
Use the following content to create an ingressgateway-brotli.yaml file.
For more information about the fields, see Description of ASMCompressor fields.
Run the following command to enable Brotli compression:
kubectl apply -f ingressgateway-brotli.yaml
Check whether Brotli compression takes effect.
Run the following command to compress the response for the
/helloworld
request path by using Brotli compression and save the compressed content to the out.br file:curl -I -H "Accept-Encoding: br" http://{IP address of the ASM gateway}/helloworld > out.br
Install Brotli.
macOS: Run the
brew install brotli
command in Brew to install Brotli.Windows: For more information about the installation procedure, see brotli.
Run the following command in Brotli to decompress the out.br file:
brotli -d out.br
You can see the decompressed file and its content, which indicates that the Brotli compression configuration takes effect.
Step 5: Verify the configuration at the route level
After you complete the configuration in Step 4, compression is enabled for all paths. Use the following method to verify the configuration:
curl ${IP address of the ASM gateway}/helloworld > helloworld_no_compress.html curl -H "Accept-Encoding: br" ${IP address of the ASM gateway}/helloworld > helloworld_compress.html curl ${IP address of the ASM gateway}/productpage > productpage_no_compress.html curl -H "Accept-Encoding: br" ${IP address of the ASM gateway}/productpage > productpage_compress.html
Run the
ls
command to view the size of packets on these two paths before and after compression is performed: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.html
Modify the virtual service and add a route name to the /helloworld path. The following code shows the content of the modified bookinfo.yaml file:
Add route-level configurations in ASMCompressor and disable the compression feature on the helloworld route. The following code shows the content of the modified ASMCompressor:
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 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: # New route-level configurations - disabled: true route_match: vhost: route: name: helloworld-route # Enter the route name configured in the virtual service. workloadSelector: labels: istio: ingressgateway
Run the following command to verify that compression is disabled for the
/helloworld
path:curl -H "Accept-Encoding: br" ${IP address of the ASM gateway}/helloworld > helloworld_route_disabled.html
Run the
ls
command again to check the size of responses on 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.html
The output shows that compression is disabled at the route level.