Kubernetes Persistence Volume

Advanced Concepts — Stateful applications

This article is part 2 of https://amulyareddyk97.medium.com/advanced-kubernetes-with-doks-d5fdd829eed4. Please go through the link.

  1. Volumes and Claims
  2. Life cycle of volumes and claims
  3. Provisioning types
  4. Binding and Mounting volume
  5. Statefulsets
# Containers are not persistent
# PV is accessible at cluster level, independent of pods
# PVs life cycle is not tied to container lifecycle

Static PV

static-pv.yml
kubectl create -f static-pv.yml
kubectl get pv
kubectl get pv <name>

Life cycle of PV and PVC

# PV is at cluster level
# When developer claims PVC is created at container level
static-pvc.yml
# Though the dev requested for 3GB volume, if the access mode matches, bigger volume is allocated. 10GB here

How to make PV visible inside a container

# Mount PVC to a mount path of a pod
static-pv-pod.yml
kubectl create -f static-pv-pod.yml

Volume Binding — Phases

Available
Bound
Released
Fail

Reclaim Policy

Delete    # If pod is deleted PV gets deleted
Retain # Even if PVC & pods are deleted, PV remains. We need to # delete PV manually
Recycle
Reclaim

CSI (Container Storage Interface)

# Without CSI, different volume plugins has to be used to interact with different storages
# CSI is single interface for different external storage systems
# Supports secure 3rd party storages without being shipped with Kubernetes code
# They communicate over gRPC

In CSI, lets create Dynamic PV

csi-pvc.yml
kubectl create -f csi-pvc.yml
kubectl get pvc # gets created
kubectl get pv # gets created automatically
kubectl get pod
kubectl describe pod <name> # can see the volume

Other topics

  1. Protection
  2. Expanding of Volume — change the size
  3. Cloning of Volume

Deletion order

Static: Delete Pod (removes mounting) -> Delete PVC (removes binding)-> Delete PV

Dynamic: Does automatically. Deleting PVC deleted PV.

Volume Cloning

Create snapshot out of PVC

snapshot-csi.yml
# Check if the PVC exists
kubectl get volumesnapshot

Creating new PVC out of snapshot

snapshot-pvc.yml
kubectl get pvc      # They have same state, same config

Clone PVC — not snapshot (pvc to pvc)

cloned-pvc.yml

StatefulSets

# Stateful applications store data in StatefulSets (db data, kv pairs) for servers, clients to use
redis service
kubectl scale sts <pod_name> replica=5
kubectl get sts

ReplicaSet is the opposite of StatefulSet. It is good for stateless applications.

ReplicaSet, StatefulSet, DeamonSet and Job/CronJobs are workloads.

DeamonSet — defines Pods that provide node-local facilities.

Other Useful commands

kubectl get pod -w
kubectl describe pod <name>
kubectl get events

Software Engineer