Certainly! In Kubernetes, storage is managed through the use of persistent volumes (PVs) and persistent volume claims (PVCs). Here are some common usages of storage in Kubernetes along with examples:
1. Static Provisioning:
Usage:
Pre-provision storage volumes before pods are created.
Example:
Copy apiVersion : v1
kind : PersistentVolume
metadata :
name : my-pv
spec :
capacity :
storage : 5Gi
accessModes :
- ReadWriteOnce
hostPath :
path : "/mnt/data"
2. Dynamic Provisioning:
Usage:
Automatically provision storage volumes when a PVC is created.
Example:
Copy apiVersion : storage.k8s.io/v1
kind : StorageClass
metadata :
name : my-sc
provisioner : my-provisioner
---
apiVersion : v1
kind : PersistentVolumeClaim
metadata :
name : my-pvc
spec :
storageClassName : my-sc
accessModes :
- ReadWriteOnce
resources :
requests :
storage : 5Gi
3. Volume Mounts in Pods:
Usage:
Mounting persistent volumes into pods.
Example:
Copy apiVersion : v1
kind : Pod
metadata :
name : mypod
spec :
containers :
- name : mycontainer
image : myimage
volumeMounts :
- name : my-pv-storage
mountPath : /app/data
volumes :
- name : my-pv-storage
persistentVolumeClaim :
claimName : my-pvc
4. ReadWriteMany Access Mode:
Usage:
Allow multiple pods to read and write to the same volume simultaneously.
Example:
Copy apiVersion : v1
kind : PersistentVolume
metadata :
name : my-pv
spec :
capacity :
storage : 5Gi
accessModes :
- ReadWriteMany
hostPath :
path : "/mnt/data"
5. ReadOnlyMany Access Mode:
Usage:
Allow multiple pods to read from the same volume, but only one pod can write.
Example:
Copy apiVersion : v1
kind : PersistentVolume
metadata :
name : my-pv
spec :
capacity :
storage : 5Gi
accessModes :
- ReadOnlyMany
hostPath :
path : "/mnt/data"
6. StatefulSets:
Usage:
Managing stateful applications with persistent storage.
Example:
Copy apiVersion : apps/v1
kind : StatefulSet
metadata :
name : my-statefulset
spec :
serviceName : "web"
replicas : 3
template :
metadata :
labels :
app : my-app
spec :
containers :
- name : my-container
image : my-image
volumeMounts :
- name : storage-volume
mountPath : /data
volumeClaimTemplates :
- metadata :
name : storage-volume
spec :
accessModes : [ "ReadWriteOnce" ]
resources :
requests :
storage : 5Gi
7. Storage Classes:
Usage:
Define storage classes with different properties.
Example:
Copy apiVersion : storage.k8s.io/v1
kind : StorageClass
metadata :
name : fast
provisioner : my-provisioner
parameters :
type : fast
8. Persistent Volume Resize:
Usage:
Dynamically resize a persistent volume.
Example:
Copy apiVersion : v1
kind : PersistentVolume
metadata :
name : my-pv
spec :
capacity :
storage : 5Gi
accessModes :
- ReadWriteOnce
hostPath :
path : "/mnt/data"
9. Subpath Volume Mounts:
Usage:
Mounting specific subdirectories from a persistent volume.
Example:
Copy apiVersion : v1
kind : Pod
metadata :
name : mypod
spec :
containers :
- name : mycontainer
image : myimage
volumeMounts :
- name : my-pv-storage
mountPath : /app/data
subPath : mysubdirectory
volumes :
- name : my-pv-storage
persistentVolumeClaim :
claimName : my-pvc
These examples cover various storage-related scenarios in Kubernetes, demonstrating how to provision, use, and manage storage resources for applications running in a Kubernetes cluster.