Introduction
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.
ingress-nginx configuration
To create the ingress-nginx based on our provider, we can follow the documentation at the following link:
https://kubernetes.github.io/ingress-nginx/deploy/
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:
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/
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:
configmap.yaml
apiVersion: v1 kind: ConfigMap metadata: name: ingress-nginx-configuration data: enable-brotli: "true" brotli-level: "6" brotli-types: "text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap"
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.
Verification
Finally we will check that we have correctly applied the changes, showing again the contents of the ingress-nginx configuration ConfigMap:
kubectl get cm ingress-nginx-configuration -n kube-system -o yaml apiVersion: v1 data: brotli-level: "6" brotli-types: text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap enable-brotli: "true" kind: ConfigMap metadata: annotations: ...
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!
We hope you found this post useful, don’t forget that theĀ Geko’s DevOps team is at your disposal. Contact us!