This is a simple minimal but highly configurable Varnish caching service for Kubernetes. This should be placed between your ingress and your application service.
It can be used in combination with multiple ingresses and application services at the same time.
Apply the following yaml file, replicas and environment variables can be adjusted to your need. This will deploy the Varnish service and Varnish proxy pods. The container is now based up on Alpine Linux and Varnish 6.4
Create a secret for Varnish cli admin operations
kubectl create secret generic varnish-secret --from-literal=secret=$(head -c32 /dev/urandom | base64)
Varnish vcl config
You will need to provide your custom vcl file, you can put this in a configmap the following way.
kubectl create configmap varnish-vcl --from-file=default.vcl
A very simple vcl example config file, here it wil take the request hostname and transfer it to a backend, in the backend we define the Kubernetes service name. No need for multiple backend, because Kubernetes does the load balancing if there are multiple pods defined.
Setup Ingress point service to Varnish
In your ingress, point to the varnish service. Multiple ingresses can be all pointing the the same varnish service, you will need to apply the proper routing in the varnish config to point everything to the correct application services.
Optional Metrics: telegraf sidecar
I am using Telegraf, InfluxDB and Grafana everywhere for metrics.
If you want metrics from Varnish, I created a sidecar container that will pull the metrics from Varnish and send it to InfluxDB. Don't forget to adjust the hostAlias in the yaml file to point it to your InfluxDB instance.