Lab Exercises for Storage
Exercise 0 - Setup
- Prepare a cluster (Single node, kubeadm, k3s, etc)
- Open browser tabs to Kubernetes Documentation, Kubernetes GitHub and Kubernetes Blog (these are permitted as per the current guidelines)
Exercise 1 - Understand storage classes, persistent volumes
Note: Leverage the docs to help you. It's unlikely you will need to recall the nuances from memory - Kubernetes Storage Classes
- Create a
storageclass
object namedaws-ebs
with the following specification:- The
provisioner
should bekubernetes.io/aws-ebs
- The
type
isio1
- The
reclaimPolicy
isDelete
allowVolumeExpansion
set tofalse
- The
- Create a
persistentvolume
based on this storage class- Note, it will not initialize unless AWS integration is configured, but the objective is to get the YAML correct.
Answer
Apply the following YAML to create the Storage Class
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: aws-ebs
provisioner: kubernetes.io/aws-ebs
parameters:
type: io1
reclaimPolicy: Delete
allowVolumeExpansion: false
mountOptions:
- debug
volumeBindingMode: Immediate
Apply the following YAML to create a Persistent Volume based on the Storage Class
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-test
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: aws-ebs
awsElasticBlockStore:
fsType: "ext4"
volumeID: "vol-id"
validate with:
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-test 5Gi RWO Recycle Available aws-ebs 5
Exercise 2 - Know how to configure applications with persistent storage
- Create a
persistentVolume
object of typehostPath
with the following parameters:- 1GB Capacity
- Path on the host is /tmp
storageClassName
is ManualaccessModes
isReadWriteOnce
- Create a
persistentVolumeClaim
to the aforementionedpersistentVolume
- Create a
pod
workload to leverage this `persistentVolumeClaim
To create the StorageClass manual
apply the following:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: manual
provisioner: kubernetes.io/no-provisioner
Answer
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-hostpath-1gb
spec:
capacity:
storage: 1Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: manual
hostPath:
path: /tmp
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-hostpath-claim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 512Mi
storageClassName: manual
---
apiVersion: v1
kind: Pod
metadata:
name: pod-with-pvc
spec:
volumes:
- name: myvol
persistentVolumeClaim:
claimName: pvc-hostpath-claim
containers:
- name: busybox
image: busybox
args:
- sleep
- "1000000"
volumeMounts:
- mountPath: "/mnt/readonly"
name: myvol
Validate with:
kubectl get po,pv,pvc
NAME READY STATUS RESTARTS AGE
pod/pod-with-pvc 1/1 Running 0 2m7s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pv-hostpath-1gb 1Gi RWO Recycle Bound default/pvc-hostpath-claim manual 2m7s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-hostpath-claim Bound pv-hostpath-1gb 1Gi RWO manual 2m7s