Configuring Persistent Storage in Kubernetes

Tutorial 3 of 5

Configuring Persistent Storage in Kubernetes

Introduction

In this tutorial, we are going to learn how to configure persistent storage in Kubernetes.

By the end of this tutorial, you will be able to:
- Understand persistent storage and its importance in Kubernetes.
- Set up and configure persistent storage in Kubernetes.
- Understand how to choose the best method for persistent storage based on your requirements.

To follow this tutorial, you should have:
- Basic knowledge of Kubernetes and its components.
- A running Kubernetes cluster to practice the examples. You can use Minikube for a local setup or any cloud-based Kubernetes service.

Step-by-Step Guide

In Kubernetes, a volume can be thought of as a directory, possibly with some data in it, which is accessible to the Containers in a Pod. A Kubernetes volume lives as long as the Pod that encloses it is alive. Hence, a volume outlives any Containers that run within the Pod, and data is preserved across Container restarts.

Persistent Volumes

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes but have a lifecycle independent of any individual Pod that uses the PV.

Persistent Volume Claims

A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources.

Configuring Persistent Volumes and Persistent Volume Claims

Let's create a Persistent Volume and a Persistent Volume Claim.

  1. Create a file named pv.yaml and add the following content:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  hostPath:
    path: /tmp/data

This YAML file defines a PersistentVolume named my-pv with a storage capacity of 1Gi, which is accessible in ReadWriteOnce mode. The hostPath volume mounts the directory from the host node's filesystem.

  1. Create a file named pvc.yaml and add the following content:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: slow
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

This YAML file defines a PersistentVolumeClaim named my-pvc, which requests for 1Gi storage that is accessible in ReadWriteOnce mode.

  1. Apply the configuration using kubectl
kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml

This will create the PersistentVolume and PersistentVolumeClaim in your Kubernetes cluster.

Summary

In this tutorial, we have learned about:

  • The concept of Persistent Volumes (PV) and Persistent Volume Claims (PVC) in Kubernetes.
  • How to configure a Persistent Volume and a Persistent Volume Claim.

Next steps:
- Learn about StatefulSets in Kubernetes, which are intended to be used with stateful applications and distributed systems.
- Explore Dynamic Volume Provisioning in Kubernetes, which allows storage volumes to be created on-demand.

Additional resources:
- Kubernetes Documentation - Storage
- Kubernetes Documentation - Persistent Volumes

Practice Exercises

  1. Create a PersistentVolume with a storage capacity of 2Gi, which is accessible in ReadWriteMany mode.
  2. Create a PersistentVolumeClaim which requests for 2Gi storage that is accessible in ReadWriteMany mode.

Solutions:

  1. Here's an example of a PersistentVolume with a storage capacity of 2Gi, which is accessible in ReadWriteMany mode:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  hostPath:
    path: /tmp/data
  1. Here's an example of a PersistentVolumeClaim which requests for 2Gi storage that is accessible in ReadWriteMany mode:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: slow
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi

Remember, the storage class and access modes must match between the PV and PVC for a claim to be satisfied.