<p>Why I'd want to have multiple replicas of my DB?</p>
<ol>
<li>Redundancy: I have &gt; 1 replicas of my app code. Why? In case one node fails, another can fill its place when run behind a load balancer.</li>
<li>Load: A load balancer can distribute traffic to multiple instances of the app.</li>
<li>A/B testing. I can have one node serve one version of the app, and another serve a different one.</li>
<li>Maintenance. I can bring down one instance for maintenance, and keep the other one up with 0 down-time.</li>
</ol>
<p>So, I assume I'd want to do the same with the backing db if possible too.</p>
<p>I realize that many nosql dbs are better configured for multiple instances, but I am interested in relational dbs.</p>
<p>I've played with operators like <a href="https://github.com/CrunchyData/postgres-operator" rel="nofollow noreferrer">this</a> and <a href="https://github.com/mysql/mysql-operator" rel="nofollow noreferrer">this</a> but have found problems with the docs, have not been able to get them up and running and found the community a bit lacking. Relying on this kind of thing in production makes me nervous. The Mysql operator has a note even, saying it's not for production use.</p>
<p>I see that native k8s <a href="https://kubernetes.io/docs/tasks/run-application/scale-stateful-set/" rel="nofollow noreferrer">statefulsets have scaling</a> but these docs aren't specific to dbs at all. I assume the complication is that dbs need to write persistently to disk via a volume and that data has to be synced and routed somehow if you have more than one instance.</p>
<p>So, is this something that's non-trivial to do myself? Or, am I better off having a dev environment that uses a one-replica db image in the cluster in order to save on billing, and a prod environment that uses a fully managed db, something like <a href="https://cloud.google.com/sql/docs/postgres#docs" rel="nofollow noreferrer">this</a> that takes care of the scaling/HA for me? Then I'd use kustomize to manage the yaml variances.</p>
<p><strong>Edit</strong>:</p>
<p>I actually found a postgres operator that worked great. Followed the docs one time through and it all worked, and it's from <a href="https://www.kubegres.io/doc/getting-started.html" rel="nofollow noreferrer">postgres docs</a>.</p>

<p>I have created this community wiki answer to summarize the topic and to make pertinent information more visible.</p>
<p>As <a href="https://stackoverflow.com/users/4216641/turing85" title="14,085 reputation">Turing85</a> well mentioned in the comment:</p>
<blockquote>
<p>Do NOT share a pvc to multiple db instances. Even if you use the right backing volume (it must be an object-based storage in order to be read-write many), with enough scaling, performance will take a hit (after all, everything goes to one file system, this will stress the FS). The proper way would be to configure clustering. All major relational databases (mssql, mysql, postgres, oracle, ...) do support clustering. To be on the secure side, however, I would recommend to buy a scalable database &quot;as a service&quot; unless you know exactly what you are doing.</p>
</blockquote>
<blockquote>
<p>The good solution might be to use a single replica StatefulSet for development, to avoid billing and use a fully managed cloud based sql solution in prod. Unless you have the knowledge or a suffiiciently professional operator to deploy a clustered dbms.</p>
</blockquote>
<p>Another solution may be to use a different operator as <a href="https://stackoverflow.com/users/299180/aaron">Aaron</a> did:</p>
<blockquote>
<p>I actually found a postgres operator that worked great. Followed the docs one time through and it all worked, and it's from postgres: <a href="https://www.kubegres.io/doc/getting-started.html" rel="nofollow noreferrer">https://www.kubegres.io/doc/getting-started.html</a></p>
</blockquote>
<p>See also <a href="https://stackoverflow.com/questions/27157227/can-relational-database-scale-horizontally/27162851">this similar question</a>.</p>
