Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Hospath to a Kubernetes Statefulset

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

like image 842
Dinesh Avatar asked Feb 05 '23 02:02

Dinesh


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.

like image 81
Jcs Avatar answered Feb 11 '23 21:02

Jcs


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.

like image 40
Jonas Avatar answered Feb 11 '23 23:02

Jonas