<blockquote>
<p>Is the data shared between those two pods of postgres images or is it possible to set it to share data?</p>
</blockquote>
<p>Each pod has its own filesystem; there is no sharing between pods by default.</p>
<p>With the right storage driver in your Kubernetes clusters, you can create <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">persistent volumes</a> that are shared between pods. However, this is usually not appropriate for a database, which typically requires exclusive access to its storage directory.</p>
<p>To achieve your goal of having two database instances with the same content, you will need to set up <a href="https://www.postgresql.org/docs/current/runtime-config-replication.html" rel="nofollow noreferrer">postgres replication</a>. In this model, your database instances form a cluster and writes to the primary server are automatically replicated to secondary servers. You can only <em>write</em> to the master, but you can <em>read</em> (i.e., perform <code>select</code> queries) from any of the replicas.</p>
<p>There are a number of operators that will help set up postgres clusters in Kubernetes. For example:</p>
<ul>
<li><a href="https://github.com/CrunchyData/postgres-operator?tab=readme-ov-file" rel="nofollow noreferrer">Crunchy postgres</a></li>
<li><a href="https://github.com/zalando/postgres-operator" rel="nofollow noreferrer">Zalando</a></li>
<li><a href="https://github.com/cloudnative-pg/cloudnative-pg" rel="nofollow noreferrer">CloudNativePg</a></li>
</ul>

CC BY-SA 4.0
2024-01-06T13:26:26.703Z
2024-01-06T13:26:26.703Z
larsks
<p>I have a structure like shown in the image below: cluster with two nodes and service for the database (postgres).</p>
<p>Is the data shared between those two pods of <code>postgres</code> images or is it possible to set it to share data?</p>
<p>This is scenario which I want the answer to:</p>
<ul>
<li>Connect to database with host <code>db.domain.com</code> (load balancer point me to <code>node1</code>)</li>
<li>Insert 1 (and only) record in table</li>
<li>Disconnect</li>
<li>Connect to database again with host <code>db.domain.com</code> (load balancer points me to other node)</li>
<li>List items in table</li>
<li>Will I get the inserted row from <code>node1</code>?</li>
</ul>
<p>Also if I scale up to 10 nodes, 10 instances of same service will be deployed, so 10 images of <code>posgres</code>, do the all share data?</p>
<p><a href="https://i.sstatic.net/s76eq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/s76eq.png" alt="" /></a></p>

<kubernetes><postgresql>
Is the data shared between two pods database deployment k8s
Aleksa Ristic
# Is the data shared between two pods database deployment k8s

(Asked by: username_0 on 2024-01-06)

I have a structure like shown in the image below: cluster with two nodes and service for the database (postgres).

Is the data shared between those two pods of `postgres` images or is it possible to set it to share data?

This is scenario which I want the answer to:

* Connect to database with host `db.domain.com` (load balancer point me to `node1`)
* Insert 1 (and only) record in table
* Disconnect
* Connect to database again with host `db.domain.com` (load balancer points me to other node)
* List items in table
* Will I get the inserted row from `node1`?

Also if I scale up to 10 nodes, 10 instances of same service will be deployed, so 10 images of `posgres`, do the all share data?

[![](https://i.sstatic.net/s76eq.png)](https://i.sstatic.net/s76eq.png)

**Tags:** kubernetes, postgresql


---

## 1 answers

### Answer by username_1 on 2024-01-06. Score: 1

> Is the data shared between those two pods of postgres images or is it possible to set it to share data?

Each pod has its own filesystem; there is no sharing between pods by default.

With the right storage driver in your Kubernetes clusters, you can create [persistent volumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/) that are shared between pods. However, this is usually not appropriate for a database, which typically requires exclusive access to its storage directory.

To achieve your goal of having two database instances with the same content, you will need to set up [postgres replication](https://www.postgresql.org/docs/current/runtime-config-replication.html). In this model, your database instances form a cluster and writes to the primary server are automatically replicated to secondary servers. You can only *write* to the master, but you can *read* (i.e., perform `select` queries) from any of the replicas.

There are a number of operators that will help set up postgres clusters in Kubernetes. For example:

* [Crunchy postgres](https://github.com/CrunchyData/postgres-operator?tab=readme-ov-file)
* [Zalando](https://github.com/zalando/postgres-operator)
* [CloudNativePg](https://github.com/cloudnative-pg/cloudnative-pg)


---

