In Kubernetes is it possible to add hostPath
storage in Statefulset. If so, can someone help me with some example?

- 4,861
- 11
- 59
- 73

- 177
- 1
- 11
2 Answers
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.

- 13,279
- 5
- 53
- 70
-
2Note that this will only work on single node clusters. If you try this in multi-node clusters you'll have issues with pods making claims to hostpath volumes that exist on entirely different nodes. – Matt Kereczman Feb 26 '20 at 16:53
-
The link for **hostpath dynamic provisioner** is broken. – Hamid Mayeli Dec 30 '21 at 11:28
-
No very surprising for an 4 years old answer The new repository URL is https://github.com/kubernetes-sigs/sig-storage-lib-external-provisioner/tree/master/examples/hostpath-provisioner – Jcs Mar 16 '22 at 09:35
-
@Matt Kereczman its possible to add spec.template.spec.nodeSelector to the deployment or StatefulSet to make sure its on a specific node, but you'll have to do the load balancing between nodes yourself, or assign one node to be the PVs' node. you can also use requiredDuringSchedulingIgnoredDuringExecution to achieve the same thing, if you want to expel other deployments from that node, i.e. make it only for PVs, its possible to use anti-affinity, all this is explained here: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/ – Azeer Esmail Mar 24 '22 at 14:40
A hostPath volume for StatefulSet
should only be used in a single-node cluster, e.g. for development. Rescheduling of the pod will not work.
Instead, consider using a Local Persistent Volume for this kind of use cases.
The biggest difference is that the Kubernetes scheduler understands which node a Local Persistent Volume belongs to. With HostPath volumes, a pod referencing a HostPath volume may be moved by the scheduler to a different node resulting in data loss. But with Local Persistent Volumes, the Kubernetes scheduler ensures that a pod using a Local Persistent Volume is always scheduled to the same node.
Consider using local static provisioner for this, the Getting Started guide has instructions for how to use it in different environments.

- 121,568
- 97
- 310
- 388