最近在建置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