Mount host dir for Postgres on Minikube - permissions issue

11/19/2018

I'm trying to setup PostgreSQL on Minikube with data path being my host folder mounted on Minikube (I'd like to keep my data on host).

With the kubernetes object created (below) I get permission error, the same one as here How to solve permission trouble when running Postgresql from minikube? although the question mentioned doesn't answer the issue. It advises to mount minikube's VM dir instead.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres          
        volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: storage
        env:
        - name: POSTGRES_PASSWORD
          value: user
        - name: POSTGRES_USER
          value: pass
        - name: POSTGRES_DB
          value: k8s
      volumes:
        - name: storage
          hostPath:
            path: /data/postgres

Is there any other way to do that other than building own image on top of Postgres and playing with the permissions somehow? I'm on macOS with Minikube 0.30.0 and I'm experiencing that with both Virtualbox and hyperkit drivers for Minikube.

-- Michal Ostruszka
docker
kubernetes
minikube
postgresql

1 Answer

11/19/2018

Look at these lines from here : hostPath

the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a hostPath volume

So, either you have to run as root or you have to change the file permission of /data/postgres directory.

However, you can run your Postgres container as root without rebuilding docker image.

You have to add following to your container:

securityContext:
  runAsUser: 0

Your yaml should look like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres          
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: storage
        env:
        - name: POSTGRES_PASSWORD
          value: user
        - name: POSTGRES_USER
          value: pass
        - name: POSTGRES_DB
          value: k8s
        securityContext:
          runAsUser: 0
      volumes:
      - name: storage
        hostPath:
          path: /data/postgres
-- Emruz Hossain
Source: StackOverflow