Using Local Storage
emptyDir
A volume of emptyDir type is created when a Pod is assigned to a node. Kubernetes automatically allocates a directory on the node, so there is no need to specify a corresponding directory file on the host node. The initial content of this directory is null.
Description
When a Pod is removed from a node, the data stored in emptyDir is permanently deleted.
Example
Create a Pod with emptyDir as the data volume:
1apiVersion: v1
2kind: Pod
3metadata:
4 name: test-ed
5spec:
6 containers:
7 - image: registry.baidubce.com/cce/nginx-alpine-go:latest
8 name: test-container
9 volumeMounts:
10 - mountPath: /test
11 name: cache-volume
12 volumes:
13 - name: cache-volume
14 emptyDir: {}
View details of the created Pod:
1$ kubectl describe pod test-ed
Check the mounting effect inside the Pod:
1$ kubectl exec -it test-ed -c test-container -- /bin/sh
hostPath
The HostPath type maps files or directories from the node’s file system into the Pod. When using a HostPath persistent volume, you can also set the type field, which supports types such as file, directory, File, Socket, CharDevice and BlockDevice.
Example
Create a Pod with HostPath as the data volume:
1apiVersion: v1
2kind: Pod
3metadata:
4 name: test-hp
5spec:
6 containers:
7 - image: registry.baidubce.com/cce/nginx-alpine-go:latest
8 name: test-container
9 volumeMounts:
10 - mountPath: /test-pd
11 name: test-volume
12 volumes:
13 - name: test-volume
14 hostPath:
15 #directory location on host
16 path: /data
Local Volume
Local volume enables the use of local disks, disk partitions, or directories on a node as persistent storage. It is ideal for data caching where applications can quickly access local data for efficient processing. It is also suitable for distributed storage systems, such as distributed databases like Cassandra and distributed file systems like Ceph or Gluster.
Example
Create a storage class
1kind: StorageClass
2apiVersion: storage.k8s.io/v1
3metadata:
4 name: local-volume
5provisioner: kubernetes.io/no-provisioner
6volumeBindingMode: WaitForFirstConsumer
Description
The WaitForFirstConsumer setting ensures that a PV is not immediately bound to a PVC but is only bound when a Pod requests the PVC.
Statically create a PV
Statically create a 5 GiB PV; this PV uses the /data/volume directory of the node (manually create the directory):
1apiVersion: v1
2kind: PersistentVolume
3metadata:
4 name: example-local-pv
5spec:
6 capacity:
7 storage: 5Gi
8 accessModes:
9 - ReadWriteOnce
10 persistentVolumeReclaimPolicy: Retain
11 storageClassName: local-volume
12 local:
13 path: /data/volume
14 nodeAffinity:
15 required:
16 nodeSelectorTerms:
17 - matchExpressions:
18 - key: kubernetes.io/hostname
19 operator: In
20 values:
21 - HOSTNAME # Replace with the corresponding node name
Use local volume PV
Create a PVC to bind to the local volume PVC, and create a Pod to use this PVC:
- Create a PVC
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4 name: mypvc
5spec:
6 accessModes:
7 - ReadWriteOnce
8 resources:
9 requests:
10 storage: 5Gi
11 storageClassName: local-volume
- Create a Pod
1kind: Pod
2apiVersion: v1
3metadata:
4 name: mypod
5spec:
6 containers:
7 - name: myfrontend
8 image: registry.baidubce.com/cce/nginx-alpine-go:latest
9 volumeMounts:
10 - mountPath: "/usr/share/nginx/html"
11 name: mypd
12 volumes:
13 - name: mypd
14 persistentVolumeClaim:
15 claimName: mypvc
- View the PV, PVC, and Pod that have been created.
1$ kubectl get pv
2NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
3example-local-pv 5Gi RWO Retain Bound default/mypvc local-volume 5s
4$ kubectl get pvc
5NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
6mypvc Bound example-local-pv 5Gi RWO local-volume 4m20s
7$ kubectl get pods
8NAME READY STATUS RESTARTS AGE
9mypod 1/1 Running 0 4m28s
- Write content to the local /data/volume and use curl to check whether the writing is successful
1$ echo "hello world" > /data/volume/index.html
2 $ curl 172.19.4.63 #Replace the ip of your own Pod here
3hello world
Note
To delete resources, you must first remove the created Pod, then delete the PVC, and finally delete the PV.
