1

I have a pod which contains two containers. One container is a web application and another store some static data for this web application.

The data here is a set of files which are stored in the folder of this container with name /data and that's only function of the container to store this data and expose them to the web application.

I'm looking for the way to share the content of this folder with web application container in this pod.

If I'm using the YAML spec below the folder in both containers is empty. Is there a way to share the data from container folder without cleaning it up?

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
    version: 1.2.3
spec:
  volumes:
    - name: my-app-data-volume

  containers:
    - name: my-app-server
      image: my-app-server-container-name
      volumeMounts:
        - name: my-app-data-volume
          mountPath: /data
      ports:
        - containerPort: 8080

    - name: my-app-data
      image: my-app-data-container-name
      volumeMounts:
        - name: my-app-data-volume
          mountPath: /data
Alexey Usharovski
  • 1,256
  • 9
  • 23
  • Why not use something specific for static data like S3, GCS (anything else) ? Those are static files, why would you put them inside a pod, replicating them other and other with each replicas? – night-gold Oct 02 '19 at 15:13
  • This set of two containers is a kind of test server which should use different test datasets. To make this possible I move the data from the application container to another one to make it simpler to switch datasets. With `docker-compose` that was very easy but now I should use them also in Kubernates environment. – Alexey Usharovski Oct 02 '19 at 15:18
  • Please explain the reason for minus to this question. – Alexey Usharovski Oct 02 '19 at 15:19
  • If you use a set of data that sould not be backed up why not use an init container to write the data inside a volume for the pod and mounting this result inside your container? You will preserve resource usage, having only one container in the resulting pod. – night-gold Oct 02 '19 at 15:27
  • Another solution would be to use configmap if you can? – night-gold Oct 02 '19 at 15:27
  • Need to think about that. For now I need to run the existing solution in Kubernetes. – Alexey Usharovski Oct 02 '19 at 15:43

1 Answers1

3

You can use an EmptyDir volume for this. Specify the container that contains the files as an initContainer, then copy the files into the EmptyDir volume. Finally, mount that volume in the web app container.

Jamie
  • 5,126
  • 1
  • 18
  • 15
  • So only way is to create an EmptyDir and copy data to it? No way to expose existing folder with data in the container as Kubernetes volume? – Alexey Usharovski Oct 02 '19 at 15:20
  • None that I know of that are in production use. See related discussion: https://stackoverflow.com/questions/30538210/how-to-mimic-volumes-from-in-kubernetes, and this alpha project: https://github.com/kubernetes-csi/csi-driver-image-populator – Jamie Oct 02 '19 at 15:29
  • Of course that's not a production solution. That's a kind of mock server for the QA environment. – Alexey Usharovski Oct 02 '19 at 15:49
  • 1
    You cannot initialize a volume with contents from an image in Kubernetes the way you can in plain Docker. – David Maze Oct 02 '19 at 15:51
  • @DavidMaze thanks a lot. Just got that. Only workaround here is to use `command` clause to copy data to shared folder. Going to look for something better)) – Alexey Usharovski Oct 02 '19 at 15:52