Storage and Persistence
🎯 Introduction
Our Node.js order service has two Pods that may be replaced during updates or failures. Order data is stored in MySQL. If we add image or attachment uploads, we would usually use object storage, such as OSS, COS, or S3, rather than store files inside an order Pod.
Why does Kubernetes also provide persistent storage? Some applications need to write files to a directory and retain them after a Pod is deleted and recreated. Examples include databases running inside the cluster and other services that need to retain files.
First, we will distinguish where business data normally lives and when a PVC is needed. Then we will use a separate test Pod to verify file persistence. We will leave the order service unchanged and will not treat mounting a PVC on an order Pod as the default upload solution.
🧱 Why can files inside a Pod disappear?
Containers can create files, but data in a container's writable layer does not automatically survive container recreation. After an old Pod is deleted, a new Pod using the same image does not automatically inherit its files.
Kubernetes can mount storage into a container directory through a Volume. However, not every Volume preserves data long term:
| File location | How long does data remain? |
|---|---|
| Container writable layer | Previously written data may be lost when the container is recreated |
emptyDir temporary volume | Survives container restarts within the same Pod; deleted with the Pod |
| Persistent volume accessed through a PVC | Can survive Pod deletion and recreation while the underlying storage and PVC remain available |
We will verify the third case: keeping files independently of the Pod lifecycle.
📦 Where do real applications normally store data?
A typical Node.js service assigns different data to suitable storage systems, rather than writing everything into its Pod:
| Data | Common storage | Does the order Pod usually need a PVC? |
|---|---|---|
| Business records such as orders and users | Databases such as MySQL or PostgreSQL | No |
| Images, attachments, and videos | Object storage such as OSS, COS, or S3 | Usually not |
| Application logs | Standard output, collected by a logging system | Usually not |
| Temporary processing files | Container temporary directory or emptyDir | No persistent PVC needed |
| In-cluster services that must retain files | Persistent volumes and PVCs as appropriate | Possibly |
“No” means the order service Pod itself usually does not need a PVC. If MySQL also runs inside Kubernetes, its own Pod may need one to store database files.
The PVC experiment below teaches how Kubernetes manages persistent storage. It is not a production recommendation to store order attachments directly in a PVC.
🧩 How do Volumes, PVs, and PVCs work together?
Suppose a test Pod needs to retain files in /app/uploads. Instead of specifying a disk location in the Pod, we first request storage and then have the Pod use it.
How does the test Pod get persistent storage?
This exercise demonstrates a PVC in a test Pod, not a production upload storage solution for the order service.
Test Pod
/app/uploads
Mount the storage as a container directory using a Volume
PVC
orders-files · 1Gi
Declare the required capacity and access mode
PV
Storage resource bound to the PVC
Files are saved in the underlying storage for the PV
A StorageClass can determine how PVs are provisioned. The diagram shows the relationship between the Pod, PVC, and PV. The backing storage may or may not be a dedicated physical disk.
| Name | Role in this example |
|---|---|
| Volume | Declares storage in the Pod configuration and makes it accessible at /app/uploads inside the container |
| PVC (PersistentVolumeClaim) | Requests storage with the required capacity and access mode |
| PV (PersistentVolume) | A cluster storage resource that satisfies and binds to the PVC |
| StorageClass | Defines how a class of storage is provisioned; where supported, it enables automatic PV creation for PVCs |
Remember the relationship: a Pod references a PVC through a Volume; the PVC binds to a PV; files live in the underlying storage represented by the PV.
We will work only with the separate test Pod, leaving the two running orders replicas unchanged.
🛠 Requesting storage
1. Check whether the cluster can provide storage
In the existing node-course kind cluster, run:
kubectl get storageclass
A typical kind environment may show:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE
standard (default) rancher.io/local-path Delete WaitForFirstConsumer
standard (default) identifies the default StorageClass. WaitForFirstConsumer means the cluster may wait until a Pod using the PVC exists before provisioning and binding storage.
Other environments may differ. Without an available default StorageClass or a suitable pre-created PV, the PVC below may remain unbound. Configure a storage provisioner first if needed. Pending does not always mean a configuration error.
2. Create a PVC
Create pvc.yaml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: orders-files
namespace: course
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Focus on three points:
orders-files: The practice PVC name the test Pod will use to find storage.storage: 1Gi: Requests approximately 1 GiB of storage.ReadWriteOnce: Abbreviated RWO; generally allows read-write mounting by one node, not necessarily only one Pod.
Run:
kubectl apply -f pvc.yaml
kubectl get pvc -n course
If the PVC shows Pending, do not immediately assume a problem. Some StorageClasses wait for a Pod using the claim before binding it.
📦 Mounting storage in the test Pod
Create storage-test.yaml as follows. A lightweight BusyBox container is used solely to verify files, without changing the order service code.
apiVersion: v1
kind: Pod
metadata:
name: orders-storage-test
namespace: course
spec:
containers:
- name: test
image: busybox:1.36
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: files
mountPath: /app/uploads
volumes:
- name: files
persistentVolumeClaim:
claimName: orders-files
Focus on two sections:
volumes: Defines a volume namedfilesbacked by theorders-filesPVC.volumeMounts: Mounts it at/app/uploadsinside the container. Files written there go to the corresponding storage.
Run:
kubectl apply -f storage-test.yaml
kubectl wait --for=condition=Ready pod/orders-storage-test -n course --timeout=120s
kubectl get pvc -n course
Simulated output; actual volume names will differ:
pod/orders-storage-test created
pod/orders-storage-test condition met
NAME STATUS VOLUME CAPACITY ACCESS MODES
orders-files Bound pvc-12345678-aaaa-bbbb-cccc-123456789abc 1Gi RWO
Bound means the PVC has bound to a storage resource, and the test Pod is ready. Inspect the cluster's PVs next:
kubectl get pv
Simulated output, showing only key columns; volume names will differ:
NAME STATUS CLAIM STORAGECLASS
pvc-12345678-aaaa-bbbb-cccc-123456789abc Bound course/orders-files standard
We created a PVC without manually creating a PV. CLAIM shows that this PV is bound to orders-files in the course Namespace. In this example, the StorageClass's provisioner created the PV dynamically.
If the Pod does not start, inspect events with kubectl describe pod orders-storage-test -n course. Investigate issues such as storage binding or image pull failures before continuing.
🔎 Do files survive deleting and recreating the Pod?
Now perform the article's main experiment.
1. Write a file
Use kubectl exec to run a command inside the test container, writing into the mounted directory:
kubectl exec -n course orders-storage-test -- \
sh -c 'echo "hello kubernetes" > /app/uploads/hello.txt'
Read the file:
kubectl exec -n course orders-storage-test -- \
cat /app/uploads/hello.txt
Output:
hello kubernetes
This proves that the file was written to the mount, but not yet that it survives the Pod's deletion.
2. Delete the old Pod and create a new one
Delete the test Pod, keeping the PVC, then use the same YAML to create a new test Pod:
kubectl delete pod orders-storage-test -n course
kubectl apply -f storage-test.yaml
kubectl wait --for=condition=Ready pod/orders-storage-test -n course --timeout=120s
The new Pod has the same name, but it is a newly created resource, not the old Pod resuming execution.
3. Read the file again
kubectl exec -n course orders-storage-test -- \
cat /app/uploads/hello.txt
Expected output:
hello kubernetes
The old Pod is gone, but the file is still readable. We retained the PVC, so the new Pod mounts the same bound storage rather than using the old container's writable layer.
This verifies data survival across Pod deletion and recreation. It does not guarantee survival after every disk failure or deletion of the entire cluster.
⚠️ A PVC does not guarantee that data can never be lost
Keep two practical limitations in mind.
First, multiple Pods cannot necessarily use one PVC as a shared disk. For example, ReadWriteOnce (RWO) generally permits read-write mounting by only one node, so it cannot simply serve as a shared disk across nodes.
Second, persistence is not backup. Whether data survives node failure, cluster deletion, or accidental PVC deletion depends on underlying storage and reclaim policy. kind's local storage is suitable for learning, not production data protection. Applications such as MySQL may also need mechanisms such as StatefulSets for stable identity and persistence; database deployment is beyond this article.
After the exercise, you can delete the test Pod:
kubectl delete pod orders-storage-test -n course
Once you no longer need the practice files, delete the PVC:
kubectl delete pvc orders-files -n course
Deleting a PVC may delete the underlying volume and its files, depending on the PV's reclaim policy. Do not use this cleanup command on a PVC containing real business data.
❓ Questions and answers
Q: Why does the file remain after deleting the test Pod?
It lives in storage bound to the PVC, not the old container's writable layer. Deleting a Pod does not automatically delete its PVC, so a new Pod can mount it and read the existing files.
Q: Is a newly created PVC broken if it shows Pending?
Not necessarily. Some StorageClasses use WaitForFirstConsumer and bind only after a Pod using the PVC exists. If it remains unbound afterward, inspect the StorageClass and Pod and PVC events.
Q: Do I still need backups with a PVC?
Yes. PVCs let Pods use persistent storage; they do not guarantee that the underlying storage cannot fail or be accidentally deleted.
🧪 Exercise
Independently verify file persistence: create the test Pod and write order-001.txt with any content in /app/uploads. Delete and recreate the Pod, then check whether the file remains.
Success criteria: Read the file again and explain why deleting the Pod preserves access, while deleting the PVC may lose data.
🎉 You now know where typical Node.js services store data, understand Volumes, PVs, and PVCs, and have verified that a recreated Pod can read existing files while the underlying storage and PVC remain available.
Scaling and Autoscaling
Scale order service Pods manually, understand CPU-based HPA autoscaling, and configure and verify it in a kind cluster.
LLM Overview
Explore what large language models can and cannot do through an online store support example, and learn how they differ from conventional programs and agents.
