Score:1

How to implement caching of HTTP responses in Kubernetes?

sa flag

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?

Score:1
tz flag

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.

Score:0
in flag

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.

gg flag
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)
Nils Goroll avatar
in flag
I tried to make the answer more helpful, but did not notice that I was not logged in.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.