Search code examples
kubernetesvolumes

Kubernetes share data between containers


I am exercising on K8S, and I need to share some data between my containers inside a POD, the problem I have is that I need to make containers have available some data from other containers that is already present on that containers at startup. Let me show an example of what I mean:

Container A at startup: /root/dir/a.txt Container B at startup /root/dirB/b.txt

In container C I want to have a directory that contains both a.txt file and b.txt file without doing any operation such as writing etc. But just using volumes. How can I do it? Thank you in advance


Solution

  • Make a emptyDir volume, mount it at /newroot on both A and B with both of those set as initContainers which will run command: [bash, -c, "cp /root/dir/a.txt /newroot/dir/a.txt] and similar for B. On C mount that emptyDir using subPath on either /root/dir or the whole path as needed.