How to mount data file in kubernetes via pvc?
Asked Answered
M

1

7

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 ?

image info:error log

pod yaml file

Mcclung answered 2/8, 2018 at 7:58 Comment(0)
B
9

how can I mount the data file in k8s ?

This is often application specific and there are several ways to do so, but mainly you want to read about subPath.

Generally, you can chose to:

  • use subPath to separate config files.
  • 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).
  • Use ConfigMaps (or even Secrets) 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 conf/interpreter.json looks like a fine example...

Notes to keep in mind:

  • 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.
  • 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:

    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
    

Edit:

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

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

Config map:

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

Deployment:

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

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

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: test.txt must exist on PV and that I'm using statefulset just to run with automatically provisioned pvc, and you can adjust accordingly...

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
Bituminize answered 2/8, 2018 at 8:44 Comment(6)
seems subPath could not solve the problem, subPath mainly for only mount specific sub-dir, not the whole dir, and also could not be the file.Mcclung
I've added some example of mounting data files using subPath that we are using in our manifests.Bituminize
I've also added example of using subPath to mount a single file from persistent volume to already populated folder. Example is tested on our cluster and works as intended, but you need to account for different way of provisioning persistent volumes (not related to subPath, just to give you pointers in adjusting example to your setup)Bituminize
I tried the config map, it could mount the file, but it does not solve my issue, the file content is empty, the content of the file could not be pre-populated. In my case, the interpreter.json is automatically and updated by the zeppelin service, I would like to mount the file interpreter.json outside of service to save the status in case the service restart.Mcclung
To make the issue clear. The isssue is that I would like to persistent one status file(status generated by the service) of some service in case the status lost when service restart, how to solve?Mcclung
You have three possibilities: If file can be pregenerated from stopped service (or empty) at start with known filename then place it on persistent volume, and mount it exactly as in my second example (PV) and you are good to go... If filename is dynamically generated on runtime without filename/content known in advance then you have to mount known folder below but are limited to whole folder-mount. Last resort is to use start script that will make symbolic link to file at some point during container start to mounted location outside of your app tree.Bituminize

© 2022 - 2024 — McMap. All rights reserved.