2
votes

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

2
Add the backup folder but change subPath from data to backup.Rodrigo Loza
Even better, don't use subpath. You don't need it.Rodrigo Loza
But psql data directory reads only inside data directory but it doesn't allow me to create mountPoint with the name dataUDIT JOSHI
Please provide information what image are you using? Is this your custom build or the official one?Mark
a database (especially a relational one) in k8s cluster already sounds wrong to me.suren

2 Answers

1
votes

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.

0
votes

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