DEV Community

Julian-Chu
Julian-Chu

Posted on

[k8s] Life cycle of volumes

So many types of volumes supported in k8s, when learning the volume concept of k8s, they made me confused, how to choose correct one.

Types of Volumes in k8s

Let's try to make it easier by understanding the life cycle of different volumes.

We can sort different types into 3 kinds of life cycle

  • life cycle follows Pod
  • life cycle follows Node
  • Independent life cycle

Life cycle follows Pod

emptyDir
This kind of volume will be created when Pod is up. Wenn pod destroyed, of course the volume is removed. One example is emptyDir. Some use cases are to share files or information between container.

Life cycle follows Node

Alt Text
hostPath is the typical example. It mounts a file or directory from host node into pod. When pods are destroyed, it will remain. But if node is unhealth, the volume is down as well. Use cases like using daemonset to load log files from other pods or sharing configuration files cross pods on the same node.

Independent Life cycle

Alt Text
For the last kind of volume, we need to consider which infrastructure we use, because the actual storage is independent from k8s cluster. It means, when node or cluster is done, the data is still persisted in actual storage. If you have any data to persist, please choose this based on your infrastructure, for example, database.

pv and pvc

There're two ways to use this. One is refer to persistent volume(pv) directly, and another is to use persistent volume claim(pvc) to bind with persistent volume.

Why do we need pvc? Because pvc provides loose couple between persistent volume and pod. The developer and architect don't need to consider the concrete infrastructure too early. For more details, please see the document of k8s.

How to choose

  • persistent data?
  • share data cross containers, pods, or nodes?
  • which infrastructure your product used ex: AWS/Azure/GCP/on-premise

Final

Hope this article could help someone who is also confused with k8s volume. If any mistake or discussion, please feel free to leave your comment.

Top comments (0)