Chuyển tới nội dung chính

Đặt lịch xóa snapshot PVC

FPT Cloud cho phép đặt lịch xóa snapshot PVC theo tên PVC, namespace và thời gian lưu trữ (retention) tính bằng giờ hoặc ngày.

Thiết lập lịch CronJob dọn dẹp snapshot​

  1. Tạo ServiceAccount để CronJob có quyền trong 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 ClusterRoleBinding để cấp quyền cho ServiceAccount đó. subjects phải liệt kê đủ mọi ServiceAccount ở các namespace đã tạo ở trên:

    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

    Kiểm tra bằng:

    kubectl get clusterrolebinding grant-cleanup-snapshot-sa-admin
  3. Apply CronJob để tự động xóa snapshot PVC:

    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

Xóa CronJob dọn dẹp​

Để dừng đặt lịch xóa snapshot cho 1 PVC, xóa CronJob tương ứng.