<p>If i deploy Postgres in a statefulSet <strong>without</strong> using replicas (just one pod) and i kill the node that the stateful set is running on will I be able to start up the node and reconnect to a persisted database</p>
<p>Here is an example configuration:
<a href="https://medium.com/@suyashmohan/setting-up-postgresql-database-on-kubernetes-24a2a192e962" rel="nofollow noreferrer">https://medium.com/@suyashmohan/setting-up-postgresql-database-on-kubernetes-24a2a192e962</a></p>
<p>I am working with someone who is convinced this should not work and that statefulSets only make sense as a way to maintain state between replicas. Im under the impression that the problem of mounting the PG data to ephemeral pods is specific to NOT using a statefulSet, and that even though there is only one pod in the example above that this will still make use of StatefulSet to solve the problem.</p>
<p>(as in this official mysql example: <a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/</a></p>

<p>The database will be persisted if the Pods make use of persisted volume storages. Ephemeral Volumes are <strong>not</strong> persisted and should not be used to save a db, as the name says, there is no long-term guarantee about durability.</p>
<p>But if your Pod saves the database on a persisted volume of some sort (such as local storage on the node itself or something more complex) then it will be persisted between runs.</p>
<p>Which means that if you have your Pod running on a node, let's say using local storage on that node, if you stop the node and then make it restart correctly, the Pod will be scheduled again and the persisted volume will be there with all the data saved previously.</p>
<hr />
<p>With this said, if you have only 1 Pod (StatefulSet with just 1 replica) and the node on which the Pod is currently running is somehow killed / stops working / stops answering then the Pod will <strong>not</strong> automatically restart on another node (not even if you are not using local storage)</p>
<p>You will be able to force it to run to another node, sure, but only with manual operations.</p>
<p>This is because (from the <a href="https://kubernetes.io/docs/tasks/run-application/force-delete-stateful-set-pod/" rel="nofollow noreferrer">docs</a>):</p>
<blockquote>
<p>In normal operation of a StatefulSet, there is never a need to force
delete a StatefulSet Pod. The StatefulSet controller is responsible
for creating, scaling and deleting members of the StatefulSet. It
tries to ensure that the specified number of Pods from ordinal 0
through N-1 are alive and ready. <strong>StatefulSet ensures that, at any</strong>
<strong>time, there is at most one Pod with a given identity running in a</strong>
<strong>cluster</strong>. This is referred to as at most one semantics provided by a
StatefulSet.</p>
</blockquote>
<p>If the controller <strong>cannot</strong> be sure if a Pod is running or not (and the example of a node getting killed or stopping to work correctly for a error is such a situation) then the Pod will <strong>never</strong> be restarted, until either:</p>
<ul>
<li>Manual operation such as a force delete.</li>
<li>The node starts answering again and becomes Ready once more.</li>
</ul>
<p>Note that draining a node will not create any problem as it will gracefully terminate StatefulSets Pods before starting them again (on other nodes).</p>
<hr />
<p>StatefulSets can work really well for databases, but usually it requires a more complex installation with multi-primary nodes and (at least) 3 replicas.</p>
<p>Also, databases requires very fast write operations on disk and as such perform better if they can work on high quality disks.</p>
<hr />
<p><strong>Update:</strong></p>
<p>StatefulSets are usually intended to be used when each of the replicas Pods require a unique identity (multi primary databases or apps that uses quorum are good example of this necessity)</p>
<p>When deployed with only 1 replica, the differences with a Deployment are small (but there are differences, for example a Deployment's Pod would eventually restart on another node if the node on which it was running stops working, a StatefulSet Pod will require manual intervention).. in general you should refer to the &quot;<a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#using-statefulsets" rel="nofollow noreferrer">Using StatefulSets</a>&quot; in the docs to decide if an app requires to be run as StatefulSet or Deployment.</p>
<p>Personally, I would run a database as a StatefulSet because it is a stateful app.. but I would also run it with 3 replicas, so that it can suffer the loss of one Pod without stopping to work.</p>
