localhost
localhost

Reputation: 603

How to mount a single file from the local Kubernetes cluster into the pods

I set up a local Kubernetes cluster using Kind, and then I run Apache-Airflow on it using Helm.

To actually create the pods and run Airflow, I use the command:

helm upgrade -f k8s/values.yaml airflow bitnami/airflow

which uses the chart airflow from the bitnami/airflow repo, and "feeds" it with the configuration of values.yaml. The file values.yaml looks something like:

web:
 extraVolumeMounts:
   - name: functions
     mountPath: /dir/functions/

 extraVolumes:
   - name: functions
     hostPath:
       path: /dir/functions/
       type: Directory

where web is one component of Airflow (and one of the pods on my setup), and the directory /dir/functions/ is successfully mapped from the cluster inside the pod. However, I fail to do the same for a single, specific file, instead of a whole directory.

Does anyone knows the syntax for that? Or have an idea for an alternative way to map the file into the pod (its whole directory is successfully mapped into the cluster)?

Upvotes: 1

Views: 3857

Answers (1)

AndD
AndD

Reputation: 2701

There is a File type for hostPath which should behave like you desire, as it states in the docs:

File: A file must exist at the given path

which you can then use with the precise file path in mountPath. Example:

web:
 extraVolumeMounts:
   - name: singlefile
     mountPath: /path/to/mount/the/file.txt

 extraVolumes:
   - name: singlefile
     hostPath:
       path: /path/on/the/host/to/the/file.txt
       type: File

Or if it's not a problem, you could mount the whole directory containing it at the expected path.


With this said, I want to point out that using hostPath is (almost always) never a good idea.

If you have a cluster with more than one node, saying that your Pod is mounting an hostPath doesn't restrict it to run on a specific host (even tho you can enforce it with nodeSelectors and so on) which means that if the Pod starts on a different node, it may behave differently, not finding the directory and / or file it was expecting.

But even if you restrict the application to run on a specific node, you need to be ok with the idea that, if such node becomes unavailable, the Pod will not be scheduled on its own somewhere else.. meaning you'll need manual intervention to recover from a single node failure (unless the application is multi-instance and can resist one instance going down)


To conclude:

  • if you want to mount a path on a particular host, for whatever reason, I would go for local volumes.. or at least use hostPath and restrict the Pod to run on the specific node it needs to run on.
  • if you want to mount small, textual files, you could consider mounting them from ConfigMaps
  • if you want to configure an application, providing a set of files at a certain path when the app starts, you could go for an init container which prepares files for the main container in an emptyDir volume

Upvotes: 3

Related Questions