kuberneteskubernetes-pvc

k8s volume mount overwrite


The work directory of my docker image is set to "/opt",and it will contain the jar file and config files,etc. I am trying to mount it pvc in k8s, but find the pvc will overwrite the /opt directory.Is there any way to mount the directory in docker image which is not empty , Thanks.


Solution

  • When a PVC is mounted to a directory, instead of adding a new file it will overwrite the directory you specify. For your case when the PVC is mounted the directory “/opt” will be replaced. To prevent this you need to create a new directory within the /opt directory or you can mount PVC to a different directory other than the current directory.

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi
      storageClassName: standard
      volumeMode: Filesystem
      volumeName: my-pvc
      mountPath: "/opt/my-pvc"
    

    This YAML will create a new directory named 'my-pvc' within the '/opt' directory and mount the PVC to it. Any files that are already in the '/opt' directory will not be overwritten.

    Or else:

    mountOptions:
        - path: /mnt
    

    This will mount the PVC to the /mnt directory instead of /opt, so the data in /opt will not be overwritten.

    This blog by Iman Tumorang will help you for reference.