All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use ASMCompressor to define compression configurations for calls between application services

Last Updated:Jul 01, 2024

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

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

  1. Obtain the IP address of the ingress gateway.

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose ASM Gateways > Ingress Gateway.

    3. On the Ingress Gateway page, obtain the value of Service address.

  2. In the address bar of your browser, enter http://{IP address of the ASM gateway}/productpage to access the Bookinfo application.

  3. 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.

    215BDB4C-576C-45ae-89ED-C378C13BB925.png

Step 2: Enable Gzip compression

  1. Create an ingressgateway-gzip.yaml file with the following content.

    For more information about the fields, see Description of ASMCompressor fields.

    Expand to view the ingressgateway-gzip.yaml

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: ASMCompressor
    metadata:
      name: ingressgateway-gzip
      namespace: istio-system
    spec:
      compressor_library:
        gzip:
          compression_level: BEST_COMPRESSION
          compression_strategy: DEFAULT_STRATEGY
          memory_level: 9
          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
      workloadSelector:
        labels:
          istio: ingressgateway
  2. 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

  1. In the address bar of your browser, enter http://{IP address of the ASM gateway}/productpage to access the Bookinfo application.

  2. 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.

    22.png

Step 4: Verify Brotli compression

  1. Modify the virtual service named bookinfo.

    1. 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:

      Expand to view the modified bookinfo.yaml file

      apiVersion: networking.istio.io/v1beta1
      kind: VirtualService
      metadata:
        name: bookinfo
      spec:
        gateways:
          - bookinfo-gateway
        hosts:
          - '*'
        http:
          - 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
          - match:
              - uri:
                  exact: /productpage
              - uri:
                  prefix: /static
              - uri:
                  exact: /login
              - uri:
                  exact: /logout
              - uri:
                  prefix: /api/v1/products
            route:
              - destination:
                  host: productpage
                  port:
                    number: 9080
      
    2. Run the following command to deploy the virtual service:

      kubectl apply -f bookinfo.yaml
  2. Enable Brotli compression.

    1. Use the following content to create an ingressgateway-brotli.yaml file.

      For more information about the fields, see Description of ASMCompressor fields.

      Expand to view the ingressgateway-brotli.yaml file

      apiVersion: istio.alibabacloud.com/v1beta1
      kind: ASMCompressor
      metadata:
        name: ingressgateway-brotli
        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
        workloadSelector:
          labels:
            istio: ingressgateway
    2. Run the following command to enable Brotli compression:

      kubectl apply -f ingressgateway-brotli.yaml
  3. Check whether Brotli compression takes effect.

    1. 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
    2. Install Brotli.

      • macOS: Run the brew install brotli command in Brew to install Brotli.

      • Windows: For more information about the installation procedure, see brotli.

    3. 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

  1. 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
  2. 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
  3. 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:

    Expand to view the modified YAML file

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: bookinfo
    spec:
      gateways:
        - bookinfo-gateway
      hosts:
        - '*'
      http:
        - 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
          name: helloworld-route    # Specify a name for the route. This way, the compression plug-in can match packets more easily. 
        - match:
            - uri:
                exact: /productpage
            - uri:
                prefix: /static
            - uri:
                exact: /login
            - uri:
                exact: /logout
            - uri:
                prefix: /api/v1/products
          route:
            - destination:
                host: productpage
                port:
                  number: 9080
  4. 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
  5. 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
  6. 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.