Francisco Roa Prieto
Francisco Roa Prieto

Reputation: 41

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

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

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:

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!

Upvotes: 1

Views: 1929

Answers (1)

Fabrice Jammes
Fabrice Jammes

Reputation: 3205

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.

Upvotes: 2

Related Questions