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

Đặt lịch snapshot PVC

Bên cạnh tính năng snapshot PV theo yêu cầu, FPT Cloud cho phép đặt lịch snapshot PVC định kỳ bằng CronJob của Kubernetes.

Thiết lập lịch CronJob cho snapshot PVC​

  1. Tạo ServiceAccount để CronJob có quyền trong cluster:

    apiVersion: v1
    kind: ServiceAccount
    metadata:
    name: 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 bạn sẽ chạy CronJob:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
    name: grant-snapshot-sa-admin
    subjects:
    - kind: ServiceAccount
    name: snapshot-sa
    namespace: default #or any other namespace that you want to deploy the cronjob
    - kind: ServiceAccount
    name: 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-snapshot-sa-admin -oyaml
  3. Apply CronJob để tự động đặt lịch snapshot PVC:

    apiVersion: batch/v1
    kind: CronJob
    metadata:
    name: pvc-snapshot #or any other name that you want to set
    namespace: default #or any other namespace that you want to deploy the cronjob, but must be in the same namespace with ServiceAccount and ClusterRoleBinding
    spec:
    schedule: "0 * * * *" #Adjust this (minute hour day(month) month day(week))
    timeZone: "Asia/Saigon"
    jobTemplate:
    spec:
    template:
    spec:
    serviceAccountName: snapshot-sa #Remember to match the name of ServiceAccount that deployed previously
    containers:
    - name: pvc-snapshot
    image: registry.fke.fptcloud.com/xplat-fke/snapshot-pvc:v0.1.0
    env:
    - name: PVC_NAME
    value: "your-pvc-name" #name of the PVC that you want to apply Cronjob to
    - name: NAMESPACE
    value: "your-pvc-namespace" #namespace of the above PVC
    restartPolicy: OnFailure

Xem danh sách snapshot​

Liệt kê snapshot để kiểm tra CronJob đang hoạt động:

kubectl get volumesnapshots.snapshot.storage.k8s.io -A
NAMESPACE NAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGE
example wp-pv-claim-example-2412020736 true wp-pv-claim 20Gi csi-cinder-snapclass snapcontent-045fe85e-7471-4bfb-9a55-2ed2ed2263dd 64m 64m
example wp-pv-claim-example-2412020836 true wp-pv-claim 20Gi csi-cinder-snapclass snapcontent-fd1333bc-70f6-417f-a91d-10349aa0647d 4m11s 4m11s

Tên snapshot wp-pv-claim-example-2412020736 được diễn giải như sau:

  • wp-pv-claim — tên PVC
  • example — tên namespace
  • 2412020736 — 24 (năm), 12 (tháng), 02 (ngày), 07 (giờ), 36 (phút), UTC+0
ghi chú

Mỗi PVC chỉ hỗ trợ 1 CronJob — tạo nhiều CronJob nếu cần đặt lịch cho nhiều PVC hoặc nhiều lịch khác nhau cho cùng 1 PVC. Khi Job mới chạy, nó tạo 1 pod thực hiện snapshot; kiểm tra bằng kubectl get volumesnapshots.snapshot.storage.k8s.io -n <namespace> hoặc kubectl get jobs.batch -n <namespace>.

Image do FPT Cloud cung cấp không có tính năng retention, nên tự xóa các snapshot cũ để tránh đầy quota disk storage.

Xóa lịch CronJob​

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