Retain static PersistentVolumeClaim

Change Claim to static hostPath volume

cat >~/carpool/postgres/postgres-pv-claim.yaml <<EOF
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: ""
  volumeName: postgres-pv
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
EOF

Create static PersistenceVolume

cat >~/carpool/postgres/postgres-pv.yaml <<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv 
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  claimRef:
    name: postgres-pv-claim
    namespace: carpool
  hostPath:
    path: /data
    type: DirectoryOrCreate
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - k3d-cnbc-agent-1
  persistentVolumeReclaimPolicy: Retain
  storageClassName: ""
  volumeMode: Filesystem
EOF

Szenario:

kubectl delete namespace carpool
kubectl delete pv <...>

kubectl create namespace carpool
kubectl apply -f postgres
kubectl apply -f app 
kubectl run curl -ti --image=curlimages/curl -- /bin/sh
cd /tmp
cat >car_1.json <<EOF
{
  "license_plate": "BO-PR-72",
  "car_type": "mini clubman SD",
  "fuel": "diesel",
  "number_of_seats": "5"
}
EOF
curl -i -X POST http://carpool:5000/car \
  -H "Content-Type: application/json" \
  --data-binary "@car_1.json"

curl http://carpool:5000/car
exit

# delete again and check retaining of pv
kubectl delete namespace carpool
kubectl get pv

# if not edit Postgres pvc and pod pending

kubectl edit pv <...>
# remove claim-ref uid and resource version
kubectl create ns carpool
kubectl apply -f postgres
kubectl apply -f app 
kubectl run curl -ti --image=curlimages/curl -- /bin/sh
curl http://carpool:5000/car
# data exists
exit

