GKE 啟用CSI之後,可以針對相關的Class型態進行Snapshot設定,直接對PVC進行備份,並在日後可以直接還原使用

首先先設置csi使用的class,使用預設也可,但預設需要掛載在pod上才會創建對應的磁區,這裡為了展示所以使用自定義的class

vi csi-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: csi-pd
provisioner: pd.csi.storage.gke.io
volumeBindingMode: Immediate
allowVolumeExpansion: true
parameters:
  type: pd-balanced
kubectla apply -f csi-class.yaml

接著建立PVC

vi ftp-volume.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ftp-volume
  namespace: ftp
spec:
  storageClassName: csi-pd
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
kubectl apply -f ftp-volume.yaml

接著開始設定snapshot class

vi snapshot-class.yaml
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
  name: snapshot-class
driver: pd.csi.storage.gke.io
deletionPolicy: Delete
kubectl apply -f snapshot-class.yaml

然後就可以開始進行備份的設定

vi ftp-snapshot.yaml
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
  name: ftp-snapshot
  namespace: ftp
spec:
  volumeSnapshotClassName: snapshot-class
  source:
    persistentVolumeClaimName: ftp-volume
kubectl apply -f ftp-snapshot.yaml

這時就會開始進行snapshot,接著可以查詢是否已經備份

kubectl get volumesnapshotcontents -n ftp

NAME                                               AGE
snapcontent-02628a1a-a89c-4255-9a40-0fccbfc4bc5d   6s

接著可以確認是snapshot是否可以使用

kubectl get volumesnapshot -n ftp \
        -o custom-columns='NAME:.metadata.name,READY:.status.readyToUse'

當顯示 READY為True時,代表此snapshot已經完成,並可以使用

NAME           READY
ftp-snapshot   true

接著將snapshot設置還原為PVC

vi back-to-pvc.yam
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ftp-bcakup
  namespace: ftp
spec:
  dataSource:
    name: ftp-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
kubectl apply -f back-to-pvc.yaml

然後查詢PVC

kubectl get pvc -n ftp

看到出現PVC就代表還原完成,可以掛載給pod使用了

NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
ftp-bcakup   Bound    pvc-78dca4d4-905f-4111-a2f2-283aa33d829e   100Gi      RWO            standard       36s
ftp-volume   Bound    pvc-bbd19786-7b92-4b35-877f-e48aedb92ba8   100Gi      RWO            csi-pd         16m