Facing issue in mounting portgresql persistance volume in kubernetes locally

10/23/2019

I am trying to mount postgrsql persitance volume in kubernetes locally with static provisioning. Here is my yaml file i created pv , pvc and pod

 apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: task-pv-volume
      namespace: manhattan
      labels:
        type: local
    spec:
      storageClassName: manual
      capacity:
        storage: 100Mi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data"
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: task-pv-claim
      namespace: manhattan
    spec:
      storageClassName: manual
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 80Mi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: dbr-postgres
 namespace: manhattan
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: postgresql:2.0.1
      tty: true
      volumeMounts:
        -  mountPath: "/var/lib/pgsql/9.3/data"
           name: task-pv-storage
           subPath: data
           readOnly: false
  nodeSelector:
     kubernetes.io/hostname: k8s-master

My volume lies in the /var/lib/pgsql/9.3/data but my pod fails and i don't want to change the location of mountPath to /var/lib/pgsql/9.3/data/backup

can you please suggest any overwrite option in yaml file

I don't want to create the folder with new name here .

enter image description here

If i changed the mountPath to /var/lib/pgsql/9.3/data/backup the pod starts running but i dont want that i want the data to writtend in the same directory /var/lib/pgsql/9.3/data

-- UDIT JOSHI
kubernetes
persistent-volumes
postgresql-9.3

2 Answers

10/31/2019

I figure out the issue its arises because my data directory is not empty initially.I need to run postgresql init db during ENTRYPOINT OR CMD at run time

It is must in kubernetes volume to have empty mountPath but it works fine if you are using docker volume for mounting

-- UDIT JOSHI
Source: StackOverflow

10/24/2019

As per official documentation for postgres:9.4 from dockerhub:

1. Please Note:

Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.

2. PGDATA environment variable:

This optional variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data, but if the data volume you're using is a filesystem mountpoint (like with GCE persistent disks), Postgres initdb recommends a subdirectory (for example /var/lib/postgresql/data/pgdata ) be created to contain the data.

As workaround please refer to:

You can also change the image to the specific one using helm chart:

From chart version 4.0.0, it is possible to use this chart with the Docker Official PostgreSQL image. Besides specifying the new Docker repository and tag, it is important to modify the PostgreSQL data directory and volume mount point. Basically, the PostgreSQL data dir cannot be the mount point directly, it has to be a subdirectory.

helm install --name postgres \
             --set image.repository=postgres \
             --set image.tag=10.6 \
             --set postgresqlDataDir=/data/pgdata \
             --set persistence.mountPath=/data/ \
             stable/postgresql

You can template this helm chart and customizing. From another point of view the main problem is that you have initialized your database - as you mentioned under /var/lib/pgsql/9.3/data/backup so please verify your docker file and your docker-entrypoint.sh to figure out where is the problem.

Please let me know if this helped.

-- Hanx
Source: StackOverflow