<p>I have a Kubernetes deployment (apache flume to be exact) which needs to store persistent data. It has a PVC set up and bind to a path, which works without problem.</p>
<p>When I simply increase the scale of the deployment through kubernetes dashboard, it gives me an error saying multiple pods are trying to attach the same persistent volume. My deployment description is something like this (I tried to remove irrelevant parts)</p>
<pre><code>{
    &quot;kind&quot;: &quot;Deployment&quot;,
    &quot;apiVersion&quot;: &quot;extensions/v1beta1&quot;,
    &quot;metadata&quot;: {
        &quot;name&quot;: &quot;myapp-deployment&quot;,    
        &quot;labels&quot;: {
          &quot;app&quot;: &quot;myapp&quot;,
          &quot;name&quot;: &quot;myapp-master&quot;
        },    
    &quot;spec&quot;: {
    &quot;replicas&quot;: 1,
    &quot;selector&quot;: {
      &quot;matchLabels&quot;: {
        &quot;app&quot;: &quot;myapp&quot;,
        &quot;name&quot;: &quot;myapp-master&quot;
      }
    },
    &quot;template&quot;: {
      &quot;spec&quot;: {
        &quot;volumes&quot;: [
          {
            &quot;name&quot;: &quot;myapp-data&quot;,
            &quot;persistentVolumeClaim&quot;: {
              &quot;claimName&quot;: &quot;myapp-pvc&quot;
            }
          }
        ],
        &quot;containers&quot;: [
          {
            &quot;name&quot;: &quot;myapp&quot;,
            &quot;resources&quot;: {},
            &quot;volumeMounts&quot;: [
              {
                &quot;name&quot;: &quot;ingestor-data&quot;,
                &quot;mountPath&quot;: &quot;/data&quot;
              }
            ]
          }
        ]
      }
    },...
</code></pre>
<p>Each pod should get its own persistent space (but with same pathname), so one doesn't mess with the others'. I tried to add a new volume in the volume array above, and a volume mount in the volume mount array, but it didn't work (I guess it meant &quot;bind two volumes to a single container&quot;)</p>
<p>What should I change to have 2 pods with separate persistent volumes? What should I change to have N number of pods and N number of PVC's so I can freely scale the deployment up and down?</p>
<p>Note: I saw a similar question <a href="https://stackoverflow.com/questions/53997598/bind-different-persistent-volume-for-each-replica-in-a-kubernetes-deployment">here</a> which explains N number of pods cannot be done using deployments. Is it possible to do what I want with only 2 pods?</p>

uylmz
<p>You should use a <code>StatefulSet</code> for that. This is for pods with persistent data that should survive a pod restart. Replicas have a certain order and are named in that way (my-app-0, my-app-1, ...). They are stopped and restarted in this order and will mount the same volume after a restart/update.</p>
<p>With the <code>StatefulSet</code> you can use a <code>volumeClaimTemplates</code> to dynamically create new <code>PersistentVolumes</code> with the creation of a new pod. So every time a pod is created a volume get provisioned by your storage class.</p>
<p>From docs:</p>
<blockquote>
<p>The volumeClaimTemplates will provide stable storage using PersistentVolumes provisioned by a PersistentVolume Provisioner</p>
</blockquote>
<pre><code>volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      storageClassName: &quot;my-storage-class&quot;
      resources:
        requests:
          storage: 1Gi
</code></pre>
<p>See docs for more details:
<a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#components" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#components</a></p>
