This hands-on lab will introduce students to Kubernetes storage concepts using a DigitalOcean Kubernetes (DOKS) cluster. Students will learn how to work with various Kubernetes storage methods, including emptyDir, hostPath, configMap, secret, and PVC.
- Installed and configured kubectl.
- basic understanding of Kubernetes concepts.
emptyDir
creates an ephemeral storage volume that exists as long as the Pod is running.
apiVersion: v1
kind: Pod
metadata:
name: emptydir-demo-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/tmp/data"
name: temp-storage
volumes:
- name: temp-storage
emptyDir: {}
Apply the Pod:
kubectl apply -f emptydir-pod.yaml
Check the volume:
kubectl exec -it emptydir-demo-pod -- ls /tmp/
hostPath
mounts a directory from the Kubernetes node into the Pod.
apiVersion: v1
kind: Pod
metadata:
name: hostpath-demo-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/host-data"
name: host-storage
volumes:
- name: host-storage
hostPath:
path: "/mnt/data"
type: DirectoryOrCreate
Apply the Pod:
kubectl apply -f hostpath-pod.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
config.json: |
{
"key": "value"
}
Apply the ConfigMap:
kubectl apply -f configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/etc/config"
name: config-volume
volumes:
- name: config-volume
configMap:
name: app-config
Apply the Pod:
kubectl apply -f configmap-pod.yaml
- Assignment Validate if the ConfigMap file has been correctly mounted by validating the file contentn
kubectl create secret generic my-secret --from-literal=password=supersecret
apiVersion: v1
kind: Pod
metadata:
name: secret-demo-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/etc/secret"
name: secret-volume
volumes:
- name: secret-volume
secret:
secretName: my-secret
Apply the Pod:
kubectl apply -f secret-pod.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: do-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: do-block-storage
Apply the PVC:
kubectl apply -f pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pvc-demo-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: storage-volume
volumes:
- name: storage-volume
persistentVolumeClaim:
claimName: do-pvc
Apply the Pod:
kubectl apply -f pvc-pod.yaml
To delete all resources:
kubectl delete pod emptydir-demo-pod hostpath-demo-pod configmap-demo-pod secret-demo-pod pvc-demo-pod
kubectl delete pvc do-pvc
kubectl delete secret my-secret
kubectl delete configmap app-config
kubectl delete sc do-block-storage-class
- Create a pod with two containers.
- Use
emptyDir
to share data between the containers. - One container writes a file, and the other container reads it.
- Create a PersistentVolume (PV) manually.
- Create a PersistentVolumeClaim (PVC) that binds to the PV.
- Deploy an NGINX Deployment that uses the PVC for storage.
- Create a ConfigMap and Secret.
- Deploy a pod that mounts the ConfigMap and Secret.
- Verify the contents inside the container.