How to deploy Postgresql on Kubernetes with NFS volume

8/7/2018

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

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

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

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

However the container is stuck with below log:

The files belonging to this database system will be owned by user "postgres". This user must also own the server process.

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".

Data page checksums are disabled.

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

Seems it stuck on post-bootstrap initialization.

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

-- Feiyu Zhou
kubernetes
nfs
postgresql

2 Answers

8/7/2018

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.

-- Daein Park
Source: StackOverflow

3/15/2019

I meet the same question, When I use helm deploy the gitlab, the postgresql can't run, and errors below:

FATAL:  data directory "/var/lib/postgresql/data/pgdata" has wrong ownership.  
HINT:  The server must be started by the user that owns the data directory.

I think it's because the postgresql run property need it's data should be own by user postgres and group postgres, but the nfs change the own user and group, makes the postgresql can't run.

Maybe change another tools like glusterfs can solve this problem, or try mysql's data mount by nfs.

-- hekai
Source: StackOverflow