unable to understand mounting postgres data path onto minikube kubernetes deployment with permission errors

5/12/2020

I’m getting started with kubernetes, and I want to create a simple app with a single webserver & postgres database. The problem I’m running into is the deployment of the postgres is giving me permission errors. The following are discussions around this:

EDIT

spec:

  • OSX - 10.15.4
  • minikube - v1.9.2
  • kubernetes - v1.18.2

minikube setup

minikube start --driver=virtualbox --cpus=2 --memory=5120 --kubernetes-version=v1.18.2 --container-runtime=docker --mount=true --mount-string=/Users/holmes/kubernetes/pgdata:/data/pgdata

The permission error: chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted

I am trying to mount a local OS directory into minikube to be used with the postgres deployment/pod/container volume mount.

After I run the above setup I ssh into minikube (minikube ssh) and check the permissions

# minikube: /
drwxr-xr-x   3 root   root   4096 May 13 19:31 data
# minikube: /data
drwx------  1 docker docker   96 May 13 19:27 pgdata

By running the script below the chmod permission error surfaces. If I change the --mount-string=/Users/holmes/kubernetes/pgdata:/data (leave out /pgdata) and then minikube ssh to create the pgdata directory:

mkdir -p /data/pgdata
chmod 777 /data/pgdata

I get a different set of permissions before deployment

# minikube: /
drwx------   1 docker docker   96 May 13 20:10 data
# minikube: /data
drwxrwxrwx 1 docker docker   64 May 13 20:25 pgdata

and after deployment

# minikube: /
drwx------   1 docker docker  128 May 13 20:25 data
# minikube: /data
drwx------ 1 docker docker   64 May 13 20:25 pgdata

Not sure why this changes, and the chmod permission error persists. It seems like the above reference links are bouncing around different methods on different machines on different vms which I don’t understand nor can I get this to work. Can someone walk me getting this to work? Super confused going through all the above discussions.

postgres.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: data-block

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  namespace: data-block
  labels:
    type: starter
data:
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: docker

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  namespace: data-block
  labels:
    app: postgres
spec:
  capacity:
    storage: 2Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /data/pgdata

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim
  namespace: data-block
  labels:
    app: postgres
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: ""

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: data-block
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:12.2
        ports:
        - containerPort: 5432
        envFrom:
        - configMapRef:
            name: postgres-config
        volumeMounts:
        - name: postgres-vol
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: postgres-vol
        persistentVolumeClaim:
          claimName: postgres-pv-claim
-- M.Holmes
containers
kubernetes
minikube
mounted-volumes
postgresql

1 Answer

5/12/2020

You are declaring the PGDATA field that maybe the cause of the issue. I faced the same error, this comes because there's as LOST+FOUND folder already in that directory however, the container wants it to be a empty dir. Giving the subPath field solves this issue. Please try this it should solve the issue and you need not need any PGDATA field. Try omitting it from your configmap and add subPath to some folder. Please go through following manifests.

https://github.com/mendix/kubernetes-howto/blob/master/postgres-deployment.yaml

https://www.bmc.com/blogs/kubernetes-postgresql/

it's a statefulset that usually you should go with and not a deployment when it comes to Database deployment.

        - name: postgredb
          mountPath: /var/lib/postgresql/data
         #setting subPath will fix your issue it can be pgdata or 
          postgres or any other folder name according to your 
          choice.
          subPath: postgres 
-- redzack
Source: StackOverflow