<p>I want to persistent data file via pvc with glusterfs in kubernetes, I mount the diretory and it'll work, but when I try to mount the file, it'll fail, because the file was mounted to the directory type, how can I mount the data file in k8s ?</p>

<p>image info:<a href="https://i.stack.imgur.com/86jeC.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/86jeC.jpg" alt="error log"></a></p>

<p><a href="https://i.stack.imgur.com/GVLMK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/GVLMK.png" alt="pod yaml file"></a></p>

<blockquote>
  <p>how can I mount the data file in k8s ?</p>
</blockquote>

<p>This is often application specific and there are several ways to do so, but mainly you want to read about <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="noreferrer">subPath</a>.</p>

<p>Generally, you can chose to:</p>

<ul>
<li>use <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="noreferrer">subPath</a> to separate config files.</li>
<li>Mount volume/path as directory at some other location and then link file to specific place within pod (in rare cases that mixing with other config files or directory permission in same dir is presenting an issue, or boot/start policy of application prevents files from being mounted at the pod start but are required to be present after some initialization is performed, really edge cases).</li>
<li>Use <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="noreferrer">ConfigMaps</a> (or even <a href="https://kubernetes.io/docs/concepts/configuration/secret/" rel="noreferrer">Secrets</a>) to hold configuration files. Note that if using subPath with configMap and Secret pod won't get updates there automatically, but is more common way of handling configuration files, and your <code>conf/interpreter.json</code> looks like a fine example...</li>
</ul>

<p>Notes to keep in mind:</p>

<ul>
<li>Mounting is "overlaping" underlying path, so you have to mount file up to the point of file in order to share its folder with other files. Sharing up to a folder would get you folder with single file in it which is usually not what is required.</li>
<li><p>If you use ConfigMaps then you have to reference individual file with subPath in order to mount it, even if you have a single file in ConfigMap. Something like this:</p>

<pre><code>containers:
- volumeMounts:
  - name: my-config
    mountPath: /my-app/my-config.json
    subPath: config.json
volumes:
- name: my-config
  configMap:
    name: cm-my-config-map-example
</code></pre></li>
</ul>

<h2>Edit:</h2>

<h3>Full example of mounting a single <code>example.sh</code> script file to <code>/bin</code> directory of a container using <code>ConfigMap</code>.</h3>

<p>This example you can adjust to suit your needs of placing any file with any privilege in any desired folder. Replace <code>my-namespace</code> with any desired (or remove completely for <code>default</code> one) </p>

<p>Config map:</p>

<pre><code>kind: ConfigMap
apiVersion: v1
metadata:
  namespace: my-namespace
  name: cm-example-script
data:
  example-script.sh: |
     #!/bin/bash
     echo "Yaaaay! It's an example!"
</code></pre>

<p>Deployment:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: my-namespace
  name: example-deployment
  labels:
    app: example-app
spec:
  selector:
    matchLabels:
      app: example-app
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - image: ubuntu:16.04
        name: example-app-container
        stdin: true
        tty: true
        volumeMounts:
          - mountPath: /bin/example-script.sh
            subPath: example-script.sh
            name: example-script
      volumes:
      - name: example-script
        configMap:
          name: cm-example-script
          defaultMode: 0744
</code></pre>

<h3>Full example of mounting a single <code>test.txt</code> file to <code>/bin</code> directory of a container using persistent volume (file already exists in root of volume).</h3>

<p>However, if you wish to mount with persistent volume instead configMap, here is another example of mounting in much the same way (test.txt is mounted in /bin/test.txt)... Note two things: <code>test.txt</code> must exist on PV and that I'm using statefulset just to run with automatically provisioned pvc, and you can adjust accordingly...</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: my-namespace
  name: ss-example-file-mount
spec:
  serviceName: svc-example-file-mount
  replicas: 1
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
        - image: ubuntu:16.04
          name: example-app-container
          stdin: true
          tty: true
          volumeMounts:
            - name: persistent-storage-example
              mountPath: /bin/test.txt
              subPath: test.txt
  volumeClaimTemplates:
  - metadata:
      name: persistent-storage-example
    spec:
      storageClassName: sc-my-storage-class-for-provisioning-pv
      accessModes: [ ReadWriteOnce ]
      resources:
        requests:
          storage: 2Gi
</code></pre>
