Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

StorageClass Kubernetes: Creation and Usage

How to Create and Use Custom StorageClass in Kubernetes

When working with Kubernetes, the concept of StorageClass plays a crucial role in managing persistent storage for your workloads. Understanding and creating custom Storage Class objects enables you to tailor the storage setup based on your unique requirements, such as backup policies, quality-of-service levels, or specific infrastructure needs.

In this guide, we’ll walk you through the process of creating a custom Storage Class, associating it with a PersistentVolumeClaim (PVC), and deploying it effectively for your Kubernetes applications. Along the way, we’ll explore best practices and integration with ZippyOPS for managed services, including DevOps, security, and cloud automation.

Diagram showing the creation and management of a custom StorageClass in Kubernetes

What is a StorageClass in Kubernetes?

In Kubernetes, a StorageClass provides a way for administrators to define different storage types or classes, each offering distinct characteristics like speed, backup policies, and quality of service. Kubernetes does not impose any specific standards on what a StorageClass should represent, making it flexible to match the needs of your environment. Some systems may call these “profiles,” but the principle is the same.

The Storage Class resource allows Kubernetes to manage persistent storage dynamically. When a PersistentVolumeClaim (PVC) is made, Kubernetes uses the associated StorageClass to provision the necessary storage resources.

Key Components of a StorageClass

A StorageClass consists of several key fields:

  • Provisioner: Determines which plugin will be used to provision the persistent volumes (PVs). Kubernetes includes default provisioners (e.g., kubernetes.io/no-provisioner), but external provisioners can also be used, offering more flexibility.
  • Parameters: This allows the administrator to define specific settings for the volume plugin.
  • Reclaim Policy: Specifies what happens when the PersistentVolume (PV) associated with the StorageClass is deleted. The policy can be set to either “Retain” or “Delete.”

Let’s dive deeper into how to create and use custom Storage Class objects in Kubernetes.

Creating a Custom StorageClass in Kubernetes

To create a custom StorageClass, you need to define it in a YAML file. This YAML file contains the required specifications, including the name, provisioner, and other settings such as the volume binding mode.

Here’s an example of how to define a custom Storage Class:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: custom-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

In this example:

  • name: The name of the StorageClass is custom-storage.
  • provisioner: We are using kubernetes.io/no-provisioner which means no automatic provisioning is handled. This is useful in scenarios where you manage volumes externally.
  • volumeBindingMode: This setting determines when the volume is bound to a PVC. WaitForFirstConsumer means the volume will only be bound when a pod consumes the volume.

Applying the Custom Storage Class

Once you’ve created the YAML file for your StorageClass, you can apply it using kubectl:

kubectl apply -f custom-storage-class.yaml

To verify that the StorageClass was successfully created, run:

kubectl get storageclass

You should see your custom StorageClass listed alongside the default options.

Creating a PersistentVolumeClaim (PVC)

Once the StorageClass is set up, you can create a PersistentVolumeClaim (PVC) to request storage from your Kubernetes cluster. Here’s an example of a PVC that uses the custom StorageClass we just created:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: custom-storage
  resources:
    requests:
      storage: 1Gi

In this example:

  • The PVC requests 1 GiB of storage.
  • The storageClassName references the custom Storage Class custom-storage.

Applying the PVC

Run the following command to apply the PVC:

kubectl apply -f pvc.yaml

Check the status of the PVC:

kubectl get pvc app-pvc

Initially, the status will show as “Pending” because Kubernetes is waiting to find a matching PersistentVolume.

Deploying an Application with a PVC

Once the PVC is bound to a volume, you can create a deployment that uses the volume. Here’s how you can do that:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  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

This deployment creates a pod running the nginx container, and mounts the persistent storage to /var/www/html.

Applying the Deployment

To apply the deployment, run:

kubectl apply -f deploy.yaml

Once the pod is running, you can check the status of your PVC again. The status should change from “Pending” to “Bound,” indicating that the volume has been provisioned successfully.

Cleaning Up

After you no longer need the deployment and the PVC, you can delete them with:

kubectl delete -f deploy.yaml
kubectl delete -f pvc.yaml

Check the PersistentVolume status:

kubectl get pv

You’ll see the status change to “Released” from “Bound,” indicating that the volume is no longer in use.


ZippyOPS Integration:

At ZippyOPS, we offer consulting, implementation, and managed services for Kubernetes, including DevOps, DevSecOps, DataOps, and Cloud automation. Our team helps you integrate MLOps, AIOps, and Microservices into your infrastructure for improved scalability, performance, and security.

Whether you need help creating custom Storage Classes, implementing automated operations, or optimizing your Kubernetes clusters, ZippyOPS is your partner in streamlining complex workflows. Explore our services to see how we can support your organization with cutting-edge solutions:
ZippyOPS Services

For more technical insights, check out our solutions for Kubernetes and cloud optimization:
ZippyOPS Solutions

Contact us today for a free consultation at sales@zippyops.com.

Conclusion

Creating and managing custom StorageClass objects in Kubernetes is a powerful way to tailor your persistent storage to meet specific application needs. By defining your Storage Class and associating it with PVCs, you can ensure that your Kubernetes workloads are equipped with the right storage resources.

With the right tools, like those offered by ZippyOPS, you can ensure your Kubernetes environment is optimized for performance, security, and scalability.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top