<p>I have a stateful set with a volume that uses a subPath: $(POD_NAME) I've also tried $HOSTNAME which also doesn't work.  How does one set the subPath of a volumeMount to the name of the pod or the $HOSTNAME?</p>
<p>Here's what I have:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: ravendb
  namespace: pltfrmd
  labels:
    app: ravendb
spec:
  serviceName: ravendb
  template:
    metadata:
      labels:
        app: ravendb
    spec:
      containers:
        - command:
            # [&quot;/bin/sh&quot;, &quot;-ec&quot;, &quot;while :; do echo '.'; sleep 6 ; done&quot;]
            - /bin/sh
            - -c
            - /opt/RavenDB/Server/Raven.Server --log-to-console  --config-path /configuration/settings.json
          image: ravendb/ravendb:latest
          imagePullPolicy: Always
          name: ravendb
          env:
            - name: POD_HOST_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: RAVEN_Logs_Mode
              value: Information
          ports:
            - containerPort: 8080
              name: http-api
              protocol: TCP
            - containerPort: 38888
              name: tcp-server
              protocol: TCP
            - containerPort: 161
              name: snmp
              protocol: TCP
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /data
              name: data
              subPath: $(POD_NAME)
            - mountPath: /configuration
              name: configuration
              subPath: ravendb
            - mountPath: /certificates
              name: certificates
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 120
      volumes:
        - name: certificates
          secret:
            secretName: ravendb-certificate
        - name: configuration
          persistentVolumeClaim:
            claimName: configuration
        - name: data
          persistentVolumeClaim:
            claimName: ravendb
</code></pre>
<p>And the Persistent Volume:</p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: pltfrmd
  name: ravendb
  labels:
    type: local
spec:
  storageClassName: local-storage
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /volumes/ravendb
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: pltfrmd
  name: ravendb
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 30Gi
</code></pre>
<p>$HOSTNAME used to work, but doesn't anymore for some reason. Wondering if it's a bug in the host path storage provider?</p>

James Hancock
<p>Ok, so after great experimentation I found a way that still works:</p>
<p>Step one, map an environment variable:</p>
<pre><code>  env:
    - name: POD_HOST_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
</code></pre>
<p>This creates $(POD_HOST_NAME) based on the field metadata.name</p>
<p>Then in your mount you do this:</p>
<pre><code>  volumeMounts:
    - mountPath: /data
      name: data
      subPathExpr: $(POD_HOST_NAME)
</code></pre>
<p>It's important to use subPathExpr as subPath (which worked before) doesn't work.  Then it will use the environment variable you created and properly expand it.</p>
