Kubernetes: hostPath Static Storage with PV vs hard coded hostPath in Pod Volume

3/11/2021

I'm learning Kubernetes and there is something I don't get well. There are 3 ways of setting up static storage:

  • Pods with volumes you attach diretctly the storage to
  • Pods with a PVC attached to its volume
  • StatefulSets with also PVC inside

I can understand the power of PVC when working together with StorageClass, but not when working with static storage and local storage like hostPath

To me, it sounds very similar:

  • In the first case I have a volume directly attached to a pod.
  • In the second case I have a volume statically attached to a PVC, which is also manually attached to a Pod. In the end, the volume will be statically attached to the Pod.

On both cases, the data will remain when the Pod is terminates and will be adopted by the next Pod which the corresponing definition, right?

The only profit I see from using PVCs over plain Pod is that you can define the acces mode. Apart of that. Is there a difference when working with hostpath?

On the other hand, the advantage of using a StatefulSet instead of a PVC is (if understood properly) that it get a headless service, and that the rollout and rollback mechanism works differently. Is that the point?

Thank you in advance!

-- Francisco Roa Prieto
kubernetes

1 Answer

3/11/2021

Extracted from this blog:

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.

Using hostPath does not garantee that a pod will restart on the same node. So you pod can attach /tmp/storage on k8s-node-1, then if you delete and re-create the pod, it may attach tmp/storage on k8s-node-[2-n]

On the contrary, if you use PVC/PV with local persistent storage class, then if you delete and re-create a pod, it will stick on the node which handle the local persistent storage.

StatefulSet creates pods and has volumeClaimTemplate field, which creates a dedicated PVC for each pod. So each pod created by the statefulSet will have its own dedicated storage, linked with Pod->PVC->PV->Storage. So StatefulSet use also the PVC/PV mechanism. More details are available here.

-- Fabrice Jammes
Source: StackOverflow