<p>We have Kubernetes running on our own servers. For Persistent Storage we have a NFS server. This works great.</p>
<p>Now we want to deploy an application with multiple replicas that should have shared storage between them, but the storage should not be persistent. When the pods are deleted, the data should be gone as well.</p>
<p>I was hoping I could achieve it with the following</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: shared-data
        emptyDir: {}

      containers:
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        name: nginx
        volumeMounts:
        - name: shared-data
          mountPath: /shared-data
        resources:
          limits:
            memory: 500Mi
</code></pre>
<p>All replicas have /shared-data, but when 1 replica stores data in that folder, the other replica's cannot see the file, so it is not shared.</p>
<p>What are my options?</p>

C-nan
<p>You can use a PVC to share data between the pods. Then, you can setup a <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/" rel="nofollow noreferrer"><code>preStop</code> lifecycle hook</a>
for the pods to cleanup the data when the pod gets deleted.</p>
<p>Here, is an example of adding <code>preStop</code> hook on a pod: <a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/</a></p>
