<p>We have parallel jobs on EKS and we would like the jobs to write to <code>hostPath</code>.</p>
<p>We are using <code>subPathExpr</code> with environment variable as according to the <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment" rel="nofollow noreferrer">documentation</a>. However, after the run, the <code>hostPath</code> contains only the one folder probably due to racing condition from the parallel jobs and whichever job get hold of the <code>hostPath</code>.</p>
<p>We are on Kubernetes 1.17. Is <code>subPathExpr</code> meant for this use case of allowing parallel jobs to write to the same <code>hostPath</code>? What are other options to allow parallel jobs to write to host volume?</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: gatling-job
spec:
  ttlSecondsAfterFinished: 300 # delete after 5 minutes
  completions: 5
  parallelism: 5
  backoffLimit: 0
  template:
    spec:
      restartPolicy: &quot;Never&quot;
      containers:
        - name: gatling
          image: GATLING_IMAGE_NAME
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name     
          volumeMounts:
            - name: perftest-results
              mountPath: /opt/gatling/results
              subPathExpr: $(POD_NAME)
      volumes:
      - name: perftest-results
        hostPath:
          path: /data/perftest-results
</code></pre>

RichardKang
<p>Tested with a simple job template as below, and files were created in respective folder and worked as expected.</p>
<p>Will investigate the actual project. Closing for now.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: subpath-jobs
  labels:
    name: subpath-jobs
spec:
  completions: 5
  parallelism: 5
  backoffLimit: 0
  template:
    spec:
      restartPolicy: &quot;Never&quot;
      containers:
        - name: busybox
          image: busybox
          workingDir: /outputs
          command: [ &quot;touch&quot; ]
          args: [ &quot;a_file.txt&quot; ]
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name     
          volumeMounts:
            - name: job-output
              mountPath: /outputs
              subPathExpr: $(POD_NAME)
      volumes:
      - name: job-output
        hostPath:
          path: /data/outputs
          type: DirectoryOrCreate
</code></pre>
<pre class="lang-sh prettyprint-override"><code># ls -R /data
/data:
outputs

/data/outputs:
subpath-jobs-6968q  subpath-jobs-6zp4x  subpath-jobs-nhh96  subpath-jobs-tl8fx  subpath-jobs-w2h9f

/data/outputs/subpath-jobs-6968q:
a_file.txt

/data/outputs/subpath-jobs-6zp4x:
a_file.txt

/data/outputs/subpath-jobs-nhh96:
a_file.txt

/data/outputs/subpath-jobs-tl8fx:
a_file.txt

/data/outputs/subpath-jobs-w2h9f:
a_file.txt
</code></pre>
