The Kubernetes Volume is simply a directory on disk mapped to the pod that allows you to store and share data usually beyond the lifetime of a pod.
-
Create busyboxvol pod with two containers (c1 and c2), each one will have the image busybox and will run the 'sleep 3600' command. Make both containers mount an emptyDir at '/etc/foo'.
# review the manifest file - look how Volume and Volume mounts are performed cat ./assets/lab-03/state-emptyDir.yaml # Create Pod from the script file kubectl apply -f ./assets/lab-03/state-emptyDir.yaml
-
Connect to the first container
c1
, write current date time in the file/etc/foo/mydata.txt
kubectl exec -it busyboxvol -c c1 -- /bin/sh ls /etc/foo/ # confirm dir is empty echo $(date) > /etc/foo/mydata.txt cat /etc/foo/mydata.txt # confirm that stuff has been written successfully exit
Notice
/etc/foo/
directory has been mounted onto the container -
Connect to the second container
c2
and read/etc/foo/mydata.txt
file to standard output.kubectl exec -it busyboxvol -c c2 -- /bin/sh cat /etc/foo/mydata.txt exit
Notice that two containers within pod busyboxvol share the directory
-
List all the storage class available on your cluster
kubectl get sc
-
Create a PersistentVolumeClaim for azure storage class
default
, calledmypvc
, a request of 1Gi with an access mode of ReadWriteOnce.# review the manifest and look how PersistentVolumeClaim is written cat ./assets/lab-03/state-mypvc.yaml # create PersistentVolumeClaim from the script file kubectl apply -f ./assets/lab-03/state-mypvc.yaml
-
Show the PersistentVolumes and PersistentVolumeClaims of the cluster
# creation can take time, press ctrl+c to exit watch loop once pv and pvc are created kubectl get pv kubectl get pvc -w
-
Create a
nginxvol
pod running nginx image and Mount the PersistentVolumeClaim to '/etc/foo'.# review the manifest and look how Volume and Volume mounts are performed cat ./assets/lab-03/state-mount-pvc.yaml # Create Pod from the script file kubectl apply -f ./assets/lab-03/state-mount-pvc.yaml
-
Connect to the 'nginxvol' pod, and copy the '/etc/passwd' file to '/etc/foo'
kubectl exec nginxvol -it -- cp /etc/passwd /etc/foo/passwd kubectl exec nginxvol -it -- cat /etc/foo/passwd
-
Delete
nginxvol
podkubectl delete po nginxvol
-
Recreate
nginxvol
pod running nginx image and Mount the PersistentVolumeClaim to '/etc/foo'.# create Pod from the script file kubectl apply -f ./assets/lab-03/state-mount-pvc.yaml
-
Connect to the 'nginxvol' pod, and list all files in '/etc/foo'
kubectl exec nginxvol -it -- ls /etc/foo kubectl exec nginxvol -it -- cat /etc/foo/passwd
Notice files persisted, even after pod was deleted and recreated.
-
Create a configmap named myconfig with values foo=lala,foo2=lolo
kubectl create configmap myconfig --from-literal=foo=lala --from-literal=foo2=lolo
-
Create a secret called mysecret with the values password=mypass
kubectl create secret generic mysecret --from-literal=password=mypass
-
Create a new nginx pod that loads the value from configmap
myconfig
->foo
in an env variable called 'option'. Also load secret 'mysecret' as a volume inside an nginx pod on path/etc/secrets
.# review the manifest and look how the configMap and secret is referenced cat ./assets/lab-03/state-configs.yaml # Create Pod from the script file kubectl apply -f ./assets/lab-03/state-configs.yaml
-
Check environment variable
option
and/etc/secrets
has expected valueskubectl exec -it nginx -- env | grep option kubectl exec -it nginx -- ls /etc/secrets
-
Create CronJob called sleepycron that runs pod busy box with
sleep 5
command every minutekubectl create ns my-cron-job kubectl create cronjob sleepycron --image=busybox -n my-cron-job --schedule "*/1 * * * *" -- sleep 5
-
List all the CronJob, Jobs and Pods
kubectl get cj,job,pod # observe every minute job and pods will be created