<p>After reading some documentation regarding <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="noreferrer">Persistent Volumes</a> in Kubernetes I am wondering which one would be the best setup (storage speaking) for running a highly available ElasticSearch cluster. I am not running the typical <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-an-elasticsearch-fluentd-and-kibana-efk-logging-stack-on-kubernetes" rel="noreferrer">EFK</a> (or ELK) setup, but I am using ElasticSearch as a proper full-text search engine.</p>
<p>I've read the <a href="https://www.elastic.co/guide/en/cloud-on-k8s/current/k8s-volume-claim-templates.html" rel="noreferrer">official Elastic Documentation</a>, but I find it quite lacking of clarification. According to &quot;Kubernetes in Action&quot;, Chapter 6:</p>
<blockquote>
<p>When an application running in a pod needs to persist data to disk and
have that same data available even when the pod is rescheduled to
another node, you can’t use any of the volume types we’ve mentioned so
far. Because this data needs to be accessible from any cluster node,
it must be stored on some type of network-attached storage (NAS).</p>
</blockquote>
<p>So if I am not mistaken, I need a <code>Volume</code> and access it through <code>PersistentVolumes</code> and <code>PersistentVolumeClaim</code> with <code>Retain</code> policies.</p>
<p>When looking at <a href="https://kubernetes.io/docs/concepts/storage/volumes/" rel="noreferrer">Official Volumes</a>, I get a feeling that one should define the Volume type him/herself. Though, when looking at a <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-an-elasticsearch-fluentd-and-kibana-efk-logging-stack-on-kubernetes" rel="noreferrer">DigitalOcean guide</a>, it does not seem there was any Volume setup there.
I picked that tutorial, but there are dozens on Medium that are all doing the same thing.</p>
<p>So: which one is the best setup for an ElasticSearch cluster? Of course keeping in mind order to not loose any data within an index, and being able to add pods(Kubernetes) or nodes (ElasticSearch) that can access the index.</p>

purple_lolakos
<p>A good pattern to deploy an ElasticSearch cluster in kubernetes is to define a <code>StatefulSets</code>.</p>
<blockquote>
<p>Because the StatefulSet replicates more than one Pod you cannot simply reference a persistent volume claim. Instead, you need to add a <code>persistent volume claim template</code> to the StatefulSet state definition.</p>
</blockquote>
<p>In order for these replicated peristent volumes to work, you need to create a <a href="https://kubernetes.io/docs/concepts/storage/dynamic-provisioning/" rel="nofollow noreferrer">Dynamic Volume Provisioning</a> and <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/" rel="nofollow noreferrer">StorageClass</a> which  allows storage volumes to be created on-demand.</p>
<p>In the  <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-an-elasticsearch-fluentd-and-kibana-efk-logging-stack-on-kubernetes" rel="nofollow noreferrer">DigitalOcean guide</a> tutorial, the persistent volume claim template is as follow:</p>
<pre><code>  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        app: elasticsearch
    spec:
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      storageClassName: do-block-storage
      resources:
        requests:
          storage: 100Gi
</code></pre>
<p>Here, the <code>StorageClass</code> is <code>do-block-storage</code>. You can replace it with your own storage class</p>
