0% found this document useful (0 votes)
25 views5 pages

Storage

Uploaded by

deepak Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Storage

Uploaded by

deepak Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

12. storage.

md 2025-08-20

Kubernetes Storage
Applications running in containers often need to read and write files. By default, containers use a temporary
filesystem. If a container crashes or a node restarts, any data written to this temporary storage is lost.
When the container restarts, it starts with a clean slate.

Volume Types
Type Description
Empty directory in Pod with read/write access. Exists only for the Pod's
emptyDir
lifespan. Useful for cache or data exchange between containers in a Pod.
hostPath File or directory from the host nodeʼs filesystem.
, Inject configuration data into Pods. See “Defining and Consuming
configMap secret
Configuration Data”.
nfs Network file system.
persistentVolumeClaim Claims persistent storage resources.
Example: Using an emptyDir Volume
apiVersion: v1
kind: Pod
metadata:
name: business-app
spec:
volumes:
- name: logs-volume
emptyDir: {}
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /var/logs
name: logs-volume

kubectl create -f pod-with-volume.yaml


kubectl get pod business-app
kubectl exec business-app -it -- /bin/sh

Persistent Volumes
1/5
12. storage.md 2025-08-20

A PersistentVolume (PV) is a storage resource in a Kubernetes cluster, decoupled from Pods and with its
own lifecycle. PVs can be:
Statically provisioned: Created by an admin and referenced explicitly.
Dynamically provisioned: Created automatically via a StorageClass when a PersistentVolumeClaim
(PVC) is made.
Example: Static PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: db-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/db

kubectl create -f db-pv.yaml


kubectl get pv db-pv

PersistentVolume Volume Modes


Type Description
Filesystem Default. Mounts the volume into a directory. Creates a filesystem if needed.
Block Uses the volume as a raw block device, without a filesystem.

kubectl get pv -o wide

PersistentVolume Access Modes


Type Short Form Description
ReadWriteOnce RWO Read/write by a single node
ReadOnlyMany ROX Read-only by many nodes
ReadWriteMany RWX Read/write by many nodes
ReadWriteOncePod RWOP Read/write by a single Pod
2/5
12. storage.md 2025-08-20

kubectl get pv db-pv -o jsonpath='{.spec.accessModes}'

PersistentVolume Reclaim Policies


Type Description
Retain Default. PV is "released" and can be reclaimed after PVC deletion.
Delete PV and its storage are deleted when PVC is deleted.
Recycle Deprecated. Use Retain or Delete instead.

kubectl get pv db-pv -o jsonpath='{.spec.persistentVolumeReclaimPolicy}'

Example: PersistentVolumeClaim
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: db-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: ""
resources:
requests:
storage: 256Mi

kubectl create -f db-pvc.yaml


kubectl get pvc db-pvc

Consuming a PVC in a Pod


apiVersion: v1
kind: Pod
metadata:
name: app-consuming-pvc
spec:
volumes:
- name: app-storage

3/5
12. storage.md 2025-08-20

persistentVolumeClaim:
claimName: db-pvc
containers:
- image: alpine
name: app
command: ["/bin/sh"]
args: ["-c", "while true; do sleep 60; done;"]
volumeMounts:
- mountPath: "/mnt/data"
name: app-storage

kubectl create -f app-consuming-pvc.yaml


kubectl get pods
kubectl describe pod app-consuming-pvc
kubectl describe pvc db-pvc
kubectl exec app-consuming-pvc -it -- /bin/sh

Storage Classes
A StorageClass defines a type or "class" of storage (e.g., fast SSD, remote cloud storage, backup policy).
StorageClasses enable dynamic provisioning of PersistentVolumes.
Creating a StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/gce-pd
reclaimPolicy: Delete

kubectl create -f fast-sc.yaml


kubectl get storageclass

Example output:

NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE


ALLOWVOLUMEEXPANSION AGE
fast kubernetes.io/gce-pd Delete Immediate false
1m

4/5
12. storage.md 2025-08-20

Using a StorageClass in a PVC


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: db-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
storageClassName: standard

Summary:
Use Volumes for temporary or shared storage within Pods.
Use PersistentVolumes and PersistentVolumeClaims for durable storage.
Use StorageClasses for dynamic provisioning and advanced storage features.

5/5

You might also like