Containers are ephemeral: when a pod dies, its local storage is lost.
emptyDir.yaml.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Pod
metadata:
name: emptydirpod
spec:
containers:
- name: my-app-container
image: nginx
volumeMounts:
- name: shared-data
mountPath: /var/data
- name: my-sidecar-container
image: busybox
command: ["sh", "-c", "echo 'hello from sidecar' > /shared/file.txt && sleep 60"]
volumeMounts:
- name: shared-data
mountPath: /shared
volumes:
- name: shared-data
emptyDir: {}
kubectl apply -f.kubectl get podsto identify the pod namekubectl describe pod to identify the container names.kubectl exec with additional -c flag to get into the my-app-container container./var/data/file.txt.Persistence
hostpath.yaml.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
name: hostpathpod
spec:
containers:
- name: my-container
image: busybox
command: ["sh", "-c", "echo 'hello from the other side' > /mnt/hostpath/file.txt && sleep 3600"]
volumeMounts:
- name: host-volume
mountPath: /mnt/hostpath
volumes:
- name: host-volume
hostPath:
path: /home/YOUR_USER_NAME_HERE/hostpath
type: DirectoryOrCreate
kubectl apply -f.~/hostpath/ directory.~/hostpath/ directory.kubectl exec to get into the pod and check the content of /mnt/hostpath/ directory.StorageClass.flowchart TD
subgraph NodeLocal["Node-local Storage"]
A["emptyDir (ephemeral)"]
B["hostPath (node filesystem)"]
end
subgraph ClusterStorage["Cluster / External Storage"]
C["Physical backend (NFS, EBS, Ceph, Longhorn, etc.)"]
D["PersistentVolume (PV)"]
end
E["PersistentVolumeClaim (PVC)"]
F["Pod"]
%% relationships
A --> F
B --> F
C --> D
D --> E
E --> F
cluster-wide resource.flowchart TD
subgraph Cluster["Kubernetes Cluster"]
N1["Node 1"]
N2["Node 2"]
N3["Node 3"]
N4["Node 4"]
end
subgraph Storage["Storage Backend"]
H["hostPath (Node-local)"]
E["EBS / NFS / Ceph / Longhorn (Networked)"]
end
PV1["PV (hostPath)"] --> H
PV2["PV (Networked)"] --> E
N1 -. can use .-> PV1
N2 -. cannot use .-> PV1
N3 -. cannot use .-> PV1
N4 -. cannot use .-> PV1
N1 -- can mount --> PV2
N2 -- can mount --> PV2
N3 -- can mount --> PV2
N4 -- can mount --> PV2