Kubenetes Volumes - neerajk555/Kubernetes GitHub Wiki
Kubernetes Storage Deep Dive Lab
This lab covers:
- Volumes (emptyDir)
- Persistent Volumes (PV)
- Persistent Volume Claims (PVC)
- StorageClass with dynamic provisioning
1. Prerequisites
- Kubernetes cluster (Minikube or any cloud provider)
kubectl
installed and configured
For local testing, start Minikube:
minikube start
2. Volumes Lab (EmptyDir)
Concept:
emptyDir
is a simple volume that exists as long as the pod runs.- Data is deleted once the pod is removed.
pod-emptydir.yaml
YAML: apiVersion: v1
kind: Pod
metadata:
name: emptydir-demo
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh", "-c", "echo Hello > /data/hello.txt && sleep 3600"]
volumeMounts:
- mountPath: /data
name: data-volume
volumes:
- name: data-volume
emptyDir: {}
Execution Steps
kubectl apply -f pod-emptydir.yaml
kubectl get pods
kubectl exec -it emptydir-demo -- cat /data/hello.txt
Expected Output:
Hello
Cleanup
kubectl delete pod emptydir-demo
3. Persistent Volumes (PV) and Persistent Volume Claims (PVC)
Concept:
- PV: A storage resource in the cluster.
- PVC: A userโs request for storage.
We will:
- Create a 1Gi PV
- Create a 1Gi PVC
- Bind a pod to the PVC
pv-pvc.yaml
YAML: ---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-demo
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /tmp/pv-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Execution Steps
kubectl apply -f pv-pvc.yaml
kubectl get pv
kubectl get pvc
- PV status โ Bound
- PVC status โ Bound
4. Pod Using PVC
pod-pvc.yaml
YAML: apiVersion: v1
kind: Pod
metadata:
name: pvc-pod
spec:
containers:
- name: app
image: busybox
command: ["/bin/sh", "-c", "echo PV Test > /data/test.txt && sleep 3600"]
volumeMounts:
- mountPath: /data
name: pvc-storage
volumes:
- name: pvc-storage
persistentVolumeClaim:
claimName: pvc-demo
Execution Steps
kubectl apply -f pod-pvc.yaml
kubectl get pods
kubectl exec -it pvc-pod -- cat /data/test.txt
Expected Output:
PV Test
Cleanup
kubectl delete pod pvc-pod
kubectl delete pvc pvc-demo
kubectl delete pv pv-demo
5. StorageClass with Dynamic Provisioning
Concept:
- StorageClass allows dynamic PV provisioning without creating PV manually.
- PVCs referencing the SC are automatically fulfilled.
For Minikube: Use
standard
StorageClass (already available).
For learning, we will create a local StorageClass withno-provisioner
.
storageclass.yaml
YAML: apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: sc-demo
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
pvc-sc.yaml
PVC Using StorageClass: apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-sc-demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
storageClassName: sc-demo
pod-sc.yaml
Pod Using SC PVC: apiVersion: v1
kind: Pod
metadata:
name: sc-pod
spec:
containers:
- name: app
image: busybox
command: ["/bin/sh", "-c", "echo SC Test > /data/sc.txt && sleep 3600"]
volumeMounts:
- mountPath: /data
name: sc-storage
volumes:
- name: sc-storage
persistentVolumeClaim:
claimName: pvc-sc-demo
Execution Steps
kubectl apply -f storageclass.yaml
kubectl apply -f pvc-sc.yaml
kubectl get pvc
kubectl apply -f pod-sc.yaml
kubectl get pods
kubectl exec -it sc-pod -- cat /data/sc.txt
Expected Output:
SC Test
Cleanup
kubectl delete pod sc-pod
kubectl delete pvc pvc-sc-demo
kubectl delete sc sc-demo
6. Verify Cleanup
kubectl get all
kubectl get pv
kubectl get pvc
kubectl get sc
Lab Completed!
You have now practiced:
- Volumes (
emptyDir
) - Persistent Volumes (PV)
- Persistent Volume Claims (PVC)
- StorageClass with dynamic provisioning