Open In App

Managing Kubernetes Storage With CSI

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Persistent Volume (PV): An abstract representation of storage from the underlying storage system.
  • Persistent Volume Claim (PVC): This is referred to as a user-defined request for storage resources.
  • Storage Class and Container Storage Interface (CSI): The storage class defines the properties of the storage as storage provisioners.

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

  • ‘volumes’ defines as storage volumes to be made available to the pod.
  • ‘example-pvc’ is a PVC that associated with volume.
  • ‘example-container’ is a single container which running an Nginx image.

Step By Step Code

Create a PVC file

PVC

Apply the PVC YAML file, verify the PVC

Pod

Apply the Pod Yaml file and verify the Pod

PVC & Pod

Kubernetes Storage Best Practices

  • Using persistent volumes and persistent volume claims to decouple storage configuration from application pods.
  • Based on workload requirements define StorageClass and consider the factors such as performance, scalability when selecting storage solutions.
  • Fine-tune storage resource requests. Distribute storage across multiple nodes to ensure high availability.
  • To ensure the data integrity, availability implement backup, replication strategies.

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads