Brotli is a compression method developed by Google and released in 2015. Depending on the scenario, brotli is capable of achieving a compression rate improvement of between 20 and 30% over gzip, which is the ingress-nginx default compression method.
To create the ingress-nginx based on our provider, we can follow the documentation at the following link:
The configuration of ingress-nginx can be done through Annotations, ConfigMap or with a custom template.
In the following link we can check which parameters can be modified with each of the ways:
In this case, to modify the compression method, we will have to use the ConfigMap.
The default compression method is gzip, with a compression level of 1. To enable and configure brotli compression, we have three parameters: enable-brotli, brotli-level and brotli-types.
- enable-brotli: true o false. Allows you to enable brotli compression.
- brotli-level: compression level. It can take values between 1 and 11, the default is 4. The higher the compression level, the more aggressive the algorithms will be to compress the files, achieving smaller sizes, but in return it will be more computationally expensive.
- brotli-types: MIME types that will be compressed on the fly by brotli.
Applying the changes
Executing the following command, we will list the ConfigMap of ingress-nginx:
kubectl get cm -l app.kubernetes.io/name=ingress-nginx -A
The ‘-A’ parameter lists the resources of all the namespaces, since depending on the provider we are using, the namespace may vary. The output of the command would be something like this:
NAMESPACE NAME DATA AGE my-namespace ingress-nginx-configuration 0 14d my-namespace ingress-nginx-tcp-services 0 14d my-namespace ingress-nginx-udp-services 0 14d
The ConfigMap that we must modify is ingress-nginx-configuration.
To see the current content of the ConfigMap, we will execute the following command:
kubectl get cm ingress-nginx-configuration -n my-namespace -o yaml
To get the content, we must specify the namespace where the ingress-nginx is displayed.
We can edit this ConfigMap directly, but in this case we will create a yaml file and then apply it. The yaml file will be something like this:
Important note: all ConfigMap parameters must be strings so we must use quotation marks.
Once we have the configmap.yaml file created, with the desired values of the parameters, we will apply it executing the following command:
kubectl apply -f configmap.yaml -n my-namespace
We have to make sure we apply the ConfigMap in the same namespace where we have the ingress-nginx deployed.
Finally we will check that we have correctly applied the changes, showing again the contents of the ingress-nginx configuration ConfigMap:
With brotli compression activated, when making a request to a web exposed through the ingress-nginx, in the response headers we will receive something like the following:
And that’s it!