Post thumbnail

Pattern for Dynamic Backup/Restore of Kubernetes Application Data

The Context

With container technologies like Docker and Kubernetes, Pods and Containers are ephemeral. All data is lost after a restart. The way to maintain application data after a restart is to attach a volume to the pod.
This doesn't address one thing however: the volume needs to reside on a storage backend and the volume will inherit the constraints of the storage backend.
Some of those constraints are:

A Possible Approach

The approach described here is to set up a dynamic backup and restore directly in the lifecycle of the Pod. The technology supporting this workflow is Restic.

Kubernetes backup workflow

Here is how the workflow works:

Use Cases and Limitations

This pattern can be useful for some cases:

Implementation Example

The following tool is an example of implementation of this workflow: https://github.com/devopsplaybook-io/container-utils/

An example of a Kubernetes definition can be:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-application
  labels:
    app: my-application
spec:
  selector:
    matchLabels:
      app: my-application
  template:
    metadata:
      labels:
        app: my-application
    spec:
      containers:
        - image: my-application
          name: my-application
          volumeMounts:
            - mountPath: /data
              name: pod-volume
        - name: backup
          image: restic/restic:latest
          command: ["sh", "-c"]
          args:
            - wget -O /tmp/container-backup.sh https://raw.githubusercontent.com/devopsplaybook-io/container-utils/init/container-backup.sh && chmod +x /tmp/container-backup.sh && /tmp/container-backup.sh
          volumeMounts:
            - mountPath: /data
              name: pod-volume
          env:
            - name: BACKUP_FOLDER
              value: "/data"
            - name: BACKUP_RESTIC_REPO
              value: "... ..."
            - name: RESTIC_PASSWORD
              value: "... ..."
            - name: BACKUP_DO_PROCESS
              value: "Y"
            - name: BACKUP_DO_START_DELAY
              value: "10800"
            - name: BACKUP_DO_LOOP_FREQUENCY
              value: "10800"
      initContainers:
        - name: init
          image: restic/restic:latest
          command: ["sh", "-c"]
          args:
            - "wget -O /tmp/container-backup.sh https://raw.githubusercontent.com/devopsplaybook-io/container-utils/main/container-backup.sh && chmod +x /tmp/container-backup.sh && /tmp/container-backup.sh"
          volumeMounts:
            - mountPath: /data
              name: pod-volume
          env:
            - name: BACKUP_FOLDER
              value: "/data"
            - name: BACKUP_RESTIC_REPO
              value: "... ..."
            - name: RESTIC_PASSWORD
              value: "... ..."
            - name: BACKUP_DO_RESTORE
              value: "Y"
      volumes:
        - name: pod-volume
          emptyDir: {}