Earlier, in Kubernetes Volumes, we got some understanding of Volumes. This post focuses on practice, learning how to use the three local storage options: emptydir, hostpath, and localvolume.
1. Basic Properties of a PV
1.1 PV Lifecycle
PV states:
- Available: available, not yet bound by any PVC
- Bound: already bound to a PVC
- Released: the PVC was deleted, but the resource has not yet been reclaimed
- Failed: automatic reclamation failed
1.2 PV Reclaim Policy
PersistentVolumeReclaimPolicy, i.e. the PV’s reclaim policy.
What to do when a Pod no longer needs the PV:
- Retain: keep the data, the administrator must clean it up manually
- Recycle: reclaim the resource, clearing the data in the PV
- Delete: delete the PV directly
Currently only NFS and HostPath type volumes support reclaim policies, while AWS EBS, GCE PD, Azure Disk, and Cinder support the Delete policy.
1.3 PV Access Modes
PV access modes (accessModes):
- ReadWriteOnce: the PV is mounted read-write to a single Pod
- ReadWriteMany: the PV is mounted read-write to multiple Pods
- ReadOnlyMany: the PV is mounted read-only to multiple Pods
2. emptyDir
When using emptyDir, Kubernetes automatically allocates a directory for the Pod on the Node. The directory starts out empty, and when the Pod is removed from the Node, the data in the emptyDir is removed as well.
It is mainly used for temporary directories that do not need to be persisted, shared directories between multiple containers, and similar scenarios.
Create the file emptydir.yaml
| |
Run the commands:
| |
You can see that file sharing between the two containers works.
3. hostPath
The hostPath type maps a file or directory from the Node’s filesystem into a Pod. Supported types are files, directories, File, Socket, CharDevice, and BlockDevice.
Create the file hostpath.yaml
| |
- Log in to host i-6fns0nua and create the directory
| |
- Create the hostpath
| |
- Log in to host i-6fns0nua and view the shared file
| |
4. local volume
Scenarios that suit local volume:
- Data caching, where applications can access data nearby and process it quickly.
- Distributed storage systems, such as distributed databases and distributed file systems.
4.1 Creating Static Storage
Create the file sc.yaml
| |
pv.yaml
| |
4.2 Using It in a Pod
Create the file pvc.yaml
| |
Kubernetes automatically matches the PV and the PVC. Below, the storage is used inside a Pod through the PVC.
pod.yaml
| |
