<p>We have realised the mistake of using a Deployment with a PVC for our stateful app instead of going with Statefulset. I was wondering how the upgrade would work. How can I point to the old data with the new statefulset ? I am guessing that the old PVC cannot be used by the volumeClaimTemplate ? I have not found anything via Google with my search abilities.</p>
<p>Did anyone else go through this phase ? If you have, what was the process you followed ?</p>
<p>Thanks.</p>
<p>Adding some more details regarding the setup.</p>
<ol>
<li>Currently it is a simple deployment with no replicas. Just 1 deployment and 1 pod.</li>
<li>PV+PVC is used to have a persistent volume mounted where we write all of the data.</li>
<li>On Helm upgrade, we have a pre-upgrade hook added which mounts the same PV+PVC into the upgrade container and upgrades the data (Modifying XML files etc)</li>
</ol>
<p>It is simple, but the helm chart is bit too complicated with lots of other noise, but basically the application can be considered as simple as above.</p>
<p>Now, what I am looking for in my next upgrade is a process where I can make the above deployment as a statefulset and also have all the data still usable by the Pod.</p>

<p>So, here is what I did. First, let me brief about my test setup:</p>
<ol>
<li>Have a set of deployments running which are having their own PVC+PV for persistence.</li>
<li>New release has all of those deployments converted into statefulsets, thus have different PVC.</li>
</ol>
<p>Problems:</p>
<ol>
<li><p>You cannot use the PV from earlier deployment because a PV is bound to a single PVC. You can refer to the PV using only that PVC.</p>
</li>
<li><p>So another question is, why can't you mount the old PV using it's PVC to new pod and copy that ?
Well, we cannot do that because, by the time the new pods are started, the old pods are released thereby releasing its associated PVC and PV. In my case I could see the PVC going to 'Terminate' state. It probably could be solved with some kind of Reclaim policy, but I am not sure of that.</p>
</li>
</ol>
<p>Solution:</p>
<ol>
<li><p>Create the PV and its PVC's manually and apply them. The PVC name should match the name that the statefulset would create which is quite straight forward. The PVC name matching for statefulset is very important, it expects it to be in certain format which you can find online.</p>
</li>
<li><p>In your new Helm chart, create a pre-upgrade kubernetes Job. This is pre-upgrade hook of Helm which is run just before the actual upgrade of Helm release.
So, here you create a container and mount both old and new PV (use the PVC you created manually).</p>
</li>
<li><p>Now, this container must run something right to copy the data ? Yeah, for that I created a new configmap and put a script in it which would just copy the data from the old PV to new PV. This configmap is mounted inside the container and the container is made to execute that command.</p>
</li>
<li><p>Run the helm upgrade command and see it in action.</p>
</li>
</ol>
<p>A rough example of the K8s pre-upgrade job:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}-pre-upgrade-job
  namespace: {{ .Values.namespace.name }}
  annotations:
    &quot;helm.sh/hook&quot;: pre-upgrade
spec:
  template:
    spec:
      containers:

      - name: upgrade82-copy
        image: &lt;your-image&gt;
        command: [&quot;/bin/bash&quot;]
        args: [&quot;-c&quot;, &quot;/scripts/upgrade82.sh&quot;]

        volumeMounts:
        - name: old-data
          mountPath: /usr/old
          readOnly: false

        - name: new-data
          mountPath: /usr/new
          readOnly: false

        - name: scripts
          mountPath: /scripts
          readOnly: false

      restartPolicy: Never
      volumes:
        - name: old-data
          persistentVolumeClaim:
            claimName: old-claim &lt;&lt; Needs to be hardcoded to what is running

        - name: new-data
          persistentVolumeClaim:
            claimName: new-data-namespace-app-0 &lt;&lt; There is a specific format for statefulset PVC

        - name: scripts
          configMap:
           name: upgrade
           defaultMode: 0755

  backoffLimit: 4
  #activeDeadlineSeconds: 200
  ttlSecondsAfterFinished : 100
</code></pre>
<p>And &quot;upgrade&quot; above is another Configmap with your copy script.</p>
