<p>I have a use-case where I need to mount a <code>hostpath</code> to a pod but that <code>hostpath should have a subdirectory with PODNAME</code>, I know something like this can be used with statefulsets but due to some constraints a hostpath mount is needed in deployment where the root directory will have sub directories as <code>podnames</code>.</p>
<p>Can this be achieved?</p>

john mich
<p>You can use the <strong><a href="https://kubernetes.io/docs/concepts/workloads/pods/downward-api/" rel="nofollow noreferrer">downward API</a></strong> of Kubernetes as <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">Environment variables</a> :</p>
<pre><code>- name: pod_name
        valueFrom:
          fieldRef:
            apiVersion: v1
            fieldPath: metadata.name
</code></pre>
<p>Feel free to refer my GitHub YAML : <a href="https://github.com/harsh4870/OCI-public-logging-uma-agent/blob/main/deployment.yaml" rel="nofollow noreferrer">https://github.com/harsh4870/OCI-public-logging-uma-agent/blob/main/deployment.yaml</a></p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: app
    image: &lt;App image&gt;
    env:
      - name: pod_name
        valueFrom:
          fieldRef:
            apiVersion: v1
            fieldPath: metadata.name
    volumeMounts:
    - mountPath: /app_logs
      name: app-logs
      subPathExpr: $(pod_name)
  volumes:
  - name: mydir
    hostPath:
      path: /var/log/app_logs
      type: DirectoryOrCreate
</code></pre>
<p>Here is full article i have used <strong>downwardAPI</strong> for logging : <a href="https://medium.com/@harsh.manvar111/oke-logging-with-uma-agent-in-oci-d6f55a8bcc02" rel="nofollow noreferrer">https://medium.com/@harsh.manvar111/oke-logging-with-uma-agent-in-oci-d6f55a8bcc02</a></p>
