3

When I helm delete <deploymentname> a deployment its PVs and PVCs are deleted as well. How can I avoid the actual data in the PVs from being deleted and be able to reclaim those PVs when I helm install <deploymentname> again?

I am using helm upgrade regularly but it makes me very uncomfortable if all it takes to delete all data is a helm delete (even without --purge option it removes all PVs)

Hedge
  • 16,142
  • 42
  • 141
  • 246
  • 1
    I guess you don't want to use `helm upgrade`? – Ryan Dawson Nov 07 '18 at 09:31
  • I am using it regularly but it makes me very uncomfortable if all it takes to delete all data is `helm delete` (even without --purge option it removes all PVs) – Hedge Nov 07 '18 at 09:40

2 Answers2

6

Assuming you are using the default Storage-class, the only way to avoid a Helm chart to delete the PV/PVCs used on it it's creating the PVCs beforehand so they are not managed by the Helm release.

The only exception is StatefulSets which by definition never delete their PVCs even when they are created by the Helm release

The other option, if your Helm charts allow it, is using a Storage-class with reclaimPolicy: Retain, that will avoid deleting your PVs when your deployments, daemon-sets pods PVCs are detached and deleted

  • I created a storage class with reclaimPolicy retain. When delete and install the Helm chart again different PVs are being created because most of my Helm charts use Services instead of StatefulSets. Sadly uo to that part I was able to use most of the helm charts as is but having to switch to DaemonSets means I need to create my own forks of them. Do you have an idea about this? – Hedge Nov 11 '18 at 22:14
5

If you are looking for persistence between deletion and re-creation, you should not use Deployment but StatefulSet. Stateful sets are something designed to be used for deploying "database-like" applications.

Stateful sets use persistent pod naming and support generating pvc per pod, also with persistent name. Those pvcs are not deleted when pods/stateful sets are deleted so they remain for reuse by recreated stateful sets or manual release by deleting the pvc(s).

Example StatefulSet took from https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/ is attached below.

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
Petr Sýkora
  • 114
  • 5