<p>I'm using the below manifest to deploy postgresql on kubernetes within NFS persistent volume:</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs2
spec:
  capacity:
    storage: 6Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 10.139.82.123
    path:  /nfsfileshare/postgres
---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs2
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 6Gi
---
apiVersion: v1
kind: Service
metadata:
  name: db

  labels:
    app: aiflow-db
spec:
  selector:
    app: aiflow-db
  clusterIP: None
  ports:
  - port: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
  namespace: test-aiflow
  labels:
    app: aiflow-db
spec:
  selector:
    matchLabels:
      app: aiflow-db
  template:
    metadata:
      labels:
        app: aiflow-db
    spec:
      containers:
      - name: db
        image: postgresql:10
        ports:
        - containerPort: 5432
        env:
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        volumeMounts:
        - mountPath: /var/lib/postgresql/data/pgdata
          name: nfs2
      volumes:
        - name: nfs2
          persistentVolumeClaim:
            claimName: nfs2
      restartPolicy: Always
</code></pre>

<p>The pg data can be mounted to nfs server (<code>/nfsfileshare/postgres   *(rw,async,no_subtree_check,no_root_squash)</code>):</p>

<pre><code>total 124
drwx------ 19  999 root    4096 Aug  7 11:10 ./
drwxrwxrwx  5 root root    4096 Aug  7 10:28 ../
drwx------  3  999 docker  4096 Aug  7 11:02 base/
drwx------  2  999 docker  4096 Aug  7 11:10 global/
drwx------  2  999 docker  4096 Aug  7 11:01 pg_commit_ts/
drwx------  2  999 docker  4096 Aug  7 11:01 pg_dynshmem/
-rw-------  1  999 docker  4513 Aug  7 11:02 pg_hba.conf
-rw-------  1  999 docker  1636 Aug  7 11:02 pg_ident.conf
drwx------  4  999 docker  4096 Aug  7 11:09 pg_logical/
drwx------  4  999 docker  4096 Aug  7 11:01 pg_multixact/
drwx------  2  999 docker  4096 Aug  7 11:10 pg_notify/
drwx------  2  999 docker  4096 Aug  7 11:02 pg_replslot/
drwx------  2  999 docker  4096 Aug  7 11:01 pg_serial/
drwx------  2  999 docker  4096 Aug  7 11:01 pg_snapshots/
drwx------  2  999 docker  4096 Aug  7 11:02 pg_stat/
drwx------  2  999 docker  4096 Aug  7 11:02 pg_stat_tmp/
drwx------  2  999 docker  4096 Aug  7 11:02 pg_subtrans/
drwx------  2  999 docker  4096 Aug  7 11:02 pg_tblspc/
drwx------  2  999 docker  4096 Aug  7 11:01 pg_twophase/
-rw-------  1  999 docker     3 Aug  7 11:02 PG_VERSION
drwx------  3  999 docker  4096 Aug  7 11:02 pg_wal/
drwx------  2  999 docker  4096 Aug  7 11:02 pg_xact/
-rw-------  1  999 docker    88 Aug  7 11:02 postgresql.auto.conf
-rw-------  1  999 docker 22729 Aug  7 11:02 postgresql.conf
-rw-------  1  999 docker    74 Aug  7 11:10 postmaster.pid
</code></pre>

<p>However the container is stuck with below log:</p>

<blockquote>
  <p>The files belonging to this database system will be owned by user
  "postgres". This user must also own the server process.</p>
  
  <p>The database cluster will be initialized with locale "en_US.utf8". The
  default database encoding has accordingly been set to "UTF8". The
  default text search configuration will be set to "english".</p>
  
  <p>Data page checksums are disabled.</p>
  
  <p>fixing permissions on existing directory
  /var/lib/postgresql/data/pgdata ... ok creating subdirectories ... ok
  selecting default max_connections ... 100 selecting default
  shared_buffers ... 128MB selecting dynamic shared memory
  implementation ... posix creating configuration files ... ok running
  bootstrap script ... ok</p>
</blockquote>

<p>Seems it stuck on post-bootstrap initialization.</p>

<p>It works only if I do not use nfs volume (works by using hostPath volume), why is that?</p>

<p>NFS does not support fsync kernel vfs call which is required transaction logs for ensuring the writing out the redo logs on the disk. So you should use block storage when you need to use RDBMS, such as PostgreSQL and MySQL. You might lose the data consistency althogh you can run the one on the NFS. </p>
