Directory
- Volume type
- Emptydir
- Hostpath
Volume type
Volume is a shared directory accessed by multiple containers in the Kubernetes pod. Volume is defined on the pod and is mounted to the same or different paths by multiple containers of the pod. The life cycle of the volume is the same as that of the pod, and the container stops and restarts in the pod generally does not affect the data in volume. So the general volume is used to persist the data generated by the pod.
Kubernetes offers a wide range of volume types, including Emptydir, Hostpath, NFS, Glusterfs, CEPHFS, Ceph RBD, and more. Refer to the official documentation for details.
In this article we briefly say Emptydir and Hostpath.
Emptydir
The Emptydir type of volume is created when the pod is assigned to node, and Kubernetes automatically assigns a directory on node, so there is no need to specify the corresponding directory file on the host node. The initial contents of this directory are empty, and when the pod is removed from node, the data in the Emptydir is permanently deleted.
Emptydir volume is primarily used for some applications that do not require permanent storage of temporary directories, multiple containers for shared directories, and so on.
The following is an example of a pod mount Emptydir:
apiVersion: v1kind: Podmetadata: name: test-podspec: containers: - image: test-webserver name: test-container volumeMounts: - name: cache-volume mountPath: /cache volumes: - name: cache-volume emptyDir: {}
Hostpath
Hostpath volume is a directory or file on the pod Mount host, allowing the container to be stored using a host's high-speed file system. The disadvantage is that, in k8s, the pod is dynamically dispatched on the node nodes. When a pod is started on the current node node and the file is stored locally via Hostpath, the file stored on the previous node cannot be used the next time it is scheduled to start on another node.
Hostpath Use Example:
apiVersion: v1kind: Podmetadata: name: test-podspec: containers: - image: test-webserver name: test-container volumeMounts: - name: test-volume mountPath: /www volumes: - name: test-volume hostPath: path: /data
Emptydir and Hostpath