<p>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!</p>

tim kelly
<p>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.</p>
<p>You can find install instructions on <a href="https://www.varnish-software.com/developers/tutorials/running-varnish-docker/" rel="nofollow noreferrer">https://www.varnish-software.com/developers/tutorials/running-varnish-docker/</a>.</p>
<p>However, I have some standalone k8s files that can be a good way to get started.</p>
<h2>Config map</h2>
<pre class="lang-yaml prettyprint-override"><code>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 == &quot;/varnish-ping&quot;) {
            return(synth(200));
        }
    
        if (req.url == &quot;/varnish-ready&quot;) {
            return(synth(200));
        }

        return(synth(200,&quot;Welcome&quot;));
    }
</code></pre>
<p>This config map contains the VCL file. This VCL file doesn't do anything useful besides having <code>/varnish-ping</code> &amp; <code>/varnish-ready</code> endpoints. Please customize to your needs.</p>
<h2>Service definition</h2>
<p>Here's a basic service definition that exposes port <code>80</code></p>
<pre class="lang-yaml prettyprint-override"><code>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
</code></pre>
<h2>Deployment</h2>
<p>And finally here's the deployment. It uses the official Varnish Docker image and more specifically the <em>6.0 LTS version</em>.</p>
<p>It uses the synthetic <code>/varnish-ping</code> &amp; <code>/varnish-ready</code> endpoints and mounts the config map under <code>/etc/varnish</code> to load the VCL file.</p>
<pre class="lang-yaml prettyprint-override"><code>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: &quot;varnish:stable&quot;
          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
</code></pre>
<h2>Deploying the config</h2>
<p>Run <code>kubectl apply -f .</code> in the folder with the various k8s files (config map, service definition &amp; deployment). This is the output you'll get:</p>
<pre><code>$ kubectl apply -f .
configmap/varnish created
deployment.apps/varnish created
service/varnish created
</code></pre>
<p>By running <code>kubectl get all</code> you'll see the status of the deployment.</p>
<p>When running this on your local computer, just call <code>kubectl port-forward service/varnish 8080:80</code> to port forward the Varnish service to <code>localhost:8080</code>. This allows you to test Varnish on k8s locally by accessing <code>http://localhost:8080</code>.</p>
<p>Run <code>kubectl delete -f .</code> to tear it down again.</p>
<h2>Disclaimer</h2>
<p>Although these configs were featured in my <a href="https://www.varnish-software.com/developers/varnish-6-by-example-book/" rel="nofollow noreferrer">Varnish 6 by Example</a> book, this is not an official tutorial. These scripts can probably be improved. However, it is a simple way to get started.</p>
