Backups get deleted, but the disk keeps filling up: Velero and Longhorn snapshot buildup
Velero's CSI backups create a snapshot in Longhorn every night, and the snapshot stays behind even after the backup is deleted. Spotting the buildup and cleaning it up with a RecurringJob.
The backup policy was clear: back up at night, keep it in object storage for 14 days, then delete it. Despite that, one morning a "snapshot space is bloated" alert came in. And not on one of the volumes whose data should change the least, but on a volume that holds logs.
Symptoms
- In the Longhorn UI, every volume has dozens of snapshots under it; the dates go all the way back to the day backups started.
- On write-heavy volumes (logs, metrics), actual usage has gone past twice the volume size.
- Old backups in object storage, on the other hand, are being deleted properly. So it's not that "backup retention isn't working".
Why does it happen?
When Velero takes a backup with CSI snapshots, it creates a snapshot in Longhorn for each volume and turns it into a backup. When the backup expires, Velero deletes the backup from object storage. But the local snapshot on the volume can stay behind, depending on how things are set up: if the VolumeSnapshotContent is left with Retain, nobody touches it.
In Longhorn, each snapshot holds the blocks that changed after it. On a volume with few writes, you won't notice. But on a log volume writing a few GB a day, a new layer gets added every night and none of them are ever deleted.
Diagnosis
# Snapshot count per volume
kubectl -n longhorn-system get snapshots.longhorn.io \
-o jsonpath='{range .items[*]}{.spec.volume}{"\n"}{end}' | sort | uniq -c | sort -rn | head
# Snapshot contents left behind by Velero, and their deletion policies
kubectl get volumesnapshotcontent \
-o custom-columns=NAME:.metadata.name,POLICY:.spec.deletionPolicy,CREATED:.metadata.creationTimestamp
If the snapshot count is close to the number of days and deletionPolicy is Retain, that's where the buildup comes from.
Fix: regular cleanup with a Longhorn RecurringJob
Longhorn's own snapshot-delete task deletes everything except the last N snapshots on each volume. It doesn't touch the backups in object storage.
apiVersion: longhorn.io/v1beta2
kind: RecurringJob
metadata:
name: snapshot-sil
namespace: longhorn-system
spec:
task: snapshot-delete
cron: "0 1 * * *" # at night, after the backup window
retain: 2 # keep the last two snapshots
concurrency: 1 # process volumes one at a time
groups: ["default"]
Don't run the first pass during the day. While deleted snapshots are being merged, disk I/O goes up. We kicked off the first round by hand in the test environment, watched the results, then moved on to the other clusters one by one.
Watch out
- On detached volumes, the snapshot can be deleted but the merge can't happen; it stays marked and completes once the volume is attached.
- Don't set
retainlower than your backup schedule needs. The snapshot required for Velero's current backup must not be deleted. Scheduling the job for the hours after the backup solves this. - If you use GitOps, put the RecurringJob in the repo as a manifest too. Otherwise it gets forgotten at the next install.
Checklist
- [ ] Does the snapshot count per volume level off within a few days?
- [ ] Has the "snapshot space" alert cleared?
- [ ] Is the number of backups in object storage unchanged?
- [ ] Does a restore drill succeed with the new setup?