<p>I have Java API which exports the data to an excel and generates a file on the POD where the request is served.
Now the next request (to download the file) might go to a different POD and the download fails.</p>

<p>How do I get around this?
How do I generate files on all the POD? Or how do I make sure the subsequent request goes to the same POD where file was generated?
I cant give the direct POD URL as it will not be accessible to clients.</p>

<p>Thanks.</p>

Nishith Shah
<p>You need to use a persistent volumes to share the same files between your containers. You could use the node storage mounted on containers (easiest way) or other distributed file system like NFS, EFS (AWS), GlusterFS etc...</p>

<p>If you you need a simplest to share the file and your pods are in the same node, you could use <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer">hostpath</a> to store the file and share the volume with other containers.</p>

<p>Assuming you have a kubernetes cluster that has only one Node, and you want to share the path <code>/mtn/data</code> of your node with your pods:</p>

<p><strong>Create a PersistentVolume:</strong></p>

<blockquote>
  <p>A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.</p>
</blockquote>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
</code></pre>

<p><strong>Create a PersistentVolumeClaim:</strong></p>

<blockquote>
  <p>Pods use PersistentVolumeClaims to request physical storage</p>
</blockquote>

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

<p>Look at the PersistentVolumeClaim:</p>

<p><code>kubectl get pvc task-pv-claim</code></p>

<p>The output shows that the PersistentVolumeClaim is bound to your PersistentVolume,  <code>task-pv-volume</code>.</p>

<pre><code>NAME            STATUS    VOLUME           CAPACITY   ACCESSMODES   STORAGECLASS   AGE
task-pv-claim   Bound     task-pv-volume   10Gi       RWO           manual         30s
</code></pre>

<p><strong>Create a deployment with 2 replicas for example:</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/mnt/data"
              name: task-pv-storage
</code></pre>

<p>Now you can check inside both container the path <code>/mnt/data</code> has the same files.</p>

<p>If you have cluster with more than 1 node I recommend you to think about the other types of <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">persistent volumes</a>.</p>

<p><strong>References:</strong>
<a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/" rel="nofollow noreferrer">Configure persistent volumes</a>
<a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">Persistent volumes</a>
<a href="https://kubernetes.io/docs/concepts/storage/volumes/#types-of-volumes" rel="nofollow noreferrer">Volume Types</a></p>
