Reputation: 1550
I'm trying to wrap my head around Kubernetes and as an exercise, I want to .igrate my docker-compose setup, which consists of nginx, php-fpm and mysql to a k8s cluster.. While I'm familiar with Docker and docker-compose, I just don't seem to figure out, how to mount a local folder into a Kunernetes pod.
What I want to achieve is something as simple as -v /home/user/www:/var/www/html
in Docker into each pod.
But no matter how I configure PersistantVolume, the folders mounted in the pods are empty. I tried Docker Desktop with WSL2, microk8s (snap installed with systemd hack) under WSL2 and minikube under WSL2.
Is this storageclass related? Is this even possible in WSL2?
Upvotes: 0
Views: 584
Reputation: 7628
The previous answer about config maps or custom image is correct. However, I prefer to use an init container to create the mount in the pod. The init container can download content from anywhere and store it in th pod before the application container starts and mounts this volume. This way you don’t need a custom image, image registry or a fileshare.
See for instance the example used here: kubernetes: mounting volume from within init container
Upvotes: 0
Reputation: 159761
The Docker bind-mount model can't really be used in Kubernetes the way you describe.
Kubernetes's model is around a cluster of essentially interchangeable machines. Say you have 10 systems. Which one has that HTML content? How does it get there? What happens if you need to add another node, or that one node is destroyed? That you happen to be using a test setup with only a single node doesn't change that design point.
If you're using Kubernetes, you're basically required to build this content into an image, and push it into a registry of some sort. There are a couple of alternatives (a single index.html
file could get stuffed into a ConfigMap; you could use external storage like AWS S3) but mostly you'd have to write a Dockerfile, COPY
this into an image, and reference that image:
in your deployment spec.
For the particular stack you mention, I've also seen a couple of setups that use a named volume to copy data from the PHP container to the nginx server. This doesn't work well in Kubernetes: the easier volume types to set up can't be shared between containers, and Kubernetes won't automatically copy content into a volume the way Docker will. I'd suggest avoiding any sort of filesystem sharing in Kubernetes; it's good practice to also avoid it in plain Docker (the COPY
-based image setup will work fine in a single-host Compose setup).
Upvotes: 2