Compression
Reduce response payload sizes and decompress incoming request bodies with gzip compression.
About gzip compression
Gzip is an HTTP option that enables your gateway proxy to compress response data or decompress request data upon client request. Compression is useful in situations where large payloads need to be transmitted without compromising the response time.
Use the TrafficPolicy resource to configure gzip compression and decompression per route. Choose between the following options:
-
Response compression: When enabled on a route, kgateway compresses HTTP responses by using gzip when the downstream client includes an
Accept-Encoding: gzipheader. The following content types are compressed by default:application/javascriptapplication/jsonapplication/xhtml+xmlimage/svg+xmltext/csstext/htmltext/plaintext/xml
-
Request decompression: When enabled on a route, kgateway decompresses gzip-encoded request bodies before forwarding them to the backend service.
For more information about how Envoy handles compression, see the Envoy compressor filter docs.
Before you begin
-
Follow the Get started guide to install kgateway.
-
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
-
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/http -n kgateway-system 8080:8080
Response compression
Enable gzip compression on a route so that kgateway compresses responses for clients that support it.
-
Create an HTTPRoute that routes requests from the
compression.exampledomain to the httpbin sample app.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-compression namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - compression.example rules: - backendRefs: - name: httpbin port: 8000 EOF -
Create a TrafficPolicy that enables response compression on the route.
kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: TrafficPolicy metadata: name: response-compression namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin-compression compression: responseCompression: {} EOFSetting Description spec.targetRefsThe HTTPRoute to apply this policy to. spec.compression.responseCompressionEnables gzip response compression. Responses are only compressed when the client sends an Accept-Encoding: gzipheader and the response content type matches one of the supported types. -
Send a request to the
/htmlhttpbin path with theAccept-Encoding: gzipheader. Verify that the response includes acontent-encoding: gzipheader, indicating that the response body is compressed.curl -vik http://$INGRESS_GW_ADDRESS:8080/html \ -H "host: compression.example:8080" \ -H "Accept-Encoding: gzip"curl -vik http://localhost:8080/html \ -H "host: compression.example" \ -H "Accept-Encoding: gzip"Example output:
< HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: text/html; charset=utf-8 content-type: text/html; charset=utf-8 < content-encoding: gzip content-encoding: gzip < transfer-encoding: chunked transfer-encoding: chunked < server: envoy server: envoy -
Send the same request without the
Accept-Encoding: gzipheader. Verify that the response does not include acontent-encoding: gzipheader, confirming that compression is only applied when the client requests it.curl -vik http://$INGRESS_GW_ADDRESS:8080/html \ -H "host: compression.example:8080"curl -vik http://localhost:8080/html \ -H "host: compression.example"Example output:
< HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: text/html; charset=utf-8 content-type: text/html; charset=utf-8 < content-length: 3741 content-length: 3741 < server: envoy server: envoy
Request decompression
Enable gzip decompression on a route so that kgateway decompresses request bodies before forwarding them to the backend service. This setup is useful when clients send gzip-encoded request bodies.
-
Create an HTTPRoute that routes requests from the
decompression.exampledomain to the httpbin sample app.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-decompression namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - decompression.example rules: - backendRefs: - name: httpbin port: 8000 EOF -
Create a TrafficPolicy that enables request decompression on the route.
kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: TrafficPolicy metadata: name: request-decompression namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin-decompression compression: requestDecompression: {} EOFSetting Description spec.targetRefsThe HTTPRoute to apply this policy to. spec.compression.requestDecompressionEnables gzip request decompression. When a request arrives with a Content-Encoding: gzipheader, kgateway decompresses the body before forwarding the request to the backend. -
Create a gzip-compressed payload to use in the test.
echo -n 'Hello, world!' | gzip > /tmp/payload.gz -
Send the compressed payload to the
/posthttpbin path with theContent-Encoding: gzipheader. Verify that thedatafield in the response shows the decompressed stringHello, world!, confirming that kgateway decompressed the request body before forwarding it to httpbin.curl -vik -X POST http://$INGRESS_GW_ADDRESS:8080/post \ -H "host: decompression.example:8080" \ -H "Content-Type: text/plain" \ -H "Content-Encoding: gzip" \ --data-binary @/tmp/payload.gzcurl -vik -X POST http://localhost:8080/post \ -H "host: decompression.example" \ -H "Content-Type: text/plain" \ -H "Content-Encoding: gzip" \ --data-binary @/tmp/payload.gzExample output:
{ ... "data": "Hello, world!", "headers": { "Content-Type": [ "text/plain" ], ... } }
Cleanup
You can remove the resources that you created in this guide.kubectl delete TrafficPolicy response-compression -n httpbin
kubectl delete httproute httpbin-compression -n httpbin
kubectl delete TrafficPolicy request-decompression -n httpbin
kubectl delete httproute httpbin-decompression -n httpbin