2

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
  • 121
  • 1
  • 3

2 Answers2

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,666
  • 2
  • 16
  • 20
0

We have k8s-ingress aka Viking as a Varnish-Based Ingress Controller, which integrates the ingress controller function with anything Varnish-Cache has to offer, of which Caching is one feature.

Viking uses the built-in VCL by default, so if your services generates proper Cache-Control headers, caching should work out of the box. In cases where it does not, you can use custom VCL for fine grained control.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/545547) – Stuggi Mar 09 '23 at 07:19
  • I tried to make the answer more helpful, but did not notice that I was not logged in. – Nils Goroll Mar 09 '23 at 11:01