Sub topic 11
Backing up persistent volume data
This chapter assumes you can run kubectl, know what a PersistentVolumeClaim (PVC) is, and have Velero installed with a working backup storage location. An etcd snapshot stores the PVC object, but not the files on the disk behind it. Velero can capture that data in two ways.
The two approaches
| Aspect | CSI snapshots | File system backup |
|---|---|---|
| How data is captured | The storage driver takes a volume snapshot | The node agent reads files and uploads them to object storage |
| Needs a CSI driver with snapshot support | Yes | No |
| Works across storage systems | Only within the snapshot's storage system, unless data is moved | Yes, any volume type that is mounted into a Pod |
| Consistency | Crash-consistent at the block level | Files are read while the Pod may still write to them |
| Speed on large volumes | Fast, because the snapshot is taken by the storage system | Slower, because every file is read and uploaded |
Choose CSI snapshots when your storage driver supports them and you restore into the same kind of storage. Choose file system backup when it does not, or when you need the data to be portable.
Running a file system backup
The install command in the previous chapter used --use-node-agent. That flag deploys the node agent, a DaemonSet that reads volume data on each node. File system backup needs it.
By default, Velero opts volumes out of file system backup. The flag --default-volumes-to-fs-backup opts all Pod volumes in:
velero backup create shop-backup \
--include-namespaces shop \
--default-volumes-to-fs-backup \
--ttl 720h0m0s
velero backup describe shop-backup --detailsTo choose volumes one by one, leave the flag off and annotate each Pod with the volume names to include:
kubectl -n shop annotate pod/db-0 \
backup.velero.io/backup-volumes=dataThe --details output lists the volumes that were backed up and the status of each upload.
What the backup contains
The Velero server saves the Kubernetes object manifests to the bucket. For each volume, it creates a PodVolumeBackup resource. The node agent on the node running the Pod picks it up, reads the files, and uploads them.
Common problems
- Backup finishes as
PartiallyFailed. Runvelero backup describe shop-backup --detailsandvelero backup logs shop-backup. A common cause is a node agent Pod that is not running on the node hosting the volume. - A volume is missing from the backup. Check that it is mounted by a running Pod. Volumes such as
emptyDir,configMapandsecretare not backed up as volume data. - Inconsistent database files. File system backup copies files while the application may be writing. For databases, use hooks to flush or freeze writes first. Hooks are covered in a later chapter.
Exercise
Your cluster uses a storage driver with no CSI snapshot support. You need to back up the PVC data of the shop namespace. Which approach do you use, and which two things must be in place?
Show answer
Use file system backup. The node agent must be installed (velero install --use-node-agent), and the volumes must be opted in, either with --default-volumes-to-fs-backup on the backup or with the backup.velero.io/backup-volumes annotation on the Pods.