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.