Custom storage classes in kubernetes

Storage class
A StorageClass provides a way for administrators to describe the “classes” of storage they offer. Different classes might map to quality-of-service levels, or backup policies, or arbitrary policies determined by the cluster administrators. Kubernetes itself is unopinionated about what classes represent. This concept is sometimes called “profiles” in other storage systems

The StorageClass Resource
Each StorageClass contains the fields provisioner, parameters, and reclaim policy, which is used when a PersistentVolume belonging to the class needs to be dynamically provisioned. The name of a StorageClass object is significant and is how users can request a particular class. Administrators set the name and other parameters of a class when first creating StorageClass objects, and the objects cannot be updated once they are created.

Provisioner
Storage classes have a provisioner that determines what volume plugin is used for provisioning PVs. This field must be specified.
You are not restricted to specifying the “internal” provisioners listed here (whose names are prefixed with “kubernetes.io” and shipped alongside Kubernetes). You can also run and specify external provisioners, which are independent programs that follow a specification defined by Kubernetes. Authors of external provisioners have full discretion over where their code lives, how the provisioner is shipped, how it needs to be run, what volume plugin it uses (including Flex), etc. The repository Kubernetes-sigs/sig-storage-lib-external-provisioner houses a library for writing external provisioners that implements the bulk of the specification. Some external provisioners are listed under the repository Kubernetes-incubator/external-storage.12

Creating custom storage classes
# cat custom-storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: custom-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
In the above code, we have defined −
kind: StorageClass → We have defined the kind as StorageClass which tells Kubernetes that the yaml file being used is to create the StorageClass.
name: custom-storage → Name of StorageClass that we are creating.

Checking the storage class and confirm its created
# kubectl apply -f custom-storage-class.yaml
storageclass.storage.k8s.io/custom-storage created

We should see the new Storage Class
# kubectl get storageclass
NAME PROVISIONER AGE
custom-storage kubernetes.io/no-provisioner ld
default (default) kubernetes.io/no-provisioner ld

Create the PersistentVolumeClaim
# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: custom-storage
resources:
requests:
storage: 1Gi
Creating PVC
# kubectl apply -f pvc.yaml
persistentvolumeclaim/app-pvc created

Getting Details About PVC
# kubectl get pvc/app-pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
app-pvc Pending custom-storage 3m11s
we will notice that the STATUS is Pending which means that it is not yet associated with a PersistentVolume

Creating a Deployment:
# cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: /var/www/html
name: data
volumes:
- name: data
persistentVolumeClaim:
claimName: app-pvc
created the deployment:
# kubectl apply -f deploy.yaml
deployment.apps/nginx-app created

Once it’s Running, check Persistentvolumeclaim again
# kubectl get pvc/app-pvc

The Status would have changed to Bound. As a result of the Pod creation, the storage request was analyzed and dynamic provisioning was triggered using the StorageClass mentioned in the PersistentVolumeClaim.

Delete the app and the Persistent Volume Claim
Let’s delete the app and the PersistentVolumeClaimas well.
#kubectl delete -f deploy.yaml
#kubectl delete -f pvc.yaml

Check the PersistentVolume using kubectl get pv - we will see that the Status has now changed to Released from Bound.




Recent Comments

No comments

Leave a Comment