Why should I use Kubernetes Persistent Volumes instead of Volumes

5/25/2021

To use storage inside Kubernetes PODs I can use volumes and persistent volumes. While the volumes like emptyDir are ephemeral, I could use hostPath and many other cloud based volume plugins which would provide a persistent solution in volumes itself.

In that case why should I be using Persistent Volume then?

-- Aditya Bhuyan
kubernetes
persistent-volumes
volumes

3 Answers

5/25/2021

PersistentVoluemes is cluster-wide storage and allows you to manage the storage more centrally.

When you configure a volume (either using hostPath or any of the cloud-based volume plugins) then you need to do this configuration within the POD definition file. Every configuration information, required to configure storage for the volume, goes within the POD definition file.

When you have a large environment with a lot of users and a large number of PODs then users will have to configure storage every time for each POD they deploy. Whatever storage solution is used, the user who deploys the POD will have to configure that storage on all of his/her POD definition files. If a change needs to be made then the user will have to make this change on all of his/her PODs. After a certain scale, this is not the most optimal way to manage storage.

Instead, you would like to manage this centrally. You would like to manage the storage in such a way that an Administrator can create a large pool of storage and users can carve out a part of this storage as required, and this is exactly what you can do using PersistentVolumes and PersistentVolumeClaims.

-- Ravi Kant Asthana
Source: StackOverflow

5/25/2021

Use PersistentVolumes when you need to set up a database like MongoDB, Redis, Postgres & MySQL. Because it's long-term storage and not deeply coupled with your pods! Perfect for database applications. Because they will not die with the pods.

Avoid Volumes when you need long-term storage. Because they will die with the pods!

In my case, when I have to store something, I will always go for persistent volumes!

-- Md Fazlul Karim
Source: StackOverflow

5/25/2021

It is very important to understand the main differences between Volumes and PersistentVolumes. Both Volumes and PersistentVolumes are Kubernetes resources which provides an abstraction of a data storage facility.

  • Volumes: let your pod write to a filesystem that exists as long as the pod exists. They also let you share data between containers in the same pod but data in that volume will be destroyed when the pod is restarted. Volume decouples the storage from the Container. Its lifecycle is coupled to a pod.

  • PersistentVolumes: serves as a long-term storage in your Kubernetes cluster. They exist beyond containers, pods, and nodes. A pod uses a persistent volume claim to to get read and write access to the persistent volume. PersistentVolume decouples the storage from the Pod. Its lifecycle is independent. It enables safe pod restarts and sharing data between pods.

When it comes to hostPath:

A hostPath volume mounts a file or directory from the host node's filesystem into your Pod.

hostPath has its usage scenarios but in general it might not recommended due to several reasons:

  • Pods with identical configuration (such as created from a PodTemplate) may behave differently on different nodes due to different files on the nodes

  • The files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a hostPath volume

  • You don't always directly control which node your pods will run on, so you're not guaranteed that the pod will actually be scheduled on the node that has the data volume.

  • If a node goes down you need the pod to be scheduled on other node where your locally provisioned volume will not be available.

The hostPath would be good if for example you would like to use it for log collector running in a DaemonSet.

I recommend the Kubernetes Volumes Guide as a nice supplement to this topic.

-- WytrzymaƂy Wiktor
Source: StackOverflow