Home > Software design >  What's the simplest way to persistently mount a file to an application hosted in kubernetes?
What's the simplest way to persistently mount a file to an application hosted in kubernetes?

Time:04-06

I have a docker image which I want to run in kubernetes. A cluster already exists and I can kubectl it.

The docker image will be static, but it needs a data file. In my docker-compose running locally it would be given as

services:
  mysvc:
    image: my-image
    volumes:
      - /home/me/data.dat:/data.dat

I want to upload /home/me/data.dat to the kubernetes cluster/pod, where it will be mounted to /data.dat in the container.

Later, while the application is running, I want to re-upload this file, so it will be replaced in the running container.

If the container restarts for any reason, I want the most-recently uploaded version of data.dat to be mounted.

What's the simplest way to do this?

CodePudding user response:

The following is a thought experiment, I didn't try it in the real world, but it is how I would have attempted the problem:

Create a persistent volume persistent volume claim with access mode set to ReadOnlyMany/ReadWriteMany, mounting it in your pod and then use kubectl cp to upload the file to the mounted path. I assume this would upload the file to the persistent volume, making it accessible to every pod that mounted the volume. If the container restarts, it should re-mount the persistent volume with the latest uploaded file.

CodePudding user response:

There is no single answer to this question:

If file size is under 1MB you can store it as a ConfigMap

A ConfigMap is not designed to hold large chunks of data. The data stored in a ConfigMap cannot exceed 1 MiB. If you need to store settings that are larger than this limit, you may want to consider mounting a volume or use a separate database or file service.

If file size is above 1MB:

  • How many nodes do you have?

    • Single node: You can put it inside the host and mount with hostPath/local

    • More than one node:

      • Do you have a node affinity for your pod (in other words, is your pod going to run always on the same node?) : You can use hostPath/local ^see above

      • No affinity (pod can be assigned to any worker node): Use a PersistentVolume such as NFS and mount it to your workload.

  • Related