Storage Classes in Kubernetes
🗼Introduction
Kubernetes, the powerful orchestration platform, provides robust storage solutions through its concept of Storage Classes. Storage Classes in Kubernetes offer a way to dynamically provision storage for your applications, allowing you to define different types of storage (e.g., SSD, HDD) and access modes (e.g., ReadWriteOnce, ReadOnlyMany). In this blog, we'll dive into the concept of Storage Classes and demonstrate how to create and use them on two popular cloud platforms: AWS and GCP.
🗼What is a Storage Class in Kubernetes?
A Storage Class in Kubernetes defines the "class" of storage a user wants to use. It abstracts the details of the underlying storage, providing a way for administrators to define different types of storage and their properties. This abstraction allows users to request storage without needing to know the specifics of how it is implemented.
Key Components of a Storage Class:
Provisioner: This specifies the type of storage backend (e.g., AWS EBS, GCP PD).
Parameters: These are specific to the provisioner and define the properties of the storage (e.g., type of disk, size).
Reclaim Policy: This defines what happens to the storage when it is released (e.g., Retain, Delete).
🗼Setting Up Storage Classes on AWS and GCP
🎗️AWS Example: Using EBS
Step 1: Define the Storage Class
First, we'll define a Storage Class for AWS EBS. Save the following YAML manifest as aws-storageclass.yaml
:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: ebs-sc
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
parameters:
csi.storage.k8s.io/fstype: xfs
type: io1
iopsPerGB: "50"
encrypted: "true"
allowedTopologies:
- matchLabelExpressions:
- key: topology.ebs.csi.aws.com/zone
values:
- us-east-2c
Step 2: Apply the Storage Class
Apply the Storage Class to your Kubernetes cluster using kubectl
:
kubectl apply -f aws-storageclass.yaml
Step 3: Create a Persistent Volume Claim
Next, create a PVC that uses this Storage Class. Save the following YAML manifest as aws-pvc.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: aws-ebs-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: ebs-sc
Apply the PVC:
kubectl apply -f aws-pvc.yaml
This PVC will dynamically provision an EBS volume of 10Gi using the defined Storage Class.
🎗️GCP Example: Using Persistent Disks
Step 1: Define the Storage Class
For GCP, we'll define a Storage Class for Persistent Disks. Save the following YAML manifest as gcp-storageclass.yaml
:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gcp-sc
provisioner: pd.csi.storage.gke.io
parameters:
type: pd-balanced
csi.storage.k8s.io/fstype: ext4
volumeBindingMode: WaitForFirstConsumer
Step 2: Apply the Storage Class
Apply the Storage Class to your Kubernetes cluster:
kubectl apply -f gcp-storageclass.yaml
Step 3: Create a Persistent Volume Claim
Create a PVC that uses this Storage Class. Save the following YAML manifest as gcp-pvc.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: gcp-pd-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: gcp-sc
Apply the PVC:
kubectl apply -f gcp-pvc.yaml
This PVC will dynamically provision a Persistent Disk of 20Gi using the defined Storage Class.
🗼Conclusion
Storage Classes in Kubernetes provide a powerful way to manage storage for your applications, abstracting the complexities of underlying storage technologies. By using Storage Classes, you can easily provision and manage storage resources tailored to the needs of your workloads. In this blog, we've shown how to define and use Storage Classes on AWS and GCP, enabling you to leverage the dynamic provisioning capabilities of Kubernetes on these cloud platforms. Start experimenting with Storage Classes in your Kubernetes cluster and unlock the full potential of dynamic storage provisioning!
Subscribe to my newsletter
Read articles from Ashutosh Mahajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ashutosh Mahajan
Ashutosh Mahajan
Proficient in variety of DevOps technologies, including AWS, Linux, Shell Scripting, Python, Docker, Terraform, Jenkins and Computer Networking. They have strong ability to troubleshoot and resolve issues and are consistently motivated to expand their knowledge and skills through expantion of new technologies.