Search code examples
dockerkubernetesstatefulset

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?


Solution

  • 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.