<p>I have a StatefulSet that starts a MYSQL cluster. The only downside at it for the moment is that for every replica I need to create a Persistent Volume and a Persistent Volume Claim with a select that matches label and podindex.
This means I cannot dynamically add replicas whithout manual interaction.</p>

<p>For this reason I'm searching for a soluction that gives me the option to have only 1 Volume and 1 Claim. And during the pod creation it knows his own pod name for the subPath during mount. (initContainer would be used to check and create the directories on the volume before the application container starts).</p>

<p>So I search a correct way for a code like:</p>

<pre><code>volumeMounts:
- name: mysql-datadir
  mountPath: /var/lib/mysql
  subPath: "${PODNAME}/datadir"
</code></pre>

Yannick De Backer
<p>You can get <code>POD_NAME</code> from the metadata ( the downward API ) by setting ENV var:</p>

<pre><code>  env:
   - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
</code></pre>

<p>But, you you cannot use <code>ENV</code> vars in volumes declarations (as far as i know...). So, everything else could be reached via workarounds. One of the workarounds is described <a href="https://github.com/kubernetes/kubernetes/issues/48677#issuecomment-347452457" rel="nofollow noreferrer">here</a></p>
