2

I am migrating my previous deployment made with docker-compose to Kubernetes. In my previous deployment, some containers do have some data made at build time in some paths and these paths are mounted in persistent volumes. Therefore, as the Docker volume documentation states,the persistent volume (not a bind mount) will be pre-populated with the container directory content.

I'd like to achieve this behavior with Kubernetes and its persistent volumes, How can I do ? Do I need to add some kind of logic using scripts in order to copy my container's files to the mounted path when data is not present the first time the container starts ?

Possibly related question: Kubernetes mount volume on existing directory with files inside the container

Michel L
  • 456
  • 7
  • 19
  • Have you considered using [Init Containers](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/)? – Wytrzymały Wiktor May 14 '20 at 10:51
  • I'd recommend not depending on this functionality, even in plain Docker. It doesn't work with bind-mounted host directories, and Docker won't update the contents of a volume if the underlying image has changed. Both of these things mean you need some way to manage the volume content, and whatever you do for that will work in Kubernetes too. – David Maze May 14 '20 at 10:55
  • @OhHiMark I have. That would mean to have an init Container that is able to copy the files from the container it is preceding (unless the init container is the same image but started differently). – Michel L May 14 '20 at 13:06
  • @DavidMaze, we did not make use of bind mount, just volumes. I guess you're right, I have to design a specific solution for my application. Do any of you know if there are some good practices or design patterns for that ? – Michel L May 14 '20 at 13:06

1 Answers1

6

I think your options are

  • ConfigMap (are "some data" configuration files?)
  • Init containers (as mentioned)
  • CSI Volume Cloning (clone combining an init or your first app container)
  • there used to be a gitRepo; deprecated in favour of init containers where you can clone your config and data from
  • HostPath volume mount is an option too
  • An NFS volume is probably a very reasonable option and similar from an approach point of view to your Docker Volumes
    • Storage type: NFS, iscsi, awsElasticBlockStore, gcePersistentDisk and others can be pre-populated. There are constraints. NFS probably the most flexible for sharing bits & bytes.

FYI The subPath might be of interest too depending on your use case and PodPreset might help in streamlining the op across the fleet of your pods

HTH

LMR
  • 91
  • 3