In the following lab we will create a Redis deployment and a PersistentVolumeClaim, the Redis deployment will use the generated PersistentVolume to store data that will survive pod restarts. Stateful workloads should utilize StatefulSets, however, this lab demonstrates that any workload can utilize a volume if necessary.
kubectl apply -f .
Now we can check that our PersistentVolumeClaim
is created:
kubectl get pvc --namespace doit-lab-14
And that the PVC created a Persistent Volume (using the Compute Engine persistent disk CSI Driver):
kubectl get pv
We'll wait for the Redis pod to come up
kubectl get po -w --namespace doit-lab-14
Once it's ready, let's run a shell inside it
kubectl -n doit-lab-14 exec -ti deployments/redis -- sh
List files within our data volume
ls -l /data
Let's check for the existence of a key in the DB
redis-cli get lab-14
We can see that it's not set (returning (nil)
). Let's set it
redis-cli set lab-14 'Hello world!'
Exit the pod (Ctrl+D) and delete it. The deployment controller will start a new one
kubectl delete po -lk8s-app=redis -n doit-lab-14
Now let's exec into the newly created pod
kubectl -n doit-lab-14 exec -ti deployments/redis -- sh
And make sure that data was persisted across restarts
/data # redis-cli get lab-14
"Hello world!"
kubectl delete ns doit-lab-14