Snapshots through PVC

This guide provides step-by-step instructions for setting up Dragonfly with snapshots through PVC. Dragonfly already supports snapshots, which allows you to create a snapshot of your database at any point in time and restore it later. With this PVC integration, you can now store your snapshots in persistent volumes and get them automatically restored when your Dragonfly pods get restarted or rescheduled across nodes. This is possible as a specific PVC is attached to each Dragonfly statefulset and the snapshot is stored in that PVC. The persistent volume can be backed by any storage provider that Kubernetes supports.

While this feature can help you recover from failures, It is not a replacement for Replication. You should still use replication if you want to maintain high availability.

Prerequisites

Deploying Dragonfly with Snapshots through PVC

Apply a Dragonfly manifest with PVC enabled on the default storage class:

  1. kubectl apply -f - <<EOF
  2. apiVersion: dragonflydb.io/v1alpha1
  3. kind: Dragonfly
  4. metadata:
  5. name: dragonfly-pvc
  6. spec:
  7. replicas: 1
  8. snapshot:
  9. cron: "*/5 * * * *"
  10. persistentVolumeClaimSpec:
  11. accessModes:
  12. - ReadWriteOnce
  13. resources:
  14. requests:
  15. storage: 2Gi
  16. EOF

This will create a Dragonfly statefulset with the given PVC spec. The persistentVolumeClaimSpec field is the same as the one used in Kubernetes PVC and can be used to configure the PVC as per your requirements.

Wait for the Dragonfly instance to be ready:

  1. kubectl describe dragonflies.dragonflydb.io dragonfly-pvc

Testing

Connect to the Dragonfly instance and add some data:

  1. kubectl run -it --rm --restart=Never redis-cli --image=redis:7.0.10 -- redis-cli -h dragonfly-pvc.default SET foo bar
  2. If you don't see a command prompt, try pressing enter.
  3. pod "redis-cli" deleted

Delete the Dragonfly pod:

  1. kubectl delete pod dragonfly-pvc-0

Wait for the pod to be recreated:

  1. kubectl get pods -w

Connect to the Dragonfly instance, and check if the data is still there:

  1. kubectl run -it --rm --restart=Never redis-cli --image=redis:7.0.10 -- redis-cli -h dragonfly-pvc.default GET foo
  2. "bar"
  3. pod "redis-cli" deleted

You should see the value bar for the key foo. This means that the data was restored from the snapshot that was stored in the PVC.