(01)Create PVC then StatefulSet
Linux
1kubectl apply -f pvc.yml2kubectl get pvc3kubectl apply -f statefulset.yml4kubectl get pods,pvc5kubectl exec app-with-storage-0 -- ls /dataPersistent storage for pods — data survives pod restarts (StatefulSet + PVC pattern)
pvc.yml
1apiVersion: v12kind: PersistentVolumeClaim3metadata:4 name: data-pvc5spec:6 accessModes:7 - ReadWriteOnce8 resources:9 requests:10 storage: 1Gi11 storageClassName: standardstatefulset.yml
StatefulSet mounting the PVC at /data
1apiVersion: apps/v12kind: StatefulSet3metadata:4 name: app-with-storage5spec:6 serviceName: app-svc7 replicas: 18 selector:9 matchLabels:10 app: app-with-storage11 template:12 metadata:13 labels:14 app: app-with-storage15 spec:16 containers:17 - name: app18 image: nginx:alpine19 ports:20 - containerPort: 8021 volumeMounts:22 - name: data23 mountPath: /data24 volumes:25 - name: data26 persistentVolumeClaim:27 claimName: data-pvcStep 01
1kubectl apply -f pvc.yml2kubectl get pvc3kubectl apply -f statefulset.yml4kubectl get pods,pvc5kubectl exec app-with-storage-0 -- ls /data