Managing storage is a challenge for most organizations. Kubernetes allows you to create persistent storage so the data is not lost when a pod is moved or deleted. In this portion of the practice exam, you will demonstrate your knowledge of `PersistentVolumes` and `PersistentVolumeClaims` in order to ensure data integrity. This will simulate a question you may see on the Certified Kubernetes Administrator (CKA) exam.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create the `PersistentVolumeClaim`.
Use the following command to check if there is already a ‘web’ namespace:
kubectl get ns kubectl get pv
Use the following command to create a
PersistentVolumeClaim
work from scratch or use a Kubernetes template file. Search the documentation :vi data-pvc.yaml #copy and paste from docs
Update the file
data-pvc.yaml
with the following details:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: data-pvc namespace: web spec: storageClassName: local-storage accessModes: - ReadWriteOnce resources: requests: storage: 256Mi
- Use the following command to create the PVC:
kubectl apply -f data-pvc.yaml
- Create a pod mounting the volume and write data to the volume.
- Use the following command to create the YAML for a busybox pod:
kubectl run data-pod --image=busybox:1.28 --restart=Never -o yaml --dry-run -- /bin/sh -c 'sleep 3600' > data-pod.yaml
- Use the following command to edit the
data-pod.yaml
file:
vi data-pod.yaml
- Update the
data-pod.yaml
file as below:
apiVersion: v1 kind: Pod metadata: name: data-pod namespace: web spec: containers: - args: - /bin/sh - -c - sleep 3600 image: busybox:1.28 name: data-pod resources: {} volumeMounts: - name: temp-data mountPath: /tmp/data dnsPolicy: ClusterFirst restartPolicy: Never volumes: - name: temp-data persistentVolumeClaim: claimName: data-pvc
- Use the following command to create the pod:
kubectl apply -f data-pod.yaml
- Use the following command to connect to the pod:
kubectl exec -it data-pod -n web -- sh
- Use the following commands to confirm the environmant and to copy the contents of the
etc/passwd
file to/tmp/data
:
# ls -la /etc/passwd # ls -la /tmp/data/ # cp /etc/passwd /tmp/data/passwd
- Use the following command to list the contents of
/tmp/data
:
# ls /tmp/data/
- Delete the pod and create a new pod and view volume data.
Use the following command to delete the pod:
kubectl delete po data-pod -n web
Use the following command to modify the pod YAML:
vi data-pod.yaml
Change the following line in the
data-pod.yaml
file:name: data-pod2
Use the following command to create a new pod:
kubectl apply -f data-pod.yaml
Use the following command to connect to the pod and view the contents of the volume:
kubectl exec -it data-pod2 -n web -- sh # ls /tmp/data