2

Right from start: very beginner in kubernetes here.

I (think to) know how I can create a volume for my StatefulSet for persistence. For that, I have

          volumeMounts:
            - name: db
              mountPath: /var/lib/mydb

and

    - metadata:
      name: db
      labels:
        app: myapp
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 50Gi 

I assume this would give my pods each (?) 50GB of space to persist their stuff.

This helps me test my app when the DB is empty and start from scratch. It would, I guess, be able to reuse the DB if pods were shutdown and restarted - that's how I understand this persistence.

However, I would like to be able to test with a DB with history also, with potentially several GB size already. This means, every pod would mount an existing DB and go from there. Therefore every pod needs to access the file but also needs to mount it independently and exclusively. I can't just mount an existing volume and have it shared between the pods.

The DB should be an on-disk key-value store like leveldb.

This would allow to test the app from different existing states of the DB.

Is this possible?

transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • 1
    What to you mean by mounting an existing DB? You may initialize your PVCs before starting your Statefulset, injecting the proper data. Either the Statefulset starts from scratch if no volumes were found, or it just loads what it finds. Say you create all PVCs first, start some init Pod then `kubectl cp` your database files to each volume, shut down the init Pod and create your Statefulset: it should start from there. – SYN Jan 28 '21 at 23:52
  • Yeah that sounds like what I need. By mounting an existing DB I mean copy an existing LevelDB into each pod so that each can use its own copy and continue from there. Does that make sense? How do I go about "inventing the proper data" before starting the StatefulSet? – transient_loop Jan 29 '21 at 00:31
  • 1
    When first starting your statefulset, it would create some PVC named after your STS. Say my STS is called `toto`, has 2 replicas, with a volumeClaimTemplate creating a `data`volume. Then, I can create a pair of PVC, `data-toto-0` and `data-toto-1`, even before creating my StatefulSet. Next, create a Job, a Pod, .... something that would mount your volume and either automatically set it up, or leave you to connect, extract your data the way you want them, ... once the volumes are ready, shut down your init Pod, and create your STS. Instead of new PVC, you'll re-use the one you prepared. – SYN Jan 29 '21 at 07:26
  • When doing this (pre-provisioning PVC with data), you should be careful about premissions. First, check what's the UID running your database container. When you'll init your PVC, make sure to use that same ID, try to reproduce the permissions you've seen on a running setup, ... – SYN Jan 29 '21 at 07:28

1 Answers1

1

Before starting playing with StatefulSets I advice you to read doc - statefulset-official-doc. Take a look also on dynamics-provisioning. Similary as @SYN said:

1. Create create PVCs - specify storage class and size there, even before creating StatefulSet.

2. Create a Job /Pod which would mount your volume and either automatically set it up and extract your data the way you want them. Mount it to the directory, which is used by your DB to store the data. When the volumes are ready, shut down your init Pod.

3. Create your StatefulSet. Instead of creating new PVCs, reuse ones you've prepared.

Malgorzata
  • 6,409
  • 1
  • 10
  • 27