[Kubernetes] Volumes

Volumes provide storage spaces for containers.

Concepts

Volumes

  • specify the details of where and how data can be stored
  • defined in the Pod spec section.

VolumeMounts

  • map (attache) the volume to a specific container with a path
  • defined in the container spec section

Volume Types

hostPath

  • Data is stored directly in a specific location on the host file system.
  • The path is on the node where the Pod is running.
  • Not a good choice in the multi-node cluster.
  • hostPath volume types:
    • Directory
    • DirectoryOrCreate
    • File
    • FileOrCreate

emptyDir

  • Data is stored on the host file system only while the Pod is running.
  • It is the temporary storage and will be deleted when the Pod is deleted.

persistentVolumeClaim

  • Data is stored using a PersistentVolume.

Tutorial – Using hostPath

  • In all nodes, create a path and a file
sudo mkdir /etc/mydir

echo "This is node {your node name}!" | sudo tee /etc/mydir/data.txt
  • Create a pod yaml file
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: host-path-test
  name: host-path-test
spec:
  containers:
  - image: busybox
    name: host-path-test
    command: ['sh', '-c', 'cat /mydata/data.txt']
    volumeMounts:
    - name: host-data
      mountPath: /mydata
  volumes:
  - name: host-data
    hostPath:
      path: /etc/mydir
      type: DirectoryOrCreate
  restartPolicy: Never
  • Create a pod and check the logs
kubectl apply -f pod.yaml

kubectl logs host-path-test

Tutorial – Using emptyDir

  • Create a pod yaml file
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: empty-dir-test
  name: empty-dir-test
spec:
  containers:
  - image: busybox
    name: empty-dir-test
    command: ['sh', '-c', 'echo "Hello, empty dir!" > /data/data.txt; cat /data/data.txt']
    volumeMounts:
    - name: empty-dir-vol
      mountPath: /data
  volumes:
  - name: empty-dir-vol
    emptyDir: {}
  restartPolicy: Never

  • Create a pod and check the logs
kubectl apply -f pod.yaml

kubectl logs empty-dir-test

PersistentVolumes

Using PersistentVolumes, you can decouple the volume details from Pods and treat storage like consumable resources.

PersistentVolume (PV)

  • PV defines details of storage – type, capacity, and access mode.
  • PV is an abstract component, and the actual physical storage must come from somewhere.
    • csi: Container Storage Interface (CSI) (for example, Amazon EFS, Amazon EBS, Amazon FSx, etc.)
      • The CSI is an abstraction designed to facilitate using different storage solutions with Kubernetes.
      • AWS has CSI plugins for Amazon EBS, Amazon EFS, and Amazon FSx for Lustre.
    • iscsi: iSCSI (SCSI over IP) storage
    • local: Local storage devices mounted on nodes
    • nfs: Network File System (NFS) storage

PV Access mode

  • ReadOnlyMany
    • allows read-only mode by many nodes at the same time
  • ReadWriteOnce
    • allows read/write by only one node at the same time
  • ReadWriteMany
    • allows read/write by multiple nodes at the same time

Reclaim Policy

You can set the “persistentVolumeReclaimPolicy” for the PV.

  • Retain (Default)
    • manual reclamation
  • Recycle
    • basic scrub (rm -rf /thevolume/*)
  • Delete
    • associated storage volume is deleted

PersistentVolumeClaim (PVC)

  • a request for the type of storage needed
  • PVC automatically binds to an available PV that meets the requirements.
  • You can mount a PVC to a container just like a regular volume.

Tutorial – using PV and PVC

  • Create a yaml file for a PV
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 20Mi
  accessModes:
  - ReadWriteOnce
  storageClassName: small-pv
  hostPath:
    path: /etc/mydir
    type: DirectoryOrCreate
  • Create a PV and check it
kubectl apply -f pv.yaml

kubectl get pv
  • Create a yaml file for a PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 20Mi
  storageClassName: small-pv
  • Create a PVC and check it
kubectl apply -f pvc.yaml

kubectl get pvc

You can see the claim is automatically bound.

  • Create a pod yaml file
apiVersion: v1
kind: Pod
metadata:
  name: pv-pvc-test
spec:
  containers:
  - image: busybox
    name: empty-dir-test
    command: ['sh', '-c', 'echo "Hello, Persisent Volume!" > /data/data.txt; cat /data/data.txt']
    volumeMounts:
    - name: pv-host-path
      mountPath: /data
  volumes:
  - name: pv-host-path
    persistentVolumeClaim:
      claimName: my-pvc
  restartPolicy: Never

Check the pod and logs

kubectl apply -f pod.yaml

kubectl get pods

kubectl logs pv-pvc-test

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s