PART 6: HANDS-ON LABS
Lab 1: Create a PV and PVC
Goal: Understand static provisioning.
bash
# Create a PV
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: manual-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: manual
hostPath:
path: /mnt/manual-pv
EOF
# Create a PVC
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: manual-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: manual
EOF
# Check binding
kubectl get pv,pvc
kubectl describe pvc manual-pvc
# Create a Pod using the PVC
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: pv-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "echo 'Hello from PV' > /data/test.txt && sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: manual-pvc
EOF
sleep 5
kubectl exec pv-pod -- cat /data/test.txt
# Cleanup
kubectl delete pod pv-pod
kubectl delete pvc manual-pvc
kubectl delete pv manual-pv
Lab 2: Dynamic Provisioning with StorageClass
Goal: Provision storage automatically.
bash
# Create a StorageClass
cat <<'EOF' | kubectl apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: dynamic-sc
provisioner: rancher.io/local-path
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
EOF
# Create a PVC
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: dynamic-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: dynamic-sc
EOF
# Check (may be Pending until Pod uses it)
kubectl get pvc dynamic-pvc
# Create a Pod
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: dynamic-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: dynamic-pvc
EOF
sleep 10
kubectl get pvc dynamic-pvc
# Should now be Bound
# Cleanup
kubectl delete pod dynamic-pod
kubectl delete pvc dynamic-pvc
kubectl delete storageclass dynamic-sc
Lab 3: Access Modes
Goal: Understand RWO vs RWX.
bash
# Create a StorageClass that supports RWX (e.g., NFS, EFS)
# For local testing, we'll simulate with hostPath and RWO
# Create a PVC with RWO
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: rwo-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: dynamic-sc
EOF
# Create a Pod using it
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: rwo-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: rwo-pvc
EOF
# Try to create a second Pod using the same RWO PVC (may fail on multi-node)
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: rwo-pod-2
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: rwo-pvc
EOF
sleep 10
kubectl get pods
# On a single-node cluster, both may work. On multi-node, RWO restricts.
# Cleanup
kubectl delete pod rwo-pod rwo-pod-2 --ignore-not-found
kubectl delete pvc rwo-pvc
Lab 4: Volume Snapshots and Restore
Goal: Create and restore from a snapshot.
Prerequisites: Snapshot CRDs and controller installed.
bash
# Install snapshot CRDs
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml
# Create a PVC and Pod
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: snap-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: dynamic-sc
---
apiVersion: v1
kind: Pod
metadata:
name: snap-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "echo 'Original data' > /data/file.txt && sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: snap-pvc
EOF
sleep 10
kubectl exec snap-pod -- cat /data/file.txt
# Create a VolumeSnapshotClass
cat <<'EOF' | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: csi-snapclass
driver: hostpath.csi.k8s.io
deletionPolicy: Delete
EOF
# Create a snapshot
cat <<'EOF' | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: snap-1
spec:
volumeSnapshotClassName: csi-snapclass
source:
persistentVolumeClaimName: snap-pvc
EOF
sleep 10
kubectl get volumesnapshot
# Modify original data
kubectl exec snap-pod -- sh -c "echo 'Modified data' > /data/file.txt"
# Restore from snapshot
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restored-pvc
spec:
dataSource:
name: snap-1
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: dynamic-sc
EOF
# Verify
sleep 10
kubectl get pvc restored-pvc
# Cleanup
kubectl delete pod snap-pod
kubectl delete pvc snap-pvc restored-pvc
kubectl delete volumesnapshot snap-1
kubectl delete volumesnapshotclass csi-snapclass
Lab 5: Online Volume Expansion
Goal: Expand a PVC without downtime.
bash
# Create a PVC with a StorageClass that allows expansion
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: expand-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: dynamic-sc
EOF
# Create a Pod
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: expand-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: expand-pvc
EOF
sleep 10
kubectl get pvc expand-pvc
# Expand (online)
kubectl patch pvc expand-pvc -p '{"spec":{"resources":{"requests":{"storage":"2Gi"}}}}'
# Monitor
kubectl get pvc expand-pvc -w
# Cleanup
kubectl delete pod expand-pod
kubectl delete pvc expand-pvc
Lab 6: Ephemeral Volumes
Goal: Use emptyDir and generic ephemeral volumes.
bash
# emptyDir
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: empty-dir-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "echo 'scratch data' > /cache/test.txt && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir:
sizeLimit: 1Gi
EOF
sleep 5
kubectl exec empty-dir-pod -- cat /cache/test.txt
# Generic ephemeral volume
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: generic-ephemeral-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "echo 'ephemeral data' > /data/test.txt && sleep 3600"]
volumeMounts:
- name: scratch
mountPath: /data
volumes:
- name: scratch
ephemeral:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: dynamic-sc
resources:
requests:
storage: 1Gi
EOF
sleep 10
kubectl exec generic-ephemeral-pod -- cat /data/test.txt
# Cleanup
kubectl delete pod empty-dir-pod generic-ephemeral-pod
Lab 7: Local Persistent Volumes
Goal: Use node-local storage.
bash
# Create a local PV (adjust node name)
NODE=$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}')
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /mnt/local-data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- $NODE
EOF
# Create a PVC
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: local-storage
EOF
kubectl get pv,pvc
# Create a Pod
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: local-pod
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "echo 'local data' > /data/test.txt && sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: local-pvc
EOF
sleep 5
kubectl exec local-pod -- cat /data/test.txt
# Cleanup
kubectl delete pod local-pod
kubectl delete pvc local-pvc
kubectl delete pv local-pv
Lab 8: Velero Backup and Restore
Goal: Back up and restore a namespace.
Prerequisites: Velero installed with a storage backend (S3, MinIO).
bash
# Install Velero (example with MinIO)
velero install \
--provider aws \
--plugins velero/velero-plugin-for-aws:v1.9.0 \
--bucket velero \
--secret-file ./credentials-velero \
--use-volume-snapshots=false \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://minio.velero.svc:9000
# Create a namespace with resources
kubectl create namespace backup-test
kubectl create deployment nginx --image=nginx -n backup-test
kubectl expose deployment nginx --port=80 -n backup-test
# Create a backup
velero backup create test-backup --include-namespaces backup-test
# Check backup
velero backup describe test-backup
velero backup logs test-backup
# Delete the namespace
kubectl delete namespace backup-test
# Restore
velero restore create --from-backup test-backup
# Verify
kubectl get all -n backup-test
# Cleanup
kubectl delete namespace backup-test
velero backup delete test-backup
Lab 9: Complete Stateful Application
Goal: Deploy a StatefulSet with PVCs, snapshots, and backup.
bash
kubectl create namespace stateful-lab
# Headless Service
cat <<'EOF' | kubectl apply -n stateful-lab -f -
apiVersion: v1
kind: Service
metadata:
name: db-headless
spec:
clusterIP: None
selector:
app: db
ports:
- port: 5432
name: postgres
EOF
# StatefulSet with PVC template
cat <<'EOF' | kubectl apply -n stateful-lab -f -
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db-headless
replicas: 3
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: postgres
image: postgres:16-alpine
env:
- name: POSTGRES_PASSWORD
value: secret
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
ports:
- containerPort: 5432
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: dynamic-sc
resources:
requests:
storage: 1Gi
EOF
# Wait for pods
kubectl wait -n stateful-lab --for=condition=Ready pod -l app=db --timeout=180s
# Check PVCs
kubectl get pvc -n stateful-lab
# Write data to db-0
kubectl exec -n stateful-lab db-0 -- psql -U postgres -c "CREATE TABLE test (id int); INSERT INTO test VALUES (1);"
# Verify
kubectl exec -n stateful-lab db-0 -- psql -U postgres -c "SELECT * FROM test;"
# Cleanup
kubectl delete namespace stateful-lab
kubectl delete pvc -n stateful-lab --all --ignore-not-found
1:1 copy of the class Google Doc, code in code blocks.