删除 K8s 部署时 NFS PersistentVolume 数据丢失

NFS PersistentVolume data lost when deleting K8s deployment

我使用 NFS 创建了一个 PersistentVolume,如下所示,当我删除部署时,我丢失了我的数据。如果我执行到 postgres 容器中,之前创建的数据库将不再存在。

使用 AWS EKS,我设法在不丢失任何数据的情况下删除了一个部署。

关于为什么会发生这种情况的任何帮助?

PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv001
spec:
  capacity:
    storage: 100Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /mnt/pv001
    server: 164.10.0.1

PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: metabase-postgres-persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

部署

...
    spec:
      volumes:
        - name: metabase-postgres-storage
          persistentVolumeClaim:
            claimName: metabase-postgres-persistent-volume-claim
...

我的mountPath错误,必须是/var/lib/postgresql/data

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: edw-pg
spec:
  serviceName: postgres-cluster-ip-service
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: postgres
          image: postgres:10.7
          ports:
            - containerPort: 5432  
          volumeMounts:
            - name: edw-persistent-storage-claim
              mountPath: /var/lib/postgresql/data
              readOnly: false
              subPath: postgres
          env:
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  name: pgpassword
                  key: PGPASSWORD
  volumeClaimTemplates:
  - metadata:
      name: edw-persistent-storage-claim
    spec:
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage: 50Gi