I am trying to create a scalable varnish cluster on some managed Kubernetes services (azure's, google's, or amazon's Kubernetes service) but I'm having trouble getting started. Any advice or references are helpful, thanks!
CodePudding user response:
Try this Varnish on Kubernetes operator.
CodePudding user response:
We (Varnish Software) are working on official Helm charts to make k8s deployments a lot easier. For the time being we only have an official Docker Image.
You can find install instructions on https://www.varnish-software.com/developers/tutorials/running-varnish-docker/.
However, I have some standalone k8s files that can be a good way to get started.
Config map
apiVersion: v1
kind: ConfigMap
metadata:
name: varnish
labels:
name: varnish
data:
default.vcl: |
vcl 4.1;
backend default none;
sub vcl_recv {
if (req.url == "/varnish-ping") {
return(synth(200));
}
if (req.url == "/varnish-ready") {
return(synth(200));
}
return(synth(200,"Welcome"));
}
This config map contains the VCL file. This VCL file doesn't do anything useful besides having /varnish-ping
& /varnish-ready
endpoints. Please customize to your needs.
Service definition
Here's a basic service definition that exposes port 80
apiVersion: v1
kind: Service
metadata:
name: varnish
labels:
name: varnish
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: varnish-http
selector:
name: varnish
Deployment
And finally here's the deployment. It uses the official Varnish Docker image and more specifically the 6.0 LTS version.
It uses the synthetic /varnish-ping
& /varnish-ready
endpoints and mounts the config map under /etc/varnish
to load the VCL file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: varnish
labels:
name: varnish
spec:
replicas: 1
selector:
matchLabels:
name: varnish
template:
metadata:
labels:
name: varnish
spec:
containers:
- name: varnish
image: "varnish:stable"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /varnish-ping
port: 80
initialDelaySeconds: 30
periodSeconds: 5
readinessProbe:
httpGet:
path: /varnish-ready
port: 80
initialDelaySeconds: 30
periodSeconds: 5
volumeMounts:
- name: varnish
mountPath: /etc/varnish
volumes:
- name: varnish
configMap:
name: varnish
Deploying the config
Run kubectl apply -f .
in the folder with the various k8s files (config map, service definition & deployment). This is the output you'll get:
$ kubectl apply -f .
configmap/varnish created
deployment.apps/varnish created
service/varnish created
By running kubectl get all
you'll see the status of the deployment.
When running this on your local computer, just call kubectl port-forward service/varnish 8080:80
to port forward the Varnish service to localhost:8080
. This allows you to test Varnish on k8s locally by accessing http://localhost:8080
.
Run kubectl delete -f .
to tear it down again.
Disclaimer
Although these configs were featured in my Varnish 6 by Example book, this is not an official tutorial. These scripts can probably be improved. However, it is a simple way to get started.