Suppose an application must keep critical data when its running instance is replaced. A stateless application avoids managing attached storage; a stateful application depends on it. Databases are a common example. In Kubernetes, persistent volumes let a Pod use storage that is managed separately from the Pod. section 1.0 section 9.0
Choose a workload arrangement that fits the application:
- For one replica, attach a PersistentVolumeClaim to a Pod managed by a Deployment. The Deployment recreates the Pod if it terminates. section 9.1.4
- For multiple replicas with state, use a StatefulSet rather than stitching together separate Deployments. section 9.0 section 9.1.4
A database that survives Pod replacement
A single-replica MariaDB Deployment mounts a claimed volume at its data directory. The Deployment can recreate a terminated Pod, while the attached disk holds its data. Running the database still brings operational work: securing, updating, and managing it. A managed database may be preferable unless the service needs customization or has no managed offering. section 9.1.4
Persistence needs a deletion policy, not just a volume mount. Deleting a StatefulSet does not delete its PersistentVolumeClaims, but deleting a claim can delete its underlying disk when the reclaim policy is Delete. Set the relevant StorageClass reclaim policy to Retain when the disk must survive removal of Kubernetes objects; the retained disk can then be relinked to recreated volume objects. section 9.3
Listing 9.5 Chapter09/9.1.4_Deployment_MariaDB/mariadb-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb-demo
spec:
replicas: 1
selector:
matchLabels:
app: mariadb
strategy: #1
type: Recreate #1
template: #2
metadata:
labels:
app: mariadb
spec:
containers:
- name: mariadb-container
image: mariadb:latest
volumeMounts:
- mountPath: /var/lib/mysql
name: mariadb-volume
resources:
requests:
cpu: 1
memory: 4Gi
env:
- name: MARIADB_ROOT_PASSWORD
value: "your database password"
volumes:
- name: mariadb-volume
persistentVolumeClaim:
claimName: mariadb-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mariadb-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
The MariaDB manifest shows a one-replica Deployment mounting a PersistentVolumeClaim at the database data directory. section 9.1.4













