Local Persistent Volume in its own directory

9/18/2019

I have got the local persistent volumes to work, using local directories as mount points, storage class, PVC etc, all using standard documentation.

However, when I use this PVC in a Pod, all the files are getting created in the base of the mount point, i.e if /data is my mount point, all my application files are stored in the /data folder. I see this creating conflicts in the future, with more than one application writing to the same folder.

Looking for any suggestions or advice to make each PVC or even application files of a Pod into separate directories in the PV.

-- haris2104
kubernetes
persistent-volume-claims

3 Answers

9/18/2019

If you store your data in different directories on your volume, you can use subPath to separate your data into different directories using multiple mount points.

E.g.

apiVersion: v1
kind: Pod
metadata:
  name: podname
spec:
    containers:
    - name: containername
      image: imagename
      volumeMounts:
      - mountPath: /path/to/mount/point
        name: volumename
        subPath: volume_subpath
      - mountPath: /path/to/mount/point2
        name: volumename
        subPath: volume_subpath2
    volumes:
    - name: volumename
      persistentVolumeClaim:
        claimName: pvcname
-- char
Source: StackOverflow

9/18/2019

You can simply change the mount path and sperate the each application mount path so that files of POD into separate directories.

-- Harsh Manvar
Source: StackOverflow

9/18/2019

Another approach is using subPathExpr. Note:

The subPath and subPathExpr properties are mutually exclusive

apiVersion: v1
kind: Pod
metadata:
  name: pod3
spec:
  containers:
  - name: pod3
    env:
    - name: POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    image: busybox
    command: [ "sh", "-c", "while [ true ]; do echo 'Hello'; sleep 10; done | tee -a /logs/hello.txt" ]
    volumeMounts:
    - name: workdir1
      mountPath: /logs
      subPathExpr: $(POD_NAME)
  restartPolicy: Never
  volumes:
  - name: workdir1
    persistentVolumeClaim:
      claimName: pvc1

As described here.

In addition please follow Fixing the Subpath Volume Vulnerability in Kubernetes here and here

-- Hanx
Source: StackOverflow