Attaching container's directory to host volume in kubernetes

5/15/2018

I have created a persistent volume on my host system at location /dynamic_data. I have a test container which has some data present at location /shared. I want to bind the /shared with the persistent volume at /dynamic_data. Now, /shared already has many files inside it and I want them to be reflected at /dynamic_data. The problem is that /dynamic_data contains nothing and the same is reflected inside the container, i.e., I loose my data inside the container because nothing was present in the host directory. Is there any way this problem can be solved?

-- Pranjal
kubernetes

1 Answer

5/15/2018

Currently persistent volume doesn't do overlaying. If you "mount" the pv to your container, you will only see what the host initially has, which in your case its empty.

My 2 cents... If you need to persist the /shared from your container, - first mount the pv to a different path in your container say /dyanamic-data.

  • Then start your container/pod

  • then exec into the pod

  • from there you can copy /shared to /dyanmic-data.

  • So now you have your files present on both container /shared and the pv.

  • Shutdown your app, mount your pv to /shared then you will be able to persist your files outside the container.

All these will require downtime unfortunately.

-- Bal Chua
Source: StackOverflow