Use Local Storage
EmptyDir
Overview
The emptyDir type Volume is created when the Pod is allocated to the Node. Kubernetes will automatically assign a directory on the Node. Therefore, there is no need to specify the corresponding directory file on the host Node. The initial content of this directory is empty.
When the Pod is removed from the Node, the data in emptyDir will be deleted permanently.
Create a Pod using kubectl create -f testemptyDir.yaml
. This Pod uses EmptyDir. The testemptyDir.yaml is as follows:
apiVersion: v1
kind: Pod
metadata:
name: test-ed
spec:
containers:
- image: nginx
name: test-container
volumeMounts:
- mountPath: /test
name: cache-volume
volumes:
- name: cache-volume
emptyDir: {}
View the details of the created Pod:
$ kubectl describe pod test-ed
Enter the Pod to view the mounting effect:
$ kubectl exec -it test-ed -c test-container /bin/sh
HostPath
Overview
The HostPath type maps files or directories in the Node file system to the Pod. When using the HostPath type of storage volume, you can also set the type field. The supported types include file, directory, File, Socket, CharDevice, and BlockDevice.
Create a Pod using kubectl create -f testhostPath.yaml
. The Pod uses HostPath. The testhostPathr.yaml is as follows:
apiVersion: v1
kind: Pod
metadata:
name: test-hp
spec:
containers:
- image: nginx
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
#directory location on host
path: /data
Local Volume
Overview
Local Volume allows you to use Node's local disks, partitions, or directories as persistent storage. Local Volume is suitable for data caching so that aplications can access data nearby and process it quickly. Also, it is suitable for distributed storage systems such as distributed database Cassandra and distributed file system ceph/gluster.
The applicability of some current Kubernetes to Local PV is as follows:
- Kubernetes v1.7: Officially introduced Local PV;
- Kubernetes v1.10: Local PV enters Beta;
- Kubernetes v1.14: Local PV enters GA
Create storage class
The content of kubectl create -f local-volume.yaml,local-volume.yaml
is as follows:
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-volume
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
Where, WaitForFirstConsumer indicates that you do not need to bind PV to PVC immediately, but bind it until a Pod needs to use PVC.
Create PV Statically
Through kubectl create -f local-volume-pv.yaml
, statically create a 5GiB PV. The PV uses the node's /data/volume directory. You need to create the directory manually.
The content of local-volume-pv.yaml is as follows:
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-local-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-volume
local:
path: /data/volume
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- HOSTNAME
Get node_name through kubectl get nodes and replace the HOSTNAME field above.
$ kubectl get pv example-local-pv
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
example-local-pv 5Gi RWO Retain Bound default/myclaim local-volume 5s
Use Local Volume PV
Create a PVC associated with the local-volume and then a pod to use the PVC, kubectl create -f local-volume-pvc.yaml
.
The content of ocal-volume-pvc.yaml is as follows:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-volume
---
kind: Pod
apiVersion: v1
metadata:
name: mypod
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: mypvc
View the created pv, pvc, and pod.
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
example-local-pv 5Gi RWO Retain Bound default/mypvc local-volume 5s
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc Bound example-local-pv 5Gi RWO local-volume 4m20s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 4m28s
Write content to the local /data/volume and use curl to check whether the write is successful.
$ echo "hello world" > /data/volume/index.html
$ curl 172.19.4.63
#Replace it with the ip of your pod.
hello world
During deletion, delete the created pod and then pvc and pv in turn.