Kubernetes Tutorial: Part 4 — The Crux
By: Parthasarathy Subburaj
Tags: Streamlit, Docker, Object Detection, Kubernetes, Deployment

Persistent Volumes

Managing the storage systems becomes a very crucial part in deploying any application. In Kubernetes, PersistentVolumes (PV) provides an interface for users and cluster administrators for managing storage by abstracting the details of how storage is provided and consumed in the cluster.

A PersistentVolume is a piece of storage in the cluster that is provisioned by the cluster administrator; they can just be seen as any other resource type like Pod, Namespace, Secrets etc. The key point to note here is that the lifecycle of a PV is independent of any individual Pod that uses it, i.e. even if the Pod using the PV crashes and is deleted, the volume is still alive, and all it’s contents are preserved, hence it is Persistent.

Any user in the cluster can get access to this volume by making a claim via a PersistentVolumeClaim (PVC) object. A user can request specific levels of memory that needs to be attached to the Pod that is being spun, and the same is allocated provided that the request is valid and is available in the cluster.

To deploy our application, let’s first create a PVC in the cluster for storing the weights of the model and later link the same to our container during runtime. Again, a PVC can be created by using a YAML file, like the one below:

YAML file for PVC creation

followed by executing:

kubectl create -f pvc.yaml

This creates a PVC named object-detection-model-weights with storage of 1 GB with read-write access to multiple nodes as specified in the accessModes of the spec section.

Now, let's spin a Pod and attach the PVC to it, and copy our model weights into the PVC. We can use the same Pod definition file that we used in the last article with a little bit of modification for mounting the PVC into it. The modified YAML file looks like this:

YAML file for Pod creation with a PVC

And for spinning the pod, we need to execute:

kubectl create -f pod-pvc.yaml

This creates a Pod named ‘ubuntu-base’ with the base image ‘ubuntu:18.04’ and mounts the PVC ‘object-detection-model-weights’ that we created in the last step, and the same is available in the directory /workspace within our Pod.

Now let’s download the model weights that we will be using in our deployment. Model weights can be downloaded from here:

Once downloaded, we can move these weights into our PVC that is attached to our running Pod ‘ubuntu-base’ by following the below steps:

Step 1: Creating the directory structure inside the PVC for us to copy the weights; this can be done by executing:

kubectl exec -it pod/ubuntu-base -n partha -- mkdir -p /workspace/model-weights/ssd

which creates the directories /workspace/model-weights/ssd in our mounted PVC; and now let’s create a provision for storing YOLO weights

kubectl exec -it pod/ubuntu-base -n partha -- mkdir -p /workspace/model-weights/yolo

Step 2: Copy the model weights from your local machine into the PVC by the executing:

kubectl cp <path to SSD weights> ubuntu-base:/workspace/model-weights/ssd -c ubuntu-base-container -n partha kubectl cp <path to YOLO weights> ubuntu-base:/workspace/model-weights/yolo -c ubuntu-base-container -n partha

Once copied, we no longer need the Pod, and the same can be deleted by executing:

kubectl delete pod/ubuntu-base -n partha

And this concludes our section on Persistent Volumes.