Month4 月 2021

GCP – GKE PVC Snapshot 設定

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

GCP-GKE Pod 硬碟複製掛載

最近在建置Kubernetes上的DB時,研究著如何進行災難復原然後摸索出了設定的流程

1.GKE啟用 Compute Engine 永久磁碟 CSI 驅動程式

2.找到DB PVC對應的硬碟,並複製,下圖我將300G的磁碟複製成另一顆sqlserver-copy

3.設定新的PV & PVC 將剛剛複製的硬碟設定至GKE讓Pod可以掛載

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sqlserver-volume-pv
  namespace: sqlserver
spec:
  persistentVolumeReclaimPolicy: Delete
  storageClassName: ""
  capacity:
    storage: 300G
  accessModes:
    - ReadWriteOnce
  claimRef:
    namespace: sqlserver
    name: sqlserver-volume-pvc
  gcePersistentDisk:
    pdName: sqlserver-copy
    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sqlserver-volume-pvc
  namespace: sqlserver
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 300G

4.啟動新的sqlserver服務,並設定pvc為上一步驟製作的名稱

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sqlserver-sts
  namespace: sqlserver
spec:
  serviceName: sqlserver-svc
  replicas: 1
  selector:
    matchLabels:
      app: ms-sqlserver
  template:
    metadata:
      labels:
        app: ms-sqlserver
    spec:
      terminationGracePeriodSeconds: 10
      securityContext:
        fsGroup: 10001
      containers:
      - name: ms-sqlserver
        image: mcr.microsoft.com/mssql/server:2019-latest
        ports:
        - containerPort: 1433
        resources:
          requests:
            cpu: 2
            memory: 6Gi
        env:
        - name: MSSQL_PID
          value: "Developer"
        - name: ACCEPT_EULA
          value: "Y"
        - name: MSSQL_COLLATION
          value: "Chinese_Taiwan_Stroke_CI_AS"
        - name: MSSQL_AGENT_ENABLED
          value: "true"
        - name: SA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: sqlserver-secret
              key: SA_PASSWORD
        volumeMounts:
        - name: sqlserver-volume
          mountPath: /var/opt/mssql
      volumes:
      - name: sqlserver-volume
        persistentVolumeClaim:
          claimName: sqlserver-volume-pvc

5.檢查相關設定是否正常,以及檢查兩台DB內容是否相同

參考資料

  1. https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/gce-pd-csi-driver
  2. https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/preexisting-pd

© 2024 Kila's IT Home

Theme by Anders NorénUp ↑