<p>We have an application deployed on GKE that would benefit from having fast temporary storage on disk.</p>

<p>The GKE <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/local-ssd" rel="nofollow noreferrer">local SSD</a> feature is almost perfect, however we have multiple pod replicas and would ideally like to support multiple pods on the same node. Mounting the local SSD using <code>hostPath</code> makes that difficult.</p>

<p><a href="https://stackoverflow.com/questions/38720828/mount-local-ssd-drive-in-container">This 2016 SO question</a> mentions the idea of mounting <code>emptyDir</code>s on the local SSD which would be perfect, but I understand still isn't an option.</p>

<p>There is a <a href="https://groups.google.com/forum/#!topic/kubernetes-users/IVg6QasyxV0" rel="nofollow noreferrer">2017 mailing list thread</a> with the same idea, but the answer was still not positive.</p>

<p>The <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/local-ssd" rel="nofollow noreferrer">GCP docs for local SSDs</a> were recently updated to describe using them via the <code>PersistentVolume</code> abstraction, which sounds sort of promising. Could I use that to achieve what I'm after?</p>

<p>The examples seem to show mounting the full local SSD as a <code>PersistentVolume</code>, when my preference is to use an isolated part of it for each pod. We also don't need the data to be persistent - once the pod is deleted we'd be happy for the data to be deleted as well.</p>

James Healy
<p>Kubernetes 1.11 added an alpha feature called <a href="https://github.com/kubernetes/kubernetes/issues/48677" rel="nofollow noreferrer">Downward API support in volume subPath</a>, which allows volumeMount subpaths to be set using the downward API.</p>

<p>I tested this by creating a GKE 1.11 alpha cluster:</p>

<pre><code>gcloud container clusters create jh-test --enable-kubernetes-alpha
  --zone=asia-southeast1-a --cluster-version=1.11.3-gke.18
  --local-ssd-count=1 --machine-type=n1-standard-2 --num-nodes=2
  --image-type=cos --disk-type=pd-ssd --disk-size=20Gi
  --no-enable-basic-auth --no-issue-client-certificate
  --no-enable-autoupgrade --no-enable-autorepair
</code></pre>

<p>I then created a 2-replica deployment with the following config:</p>

<pre><code>      env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
      volumeMounts:
        - name: scratch-space
          mountPath: /tmp/scratch
          subPath: $(POD_NAME)
  volumes:
    - name: scratch-space
      hostPath:
        path: "/mnt/disks/ssd0"
</code></pre>

<p>If I <code>kubectl exec</code>'d into each pod, I had a <code>/tmp/scratch</code> directory that was isolated and very performant.</p>

<p>If I SSHd into the host, then I could see a directory for each pod:</p>

<pre><code>$ ls -l /mnt/disks/ssd0/
drwx--x--x 14 root root 4096 Dec  1 01:49 foo-6dc57cb589-nwbjw
drwx--x--x 14 root root 4096 Dec  1 01:50 foo-857656f4-dzzzl
</code></pre>

<p>I also tried applying the deployment to a non-alpha GKE 1.11 cluster, but the SSD content ended up looking like this:</p>

<pre><code>$ ls -l /mnt/disks/ssd0/
drwxr-xr-x 2 root root 4096 Dec  1 04:51 '$(POD_NAME)'
</code></pre>

<p>Unfortunately it's not realistic to run our workload on an alpha cluster, so this isn't a pragmatic solution for us yet. We'll have to wait for the feature to reach beta and become available on standard GKE clusters. It does seem to be <a href="https://github.com/kubernetes/kubernetes/issues/64604" rel="nofollow noreferrer">slowly progressing</a>, although the API <a href="https://github.com/kubernetes/community/pull/2871" rel="nofollow noreferrer">will probably change slightly</a>.</p>

<p>For kubernetes 1.14, the syntax for <code>volumeMounts</code> has changed to use a new <code>subPathExpr</code> field. The feature remains alpha-only:</p>

<pre><code>      env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
      volumeMounts:
        - name: scratch-space
          mountPath: /tmp/scratch
          subPathExpr: $(POD_NAME)
  volumes:
    - name: scratch-space
      hostPath:
        path: "/mnt/disks/ssd0"
</code></pre>
