A Kubernetes volume is a directory that contains data accessible to containers in a given Pod in the orchestration and scheduling platform. Volumes provide a plug-in mechanism to connect ephemeral containers with persistent data stores elsewhere.
Kubernetes volumes persist until the Pod -- Kubernetes's atomic unit of container deployment -- is deleted. When a Pod with a unique identification is deleted, the volume associated with it gets destroyed. If a Pod is deleted but replaced with an identical Pod, a new and identical volume is also created. No matter what Node -- a logical grouping of host resources -- the Pod runs on, Kubernetes will mount the Pod's volumes to it, allowing containers to move across infrastructure without losing access to the externally hosted data that they require for the workload.
The data within a volume outlasts the containers running in the Pod, which can shut down and restart as ephemeral units. Data remains safe in volumes even if a container crashes, because a container crash is not enough to cut off a Pod from a Node. A Pod specifies what volumes it will access, and where the volume will be placed in the containers.
Volumes cannot be added to other volumes and links do not exist between volumes. The Kubernetes user must specify volume mounting for each container in a Pod. A representation of each Pod's desired state -- what it should be and its volumes -- is stored in Kubernetes's API server and central controller. Kubernetes also uses its kubelet agents to reconcile deployments. For example, if a volume cannot be used twice, the kubelet will detect this and report that it cannot safely use the volume, and retry and reevaluate deployment options through the scheduler until one is ready that meets the requirements.
Types of Kubernetes volumes
Kubernetes supports more than 20 types of volumes. One common example is the emptyDir volume, created when a Pod is first assigned to a Node. The volume is initially empty, with all containers in the Pod able to read from it. This emptyDir volume allows users to mount multiple paths in each container. Another example of a common volume is hostPath, which mounts a directory from the host node's filesystem into a Pod.
A local volume can represent mounted local storage devices such as partitions, directories or disks.
Specialty volumes are used to mount platform-specific volumes into containers. Examples include AzureFileVolume and AzureDiskVolume for the Microsoft Azure public cloud, and vsphereVolume for VMware virtualized environments.
In some situations, it is helpful to share one volume across multiple uses in one Pod. Kubernetes contains a property, volumeMounts.subPath, to specify a subpath inside the referenced volume.