<p>Could anyone clarify on the Persistent Volume in Kubernetes?</p>

<p>In this below example, the /my-test-project is in the persistent volume.Then, why I need these mounts as technically my entire directory /my-test-project is persisted? How these mountpath and subpath would help if entire directory is persisted.Thanks!</p>

<pre><code>volumeMounts:
    - name: empty-dir-volume
      mountPath: /my-test-project/data-cache
      subPath: data-cache
    - name: empty-dir-volume
      mountPath:  /my-test-project/user-cache
      subPath: user-cache

  volumes:
  - name: empty-dir-volume
    emptyDir: {}

</code></pre>

<p>Your <code>/my-test-project</code> entire directory is <strong>not persisted</strong>.</p>

<ul>
<li><p>mountPath or path in host <code>/my-test-project/data-cache</code> is persisted in <code>empty-dir-volume</code> in path <code>data-cache</code></p></li>
<li><p>mountPath <code>/my-test-project/user-cache</code> is persisted in <code>empty-dir-volume</code> in path <code>user-cache</code></p></li>
</ul>

<p>Which mean when you create files inside <code>/my-test-project/data-cache</code>, it will be persisted in <code>data-cache</code>(subpath) inside emtpy-dir-volume. Similarly for user-cache. Whenever you create files inside <code>/my-test-project/</code> it <strong>wont be persisted</strong>. Lets say you create <code>/my-test-project/new-dir</code>, now <code>new-dir</code> will not be persisted.</p>

<p>For better explaination, lets take the below example(two containers sharing the volume, but in <strong>different mounthPath</strong>):</p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: share-empty-dir
spec:
  containers:
  - name: container-1
    image: alpine
    command:
      - "bin/sh"
      - "-c"
      - "sleep 10000"
    volumeMounts:
    - name: empty-dir-volume
      mountPath: /my-test-project/data-cache
      subPath: data-cache-subpath
    - name: empty-dir-volume
      mountPath:  /my-test-project/user-cache
      subPath: user-cache-subpath
  - name: container-2
    image: alpine
    command:
      - "bin/sh"
      - "-c"
      - "sleep 10000"
    volumeMounts:
      - name: empty-dir-volume
        mountPath: /tmp/container-2
  volumes:
  - name: empty-dir-volume
    emptyDir: {}
</code></pre>

<p>In container-1:</p>

<ul>
<li>mountPath <code>/my-test-project/user-cache</code> is persisted in <code>empty-dir-volume</code> in path <code>user-cache-subpath</code></li>
<li>mountPath <code>/my-test-project/data-cache</code> is persisted in <code>empty-dir-volume</code> in path <code>data-cache-subpath</code></li>
</ul>

<p>In container-2: </p>

<ul>
<li>mountPath <code>/tmp/container-2</code> is persisted in <code>empty-dir-volume</code> in path "" (which means "/")</li>
</ul>

<p>Observations:</p>

<ul>
<li>touch <code>/my-test-project/user-cache/a.txt</code>. we can see this file in container-2 at <code>/tmp/container-2/user-cache-subpath/a.txt</code> and reverse will work</li>
<li>touch <code>/my-test-project/data-cache/b.txt</code>. we can see this file in container-2 at <code>/tmp/container-2/data-cache-subpath/a.txt</code> and reverse will work</li>
<li>touch <code>/tmp/container-2/new.txt</code>, we can never this file in container-1 as the base path we are have specified subPaths in container-1</li>
<li>Play around similarly for even better understanding</li>
</ul>

<p>Note: Just to be clear, you are using <code>emptyDir</code> type volume, which means whenever pod gets deleted, data will be lost. This type is used only to share the data between containers.</p>
