Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gzip in gke with nginx-ingress

I'm trying to configure gzip to work in a python application that runs on a kubernetes with nginx-ingress in GKE. But I discovered that it is no use to enable gzip in the ingress-controller config-map because I need to enable compression on the backend as I understand it.

How can I enable compression on the backend of my python application to run gzip on nginx controller?

My main problem is that searching here in stackoverflow I know I need to put the compression in the backend, just do not know how to do this.

like image 246
Jonatas Oliveira Avatar asked Mar 05 '23 16:03

Jonatas Oliveira


2 Answers

Focusing specifically on the title of this question and extending on the example of such setup as pointed by user @Raunak Jhawar.

You can configure your nginx-ingress to compress the data by updating the ingress-nginx-controller configmap.

This would work on the path:

  • Pod ----> NGINX Ingress controller - GZIP -> Client (Web browser)

To enable such setup you will need to edit the Ingress controller configmap like below:

  • $ kubectl edit configmap -n ingress-nginx ingress-nginx-controller
data: # ADD IF NOT PRESENT
  use-gzip: "true" # ENABLE GZIP COMPRESSION
  gzip-types: "*" # SPECIFY MIME TYPES TO COMPRESS ("*" FOR ALL) 

You can find more reference and options to configure by following below link:

  • Kubernetes.github.io: Ingress NGINX: User guide: Nginx-configuration: Configmap

A side note!

You can also use other methods of editing resources like: $ kubectl patch

This changes would make the nginx-ingress-controller Pod to be automatically reconfigured.

I've included an example of such setup below.


To check if the compression occurs and if it's working I've used following setup:

  • NGINX Ingress Controller spawned by:
    • Kubernetes.github.io: Ingress NGINX: Deploy: GCE-GKE
  • NGINX pod with a 5mb.txt file filled with 0's
  • Service and Ingress resource that will expose the NGINX pod with NGINX Ingress Controller

You can check if your setup with nginx-ingress supoorts gzip compression by either:

  • Checking with Developer tools with a browser of your choosing:

    • Chrome -> F12 -> Network -> Go to site (or refresh) and press on example file (look on Response Header):

    RESPONSE HEADER

  • You can also use curl command like below (source):

    • $ curl $URL$ --silent --write-out "%{size_download}\n" --output /dev/null - get size without compression
    • $ curl $URL$ --silent -H "Accept-Encoding: gzip,deflate" --write-out "%{size_download}\n" --output /dev/null - get the size with compression (if supported)

Above methods have shown the compression rate of about 99% (5MB file compressed to 50KB)


I also encourage you to check below links for additional reference:

like image 84
Dawid Kruk Avatar answered Mar 11 '23 08:03

Dawid Kruk


This makes this as a nginx conversation. There are a lot of docs available which guides on hows and whats with nginx compression. Essentially, you have to add a bunch of gzip related settings and options in your nginx configuration file.

The tread shared above is a good starter and shows exactly what needs to be on the nginx conf. Please note that, as a good practice, do not compress any/all inbound packets as this essentially adds more compute burden on the CPU. The nginx conf has one option to only compress packets which exceed certain size.

like image 34
Raunak Jhawar Avatar answered Mar 11 '23 10:03

Raunak Jhawar