zalando / postgres-operator

Postgres operator creates and manages PostgreSQL clusters running in Kubernetes
https://postgres-operator.readthedocs.io/
MIT License
4.11k stars 949 forks source link

Feature: Restore cluster to existing pv's (with the same name with the same namespace) #1900

Open felrivero opened 2 years ago

felrivero commented 2 years ago

Hello everyone! Could you add functionality to install a cluster with existing pv (the case when someone accidentally deleted the cluster but the pv disks remained). I made a script that can generate pvc from existing pv deleted cluster, which is then successfully mounted by the operator - I checked (it is important that the namespace, cluster name and TeamID match the old values)

#!/bin/bash
VOLUME_SIZE=$2
NAMESPACE=$3
if [[ -z $3 ]];then
  echo No args
  echo Use command for example:
  echo $0 10Gi namespace "pv_id_1 pv_id_2 pv_id_3"
  exit 1
fi
EGREP_STR=$(echo $2 | tr ' ' '|')
PVC=$(kubectl get pv|egrep $EGREP_STR)
if [[ -n $PVC ]];then
  while IFS= read -r line
  do
    PV_NAME=$(echo $line|awk '{print $1}')
    PVC_NAME=$(echo $line|awk '{print $6}'|cut -d '/' -f 2)
    kubectl -n $NAMESPACE patch pv  --type json -p '[{"op": "remove", "path": "/spec/claimRef"}]' $PV_NAME
cat <<EOF | kubectl -n $NAMESPACE apply -f -
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: ${PVC_NAME}
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: ${VOLUME_SIZE}
  volumeName: ${PV_NAME}
EOF
  break
  done <<< "$PVC"
else
  echo "PVC var is empy"
  exit 1
fi
JulesLalu commented 1 year ago

Hi,

Also interested by this feature, do you plan to add it ?

AlexLyubimov commented 1 year ago

Looks good :+1:

ngnix commented 1 year ago

Hey guys, do you have any update?