haris2104
haris2104

Reputation: 23

Local Persistent Volume in its own directory

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.

Upvotes: 2

Views: 2387

Answers (3)

Mark
Mark

Reputation: 4067

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

Upvotes: 2

char
char

Reputation: 2147

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

Upvotes: 3

Harsh Manvar
Harsh Manvar

Reputation: 30208

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

Upvotes: 2

Related Questions