VolumeClaimTemplates with subPath

5/18/2018

In a Kubernetes cluster, I need to have a StatefulSet bound to a volume (volume 0 in the picture) and for each replica, it should claim on a subPath folder (replica1 on folder node0, replica 2 on folder node1, ...).

enter image description here

I could not do such a thing. Any suggestion?

I thought it was possible to instruct the statefulset to use a subPath. Like this example:

[statefulset.yml]

  ...
  volumeClaimTemplates:
  - metadata:
      name: my-claim
    spec:
      resources:
        requests:
          storage: 1Gi
      accessModes:
      - ReadWriteMany
      storageClassName: volume-0
      subPath: node

Did someone have such a use case?

-- Nicola Ben
kubernetes

1 Answer

6/27/2018

You should use subPath in the VolumeMounts clause.

For example:

apiVersion: v1
kind: Pod
metadata:
  name: my-lamp-site
spec:
    containers:
    - name: apachephp
      image: php:7.0-apache
      volumeMounts:
      - mountPath: /var/www/html
        name: apachephp-data
        subPath: html
      - mountPath: /var/www/upload
        name: apachephp-data
        subPath: upload
    volumes:
    - name: apachephp-data
      persistentVolumeClaim:
        claimName: apachephp-claim-data

The volumeMounts.subPath property can be used to specify a sub-path inside the referenced volume instead of its root. To be clear, I suggest to use subPath clause to sharing one volume for multiple Pods, or even for multiple uses inside a single Pods .

-- freedev
Source: StackOverflow