Skip to main content

Schedule PVC Snapshot Deletion

FPT Cloud lets you schedule deletion of PVC snapshots by PVC name, namespace, and a retention period in hours or days.

Set up a CronJob schedule for snapshot cleanup​

  1. Create a ServiceAccount so the CronJob has permissions in the cluster:

    apiVersion: v1
    kind: ServiceAccount
    metadata:
    name: cleanup-snapshot-sa
    namespace: default #or any other namespace that you want to deploy the cronjob
  2. Apply a ClusterRoleBinding to grant permissions to that ServiceAccount. subjects must include every ServiceAccount across the namespaces created above:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
    name: grant-cleanup-snapshot-sa-admin
    subjects:
    - kind: ServiceAccount
    name: cleanup-snapshot-sa
    namespace: default #or any other namespace that you want to deploy the cronjob
    - kind: ServiceAccount
    name: cleanup-snapshot-sa
    namespace: namespace1 #if user wants to have multiple cronjob in multiple namespace
    roleRef:
    kind: ClusterRole
    name: cluster-admin
    apiGroup: rbac.authorization.k8s.io

    Verify it with:

    kubectl get clusterrolebinding grant-cleanup-snapshot-sa-admin
  3. Apply a CronJob to automatically delete PVC snapshots:

    apiVersion: batch/v1
    kind: CronJob
    metadata:
    name: cleanup-snapshots
    spec:
    schedule: "0 2 * * *" #Adjust this (minute hour day(month) month day(week))
    timeZone: "Asia/Saigon"
    jobTemplate:
    spec:
    template:
    spec:
    serviceAccountName: cleanup-snapshot-sa #match with service account name
    containers:
    - name: snapshot-cleanup
    image: registry.fke.fptcloud.com/xplat-fke/cleanup-snapshot-pvc:v0.1.0
    env:
    - name: NAMESPACE
    value: "your-pvc-namespace" #Replace with your PVC namespace
    - name: PVC_NAME
    value: "your-pvc" #Replace with your PVC name
    - name: RETENTION_MAX
    value: "7" #or any int > 0
    - name: RETENTION_UNITS
    value: "days" #or "hours"
    restartPolicy: OnFailure

Remove the cleanup CronJob​

To stop scheduled snapshot deletion for a PVC, delete the corresponding CronJob.