9.5 Storage and Persistence
Managing Data in Kubernetes
Kubernetes provides several storage options for persistent data, from simple volumes to enterprise storage solutions.
Volumes
Basic Storage Types
# Pod with different volume types
apiVersion: v1
kind: Pod
metadata:
name: storage-demo
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: empty-vol
mountPath: /tmp/empty
- name: host-vol
mountPath: /tmp/host
- name: config-vol
mountPath: /etc/config
volumes:
- name: empty-vol
emptyDir: {} # Temporary storage
- name: host-vol
hostPath: # Node filesystem
path: /data
- name: config-vol
configMap: # Configuration data
name: app-config
Persistent Volumes
Long-term Storage
Persistent Volumes (PV) provide durable storage that exists beyond pod lifecycle.
Persistent Volume
# Manual PV creation
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/postgres
Persistent Volume Claim
# Request storage
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Using PVC in Pod
# Pod using PVC
apiVersion: v1
kind: Pod
metadata:
name: postgres
spec:
containers:
- name: postgres
image: postgres:15
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: postgres-pvc
Storage Classes
Dynamic Provisioning
Storage Classes enable automatic PV creation when PVCs are requested.
# AWS EBS Storage Class
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-ssd
provisioner: ebs.csi.aws.com
parameters:
type: gp3
fsType: ext4
reclaimPolicy: Delete
allowVolumeExpansion: true
Using Storage Class
# PVC with Storage Class
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-storage
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 20Gi
StatefulSet Storage
Volume Claim Templates
StatefulSets can automatically create PVCs for each pod.
# StatefulSet with storage
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: database
spec:
serviceName: database
replicas: 3
selector:
matchLabels:
app: database
template:
metadata:
labels:
app: database
spec:
containers:
- name: postgres
image: postgres:15
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: fast-ssd
resources:
requests:
storage: 50Gi
ConfigMaps and Secrets as Volumes
Configuration Files
# ConfigMap as volume
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
# Using ConfigMap in pod
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: config
mountPath: /etc/nginx/conf.d
volumes:
- name: config
configMap:
name: nginx-config
Backup and Snapshots
Volume Snapshots
# Create volume snapshot
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: postgres-snapshot
spec:
source:
persistentVolumeClaimName: postgres-pvc
volumeSnapshotClassName: csi-hostpath-snapclass
Restore from Snapshot
# PVC from snapshot
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-restore
spec:
dataSource:
name: postgres-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Essential Commands
# Persistent Volumes
kubectl get pv
kubectl get pvc
kubectl describe pv postgres-pv
kubectl describe pvc postgres-pvc
# Storage Classes
kubectl get storageclass
kubectl describe storageclass fast-ssd
# Volume usage
kubectl exec postgres-0 -- df -h /var/lib/postgresql/data
# Snapshots
kubectl get volumesnapshots
kubectl get volumesnapshotclasses
What’s Next?
Next, we’ll explore Configuration Management with ConfigMaps and Secrets.