Kubernetes State Primitives

In this Cloud Native Short Take, industry expert Jerry Lozano explores Kubernetes state primitives and how Kubernetes provides persistent pod storage. Kubernetes offers multiple ways to achieve this, including creating a persistent volume and linking it directly to a specific pod. However, a more flexible approach is to use a Persistent Volume Claim (PVC) that maps to a Persistent Volume. Jerry demos how PVCs decouple the storage needs of a pod from the actual storage realization, one of the subjects covered in the Kubernetes State Primitives module from RX-M.

Video Transcript / Summary

Hello everyone my name is Jerry Lozano and i am an instructor for RX-M and this is our Cloud Native Short Take on Kubernetes state primitives: making data persistent in a Kubernetes pod. In this module our job is to explain how Kubernetes abstracts the notion of storage for pods.

To start it’s important to understand that Kubernetes has multiple ways to provide persistent storage for pods. There is a need to retain data independent of the lifetime of a pod. One way to do that is to create a Persistent Volume (PV) and have it linked directly to a specific pod. A more flexible way is to build an abstraction: have pods specify information about the kind of storage that they need and then to have this abstraction known as a Persistent Volume Claim (PVC) map to a specific Persistent Volume. The PVCs are a way to decouple the storage needs of a pod from the actual realization of the storage. That’s what we’ll end up looking at in this first demonstration.

We start by creating a Persistent Volume and I have already created a spec file for my volume. I’m going to create a Persistent Volume resource with the name myvol:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: myvol
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /tmp/mycontent

Notice its capacity will be 100 megabytes and it will be both readable and writeable but only by one pod at a time. Importantly this volume is realized the actual storage is coming from my systems /tmp/mycontent directory. I’ve already created this path on this host and placed 3 empty files in it called file1 file2 and file3.

Let’s create the volume using kubectl apply -f myvol.yaml. Separate from the volume we want to create a PVC. Here is my spec for the PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi

You can see that the type of resource being created is a Persistent Volume Claim with the name mypvc. Notice that the access mode read write once matches the access mode of the volume that i created. So at this point myvol is eligible to realize the Persistent Volume Claim. The other major match must occur on the size of the PVC with the actual Persistent Volume that we hope is used to realize the claim. So let’s separately create the claim. At this point if we use kubectl to get information about our Persistent Volumes and our Persistent Volume Claims we should see them both and most importantly we’re seeing that the Persistent Volume is bound to a specific claim the mypvc claim.

Now which pod at this point is using that volume? The answer is i have no pods; I’m going to create a pod whose name is persistentpod:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: persistentpod
  name: persistentpod
spec:
  containers:
  - image: nginx
    name: persistentpod
    volumeMounts:
    - mountPath: "/hostmount"
      name: persistence
  volumes:
  - name: persistence
    persistentVolumeClaim:
      claimName: mypvc

In the container spec we call out a volume mount. It’s based on a volume whose name is persistence. At the same level as the containers list there is a volumes: list and we call out a specific volume called persistence which is based on a PVC a Persistent Volume Claim with the specific name of mypvc. That’s the name of the PVC we previously created. Using kubectl apply -f mypod we create the pod. If we list our pods we will see that persistentpod running nginx is up and running and it has access to the Persistent Volume that we just mounted. 

Let’s see if we can access that directory we can use kubectl exec a command on the pod called persistentpod. The command that we wish to execute will be ls /hostmount. When the command completes we see the three files that are in /hostmount! 

Persistent Volume and claim creation are just some of the things you will learn in the Kubernetes Stateful Primitives module from RX-M. You can build your own custom Kubernetes course with the courseware builder right here on the RX-M website.

That’s our Cloud Native Short Take on the Kubernetes Stateful Primitives module!

Secret Link