Kubernetes Series Part 5: Mastering Kubernetes Storage

Table of Contents

Kubernetes Series Part 5: Mastering Kubernetes Storage

In our previous post, we explored the fundamental concepts of Kubernetes, YAML, and Ingress. Now, let’s delve into another crucial aspect of Kubernetes: storage.

Understanding Persistent Storage in Kubernetes

Kubernetes, by default, manages ephemeral storage for Pods. This means that when a Pod is terminated, any data stored within it is lost. To overcome this limitation, Kubernetes offers Persistent Volumes (PVs) and Persistent Volume Claims (PVCs).

Persistent Volumes (PVs)

  • Represent physical storage on a node, such as a disk or cloud storage.
  • Can be dynamically provisioned or statically created.

Persistent Volume Claims (PVCs)

  • Request storage from a PV.
  • Can be bound to a specific PV or dynamically provisioned.

StatefulSets vs. Deployments: Managing Stateful Applications

  • Deployments: Manage stateless applications.
  • StatefulSets: Manage stateful applications that require persistent storage and unique network identities.

Hands-on Labs

Lab 1: Creating Persistent Volumes and Persistent Volume Claims

  • Create storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: standard
provisioner: k8s.io/minikube-hostpath

Apply the yaml:

kubectl apply -f storage-class.yaml
  • Create static-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: static-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard

Apply the yaml:

kubectl apply -f static-pvc.yaml
  • Check the PVC Status:
kubectl get pvc

Lab 2: Deploying a Stateful Application (Redis)

  • Create redis.yaml:
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      app: redis
  serviceName: "redis"
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:alpine
        ports:
        - containerPort: 6379
          name: redis
        volumeMounts:
        - name: redis-persistent-storage
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: redis-persistent-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "standard"
      resources:
        requests:
          storage: 1Gi

Apply the yaml:

kubectl apply -f redis.yaml
  • Check the StatefulSet and Pods:
kubectl get statefulset redis
kubectl get pods

Conclusion

By understanding and effectively utilizing PVs, PVCs, Storage Classes, and StatefulSets, you can ensure that your Kubernetes applications have the persistent storage they need to operate reliably and recover from failures.