1

How can I cache HTTP responses from my services in Kubernetes?

I have a simple web service in my cluster and am wondering how I could cache static assets (static html, images, fonts, etc.) beyond relying on client caches.

My setup is very simple:

 ┌─────────────────┐    ┌─────────────┐   ┌─────────────────┐
 │                 │    │             │   │                 │
 │  ingress-nginx  ├────►     svc     ├───►   deployment    │
 │                 │    │             │   │                 │
 └─────────────────┘    └─────────────┘   └─────────────────┘

Options I've considered:

  • external CDN (e.g. Cloudflare)
    • => ruled out due to data protection compliance rules
  • Cloud provider's CDN (e.g. Cloudfront)
    • => our cloud provider doesn't have such a service
  • proxy_cache in the ingress-nginx-controller & ingress
    • => seems… messy?
  • a dedicated caching service (e.g. Varnish) between ingress-nginx and my service
    • => is this a good idea?
    • => are there more "cloud-native" choices than configuring my own Varnish deployment?
  • a caching proxy in a sidecar (e.g. Varnish or nginx)
    • => not ideal because cache pods have to scale in line with application pods
  • caching in the application
    • => I'd prefer keeping this concern out of the application

I'm curious: how are people solving this problem in their clusters?

nfelger
  • 111
  • 2

1 Answers1

1

How can I cache HTTP responses from my services in Kubernetes?

You can always set custom nginx configurations via nginx.ingress.kubernetes.io/server-snippet annotations. You may want to add several proxy_cache related configurations to do that.

how I could cache static assets (static html, images, fonts, etc.) beyond relying on client caches.

Separate your application and your static assets.

Store and run your application in Kubernetes, and store your static assets somewhere else that supports public file access.

You can then use any CDN to deliver your static assets to your clients without burdening your Kubernetes applications.

mforsetti
  • 2,488
  • 2
  • 14
  • 20