1

My goal is to run postgres on minikube, where /var/lib/postgresql/data is mounted from my laptop. I followed many posts on how to get there but unsuccesfull just yet, the closest I've gotten is here:

First I mount my local /data/ to minikube and confirm that - to my understanding the /data has root permissions, so I needed to do sudo mkdir -p /data/postgres-pv and sudo cp -R <source_path>/data/* /data/postgres-pv to copy data over there.

My PV is:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  namespace: demo
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/postgres-pv

For debug, I was able to run a busybox pod and make sure I'm seeing the right data, I tired adding files on my laptop at /data/postgres-pv and instantly seeing them in busybox mounted path using the following claim:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc
  namespace: demo
  labels:
    type: local
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  volumeName: postgres-pv

The busybox I used for debug:

kind: Pod
apiVersion: v1
metadata:
  name: busybox
  namespace: demo
spec:
  containers:
    - name: busybox
      image: busybox
      command:
        - sleep
        - "3600"
      volumeMounts:
      - mountPath: "/data"
        name: postgres-pvc
  volumes:
    - name: postgres-pvc
      persistentVolumeClaim:
        claimName: postgres-pvc

When I try to load the same exact folder from my local laptop into a pod running postgres and override /var/lib/postgresql/data I get an error, I tried different variations of the following, including subPath and PGDATA variable as again, seen in many posts (example here)

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: admindb
  namespace: demo # should be replaced
spec:
  template:
    metadata:
      labels:
        app: admindb
    spec:
      containers:
      - name: postgres
        image: postgres:9.6.5
        ports:
        - containerPort: 5432
        env:
#        - name: POSTGRES_DB
#          valueFrom:
#            secretKeyRef:
#              name: admindb-secret-config
#              key: dbname
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: user
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: password
#        - name: PGDATA # overriding the default mount so we can load our own data from PVC
#          value: /var/lib/postgresql/data/pgdata/
        volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: postgres-pvc
#            subPath: pgdata
      volumes:
        - name: postgres-pvc
          persistentVolumeClaim:
            claimName: postgres-pvc

The error I get when I check the log is:

+ kubectl logs -n demo admindb-546d55d9b5-ddr4f
chown: cannot read directory ‘/var/lib/postgresql/data/pg_multixact’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata/pgdata’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_wal’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_snapshots’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_commit_ts’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_stat’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/PG_VERSION’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_stat_tmp’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pg_hba.conf’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.pid’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_logical’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_notify’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_subtrans’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_serial’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_replslot’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.conf’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgres/pgdata’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgres’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_tblspc’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.auto.conf’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_twophase’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_xact’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_dynshmem’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.opts’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pg_ident.conf’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/global’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/base’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data’: Input/output error

if I modify the deployment to include

- name: PGDATA
  value: /var/lib/postgresql/data/pgdata/

The error then becomes:

+ kubectl logs -n demo admindb-6dc94659dd-4kc9t
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata/pgdata’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata/’: Input/output error

It sure feels that I'm going in circles between all the different posts while missing something fundamental in the process, any help or point in the right direction is greatly appreciated - feel like I need to change a line or two in my deployment and it will work! or change a permission on my laptop.

Naim Salameh
  • 387
  • 4
  • 18

1 Answers1

1

Change your volumeMounts to:

        volumeMounts:
          - mountPath: /var/lib/postgresql/something
            name: postgres-pvc

You can read the documentation of PostgreSQL 9.6 section 18.2. Creating a Database Cluster:

18.2.1. Use of Secondary File Systems

Many installations create their database clusters on file systems (volumes) other than the machine's "root" volume. If you choose to do this, it is not advisable to try to use the secondary volume's topmost directory (mount point) as the data directory. Best practice is to create a directory within the mount-point directory that is owned by the PostgreSQL user, and then create the data directory within that. This avoids permissions problems, particularly for operations such as pg_upgrade, and it also ensures clean failures if the secondary volume is taken offline.

Crou
  • 10,232
  • 2
  • 26
  • 31
  • https://stackoverflow.com/users/3156333/crou that's great !! can you comment on the best practice to make that /something the main data? i.e. after I connect to the database I still don't see the the schema that I prepopulated, my first guess would be to modify the postgres.conf to point the /something as the data directory unless you have any other pointers – Naim Salameh Oct 08 '19 at 16:39
  • I think you should use [initContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) to load schema from PV, or create a [ConfigMap](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/) which will store your postgres.conf file. – Crou Oct 09 '19 at 08:11