Adding Hospath to a Kubernetes Statefulset

1/8/2018

In Kubernetes is it possible add hostPath storage in Statefulset. If so Can someone help me with some example.

-- Dinesh
docker
kubernetes

1 Answer

1/8/2018

Yes but it is definitely for testing purposes.

First you need to create as many Persistent Volume as you need

kind: PersistentVolume
apiVersion: v1
metadata:
  name: hp-pv-001
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/data01"

kind: PersistentVolume
apiVersion: v1
metadata:
  name: hp-pv-002
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/data02"
...

Afterwards, add this VolumeClaimsTemplate to your Statefulset

volumeClaimTemplates:
- metadata:
    name: my-hostpath-volume
  spec:
    storageClassName: manual
    accessModes: ["ReadWriteOnce"]
    resources:
      requests:
        storage: 5Gi
    selector:
      matchLabels:
        type: local

Another solution is using the hostpath dynamic provisioner. You do not have to create the PV bin advance but this remains a "proof-of-concept solution" as well and you will have to build and deploy the provisioner in your cluster.

-- Jcs
Source: StackOverflow