Open In App

Managing Kubernetes Storage With CSI

Kubernetes is a popular platform that provides features to manage containerized applications efficiently and automate deployment. One of the aspects of managing the applications in Kubernetes is maintaining persistent storage.

What is Kubernetes CSI?

To maintain persistent storage, containerized applications need Kubernetes storage. CSI stands for container storage interface, which provides an interface for integrating storage systems with Kubernetes. It provides management and consumption of storage with Kubernetes clusters.



Terminologies

Before further proceeding, we need to know a few terminologies

The need for CSI

Kubernetes had limited features to integrate with external storage before CSI was introduced. So it was more challenging to add new storage without changing the database. Kubernetes CSI solved the issue and decoupled storage functionality from Kubernetes components.



How to use a CSI volume, step-by-step

Enable CSI on the cluster

So the first step is to deploy the CSI driver, with the storage provider in the cluster. That evolves enables the CSI driver as DaemonSet or deployment to run all nodes in the cluster.

kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/container-storage-interface/master/deployment.yaml

Create a StorageClass: StorageClass specifies the parameters for provisioning storage volumes. That includes attributes such as volume type, size, and access mode.

Example of StorageClass:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: example-storage-class
provisioner: my-storage-system

Create Provision Persistent Volumes: After the StorageClass is defined, Kubernetes can dynamically provision PVs based on PVC requests from pods.

kubectl apply -f pvc.yaml

The Example pvc.yaml file looks like:

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

Mount the Storage in a Pod:

In this step one needs to mount storage into a pod by mention volumes within the pod’s YAML configuration.

Sample mount the storage in a pod.

apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
volumes:
- name: example-volume # Define the volume to be mounted
persistentVolumeClaim:
claimName: example-pvc # Reference the PersistentVolumeClaim (PVC) to mount
containers:
- name: example-container
image: nginx # Use any desired container image
volumeMounts:
- mountPath: /data # Specify the mount path inside the container
name: example-volume # Reference the volume defined above

Explanation

Step By Step Code

Create a PVC file

Apply the PVC YAML file, verify the PVC

Apply the Pod Yaml file and verify the Pod

Kubernetes Storage Best Practices

Conclusion

CSI provides a flexible way to manage storage in kubernetes, that enables seamless integration with storage systems. With following above steps, it’s possible to manage persistent storage that enhance scalability.

Managing Kubernetes Storage with CSI – FAQ’s

How to handle data persistence in Kubernetes ?

To handle data persistence, PVs and PVCs used to provision persistent storage applications.

How to monitor storage in Kubernetes ?

Kubernetes monitoring tools like Grafana can be used as monitor storage performance, capacity and health.

How to use external storage with Kubernetes?

Kubernetes supports integration with various external storage systems through the Container Storage Interface (CSI), that allow users to leverage a wide range of storage solutions.

Article Tags :