<p>I'm approaching k8s volumes and best practices and I've noticed that when reading documentation it seems that you always need to use <strong>StatefulSet</strong> resource if you want to implement persistency in your cluster:</p>
<blockquote>
<p>&quot;StatefulSet is the workload API object used to manage stateful
applications.&quot;</p>
</blockquote>
<p>I've implemented some tutorials, <strong>some of them use StatefulSet, some others don't</strong>.</p>
<p>In fact, let's say I want to persist some data, I can have my stateless Pods (even MySql server pods!) in which I use a PersistentVolumeClaim which persists the state. If I stop and rerun the cluster, I can resume the state from the Volume with no need of StatefulSet.</p>
<p>I attach here an example of Github repo in which there is a stateful app with MySql and no StatefulSet at all:
<a href="https://github.com/shri-kanth/kuberenetes-demo-manifests" rel="nofollow noreferrer">https://github.com/shri-kanth/kuberenetes-demo-manifests</a></p>
<p>So <strong>do I really need to use a StatefulSet</strong> resource for databases in k8s? Or are there some specific cases it could be a necessary practice?</p>

Alessandro Argentieri
<p><a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> is not the same as PV+PVC.</p>
<p>A StatefulSet manages Pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.</p>
<p>In other words it manages the deployment and scaling of a set of Pods , and provides guarantees about the ordering and uniqueness of these Pods.</p>
<blockquote>
<p>So do I really need to use a StatefulSet resource for databases in k8s?</p>
</blockquote>
<p>It depends on what you would like to achieve.</p>
<p>StatefulSet gives you:</p>
<ul>
<li>Possibility to have a Stable Network ID (so your pods will be <em>always</em> named as <code>$(statefulset name)-$(ordinal)</code> )</li>
<li>Possibility to have a Stable Storage, so when a Pod is (re)scheduled onto a node, its volumeMounts mount the PersistentVolumes associated with its PersistentVolume Claims.</li>
</ul>
<blockquote>
<p>...MySql and no StatefulSet...</p>
</blockquote>
<p>As you can see, if your goal is just to run single RDBMS Pod (for example Mysql) that stores all its data (DB itself) on PV+PVC, then the StatefulSet is definitely an overkill.</p>
<p>However, if you need to run Redis cluster (distributed DB) :-D it'll be close to impossible  to do that without a StatefulSet (to the best of my knowledge and based on numerous threads about the same on StackOverflow).</p>
<p>I hope that this info helps you.</p>
