A docker image running on my laptop with docker volume folder myVolume
inside it bounded to a folder on my desktop ( can be any directory on host machine) Desktop-Volume
:
docker run -it -v ~/Desktop/Desktop-Volume:/myVolume ..
There are files and folders inside Desktop-Volume
, when the docker app runs this folder got populated with newly created files which of course after shutting down the docker they still remain in Desktop-Volume
,
Now, i create a Kubernetes cluster on Google Cloud and make PersistentVolumes and run the deployment
, but the container crashes because it depends on those pre-processed Desktop-Volume
in order to initialize , so i need to place those files into PersistentVolumes before running my container on Kubernetes cluster ,but i don't know how .
The pod was something like this
spec:
volumes:
- name: demo
persistentVolumeClaim:
claimName: disk
containers:
- name: myContainer
image: "gcr.io/my-instance/myDocker:latest"
volumeMounts:
- mountPath: "/myVolume"
name: disk
I also tried :
i connect
to cluster shell and from there upload the Desktop-Volume.zip
from my laptop to the shell then unzip and set
hostPath:
path: "/home/<name>/Desktop-Volume"
but the container crashed no such a file or directory
referring to files which reside in myVolume
through Desktop-Volume
- how can i place the content of my pre-processed
Desktop-Volume
files into PersistentVolumes before running container on cluster ? - where is the mount path of the PersistentVolumes so that i can
ls
to there and see my files ?
Best Answer
This sounds like your container needs a pre-populated volume to start up. You could use hostPath like you mentioned but this is not recommended for a couple reasons. Instead, you'll transfer your data to a GCE PD, make sure your PV uses that pre-existing disk and then mount that to your pod. Here's some details:
1. Copy data to a GCE PD: Spin up a VM then either transfer your data from your local machine to the VM or create the filesystem and prep the files required directly from the VM instead of your local machine.
2. Prep a GCE disk for PV use: Either detach the GCE disk from your VM or take a snapshot of the disk and create a new GCE PD from the snapshot.
3. Create a PV to consume the disk: It is important to follow the documentation for this step carefully. The default behavior of PVC in GKE is to be provisioned dynamically using a StorageClass. Instead, we want to make sure the PV is created first and targets the GCE-PD explicitely. GCP has a good walk through on how to do this.
4. Create the PVC to target the PV: As I mentioned, the default behavior will be to create a brand new disk dynamically which will be a blank new disk. Make sure to follow the walk through carefully. If any of the fields are set incorrectly, a different PV will be created.
5. Mount the PVC in your pod: First of all note that GCE disks don't support read/write many, so this PVC can only be used by a single pod.
If you need to have more than a single pod, you'll need create multiple PVCs using the above steps. If the files and filesystem the container needs only has to be read only, then you have two other options to make it easier:
Create the GCE disk in readOnlyMnay mode, this way the same PVC can be used across multiple pods.
Use a configMap with all the files you need. This takes a little bit more planning before hand to make sure all the appropriate files are in the right place