Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount data file in kubernetes via pvc?

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

like image 864
zulv Avatar asked Mar 06 '23 16:03

zulv


1 Answers

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
like image 137
Const Avatar answered Mar 11 '23 22:03

Const