Best practice on reading and writing to a persistent Volume in a live Kubernetes Cluster

3/4/2021

I am designing a Kubernetes system which will require storing audio files. To do this I would like to setup a persistent storage volume making use of a stateful set.

I have found a few tutorials on how to set something like this up, but I am unsure once I have created it how to read/write the files. What would be the best approach to do this. I will be using a flask app, but if I could just get a high level approach then I can find the exact libraries myself.

-- EoinHanan
kubernetes
kubernetes-statefulset

1 Answer

3/4/2021

Not acknowledging on facts how it should be implemented programming wise and the specific tuning for dealing with audio files, you can use your Persistent Volume the same as you would read/write data to a directory (as correctly pointed by user @zerkms in the comments).

Answering this specific part of the question:

but I am unsure once I have created it how to read/write the files.

Assuming that you've created your StatefulSet in a following way:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: ubuntu-sts
spec:
  selector:
    matchLabels:
      app: ubuntu # has to match .spec.template.metadata.labels
  serviceName: "ubuntu"
  replicas: 1
  template:
    metadata:
      labels:
        app: ubuntu
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: ubuntu
        image: ubuntu
        command:
        - sleep 
        - "infinity" 
        volumeMounts:
        - name: audio-volume
          mountPath: /audio 
  volumeClaimTemplates: 
  - metadata:
      name: audio-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: standard
      resources:
        requests:
          storage: 1Gi

Take a look on below part (it's showing where your Volume will be mounted):

        volumeMounts:
        - name: audio-volume
          mountPath: /audio # <-- HERE!

Disclaimer!

This example is having the 1:1 Pod to Volume relation. If your use case is different you will need to refer to the Kubernetes documentation about accessModes.

You can exec into this Pod to look how you can further develop your application:

  • $ kubectl exec -it ubuntu-sts-0 -- /bin/bash
  • $ echo "Hello from your /audio directory!" > /audio/hello.txt
  • $ cat /audio/hello.txt
root@ubuntu-sts-0:/# cat /audio/hello.txt 
Hello from your /audio directory!

A side note!

If it happens that you are using the cloud-provider managed Kubernetes cluster like GKE, EKS or AKS, please refer to it's documentation about storage options.

I encourage you to check the official documentation on Persistent Volumes:

Also, please take a look on documentation regarding Statefulset:


Additional resources:

-- Dawid Kruk
Source: StackOverflow