<p>I have a small problem and need to know what is the best way to approach this/solve my issue.</p>

<p>I have deployed few pods on Kubernetes and so far I have enjoyed learning about and working with Kubernetes. Did all the persistent volume, volume claim...etc. and can see my data on the host, as I need those files for further processing.</p>

<p>Now the issue is 2 pods (2 replicas) sharing the same volume claim are writing to the same location on the host, expected, but unfortunately causing the data to be duplicated in the output file.</p>

<p>What I need is:</p>

<ul>
<li>To have a unique output of each pod on the host. Is the only way to achieve this is by
having two deployment files, in my case, and each to use a different volume claim/persistent
volume ? At the same time not sure if this is an optimal approach for future updates, upgrades, availability of certain number of pods ... etc.</li>
<li>Or can I still have one deployment file with 2 or more replicas and still avoid the output duplication when sharing the same pvc ?  </li>
</ul>

<p>Please note that I have one node deployment and that's why I'm using hostpath at the moment.</p>

<p>creating pv:</p>

<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: ls-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/ls-data/my-data2"
</code></pre>

<p>claim-pv:</p>

<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: ls-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
</code></pre>

<p>How I use my pv inside my deployment:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: logstash
  namespace: default
  labels:
    component: logstash
spec:
  replicas: 2
  selector:
    matchLabels:
     component: logstash
#omitted 
        ports:
        - containerPort: 5044
          name: logstash-input
          protocol: TCP
        - containerPort: 9600
          name: transport
          protocol: TCP
        volumeMounts:
        - name: ls-pv-store
          mountPath: "/logstash-data"
      volumes:
      - name: ls-pv-store
        persistentVolumeClaim:
         claimName: ls-pv-claim
</code></pre>

<p>Depending on what exactly you are trying to achieve you could use <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noreferrer">Statefulsets</a> instead of Deployments. Each Pod spawn from the Statefulset's Pod template can have it's own separate PersistentVolumeClaim that is created from the volumeClaimTemplate (see the link for an example). You will need a <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#storageclasses" rel="noreferrer">StorageClass</a> set up for this.</p>

<p>If you are looking for something simpler you write to <code>/mnt/volume/$HOSTNAME</code> from each Pod. This will also ensure that they are using separate files as the hostnames for the Pods are unique.</p>
