kubernetes hostPath volume behaviour on a cluster with nore than one node

4/30/2020

If I have a deployment with several replicas using a persistent volume claim (pvc)/or persistent volume (pv)
My understanding is that all replicas will share the same pv. Is this first assertion true?

If behind the pvc we have an hostPath pv, my understanding is that it is not supported when number of cluster node > 1, as we can not sync the volume. Thus this would make first assertion false!
Could I still use hostPath taking the risk that if pods are deployed in different node by the scheduler, they will not have the same content?

To fix this, there is local persistent volume where we define an affinity between node where the volume is, and pod using the volume. So that scheduler always deploys the pod on the node (prevents in particular data loss).
What would happen in case we have several replicas, would all replicas be deployed on the same node?

-- scoulomb
docker
kubernetes

1 Answer

5/4/2020

My understanding is that all replicas will share the same pv.

This is correct. When creating HostPath PV or local storage PV all replicas will run as expected. But with kubernetes you can dynamically create a PersistentVolume (using StorageClasses). The problem with the second approach is that a volume will get attached to only one pod and the rest of your replicas may throw following error:

Multi-Attach error for volume "pvc-XXXX" Volume is already used by pod(s) YYYY

(I say may because it depends on type of volume).

If behind the pvc we have an hostPath pv, my understanding is that it is not supported when number of cluster node > 1, as we can not sync the volume.

I am not sure you understand the idea behind hostPath volumes. HostPath volumes are used to access data on hosts. Its considered bad practise to use hostpath to store application data. HostPath is usually only used with daemonsets and very application-specific workloads.

To answer the sync part; You are right, you cannot sync hostPaths unless its pointing to NFS mounted stoarage or similar.

To fix this, there is local persistent volume where we define an affinity between node where the volume is, and pod using the volume. So that scheduler always deploys the pod on the node (prevents in particular data loss). What would happen in case we have several replicas, would all replicas be deployed on the same node?

Yes, all replicas would be deployed on the same node. This is not the best option because when the specific node goes down, all your application replicas go down with him and the application can no longer be called highly available.

-- HelloWorld
Source: StackOverflow