Kubernetes Pod - Sync pod directory with a local directory

11/11/2020

I have a python pod running. This python pod is using different shared libraries. To make it easier to debug the shared libraries I would like to have the libraries directory on my host too. The python dependencies are located in /usr/local/lib/python3.8/site-packages/ and I would like to access this directory on my host to modify some files.

Is that possible and if so, how? I have tried with emptyDir and PV but they always override what already exists in the directory.

Thank you!

-- Jérémy Octeau
kubernetes
kubernetes-pod
kubernetes-pvc

2 Answers

12/3/2020

You can take into consideration also below third party tools, that were created especially for Kubernetes app developers keeping in mind this functionality (keep in-sync source and remote files).

  1. Skaffold's Continuous Deployment workflow - it takes care of keeping source and remote files (Pod mounted directory) in sync.
  2. Telepresence`s Volume access feature.
-- Nepomucen
Source: StackOverflow

11/12/2020

This is by design. Kubelet is responsible for preparing the mounts for your container. At the time of mounting they are empty and kubelet has no reason to put any content in them.

That said, there are ways to achieve what you seem to expect by using init container. In your pod you define init container using your docker image, mount your volume in it in some path (ie. /target) but instead of running regular content of your container, run something like

cp -r /my/dir/* /target/ 

which will initiate your directory with expected content and exit allowing further startup of the pod Please take a look: overriding-directory.

Another option is to use subPath. Subpath references files or directories that are controlled by the user, not the system. Take a loot on this example how to mount single file into existing directory:

---
        volumeMounts:
        - name: "config"
          mountPath: "/<existing folder>/<file1>"
          subPath: "<file1>"
        - name: "config"
          mountPath: "/<existing folder>/<file2>"
          subPath: "<file2>"
      restartPolicy: Always
      volumes:
        - name: "config"
          configMap:
            name: "config"
---

Check full example here. See: mountpath, files-in-folder-overriding.

You can also as @DavidMaze said debug your setup in a non-container Python virtual environment if you can, or as a second choice debugging the image in Docker without Kubernetes.

-- Malgorzata
Source: StackOverflow