1

I have a question about Kubernetes containers and persistent volumes.

How can I make some of the preexisting folders of a Kubernetes container persistent?

I know the usage of PVCs in Kubernetes but the problem about mounting a PVC to a container is that this operation -naturally- deletes everything in the mount path. eg. Say that we have an image which has a non-empty directory /xyz and we want to make this directory persistent. If we create a PVC and mount it to /xyz, we would lose everything inside /xyz as well (we don't want this to happen). So we want that directory to be persistent from the start with the files inside of it.

I'm not so sure if Docker or any other container technology responds such a feature, so it may not be suitable for Kubernetes too. Would be glad if anyone can enlighten me about this. Thanks!

My approaches so far:

  • Copying: Creating a PVC for the directory contents and mounting it to an init container or job that copies /xyz to the /mounted/xyz, then mounting PVC to the main container's /xyz. This approach has some drawbacks if the directory is too fat or has some OS/runtime-specific configurations.
  • Hostpath: Populating a directory with the contents of /xyz (eg. /in/host/xyz) before starting the container. Then mounting this path from host to the container. Not a good approach since it's hard to automate.
tuna
  • 136
  • 7

1 Answers1

2

there is no way to mount a Volume in a certain folder without overwriting its contents.

In my opinion the best approaches could be:

  1. The first one reported by you (for large content):

    a. Create PVC

    b. Add an initContainer to your Deployment that mount the Volume in a DIFFERENT path from the directory containing the data to move/copy

    c. Add to the initContainer a "command" field with the commands to move/copy the content from the "source" directory to the mounted volume (target)

    d. Mount to the "main" container the PVC used in the initContainer at the "source" directory path

  2. Create a K8s cronjob (or job that works once if the files are never modified) that syncs from one folder to another (similar to point 1, but avoid waiting a long time before the application Pod starts, since the initContainer is no longer needed). Cronjob example (Pay attention to file owners; you may need to run the job under the same serviceAccount that produced those files)

  3. If they are static files, build the Docker image with all the contents of the folder already inside (Dockerfile —> copy). https://docs.docker.com/engine/reference/builder/

I strongly recommend not using hostPath in PRODUCTION environments. https://kubernetes.io/docs/concepts/storage/volumes/#hostpath

glv
  • 994
  • 1
  • 1
  • 15
  • Thanks for the response! I use exactly the same approach as you mentioned. I think another option is to use volume snapshots to avoid custom copying operation, but haven't tried it yet. – tuna Mar 17 '23 at 07:57