Why we need Kubernetes Volumes?
On-disk files in a container are ephemeral, which presents some problems for non-trivial applications when running in containers.
- One problem is the loss of files when a container crashes.
- The second problem occurs when sharing files between containers running together in a
Pod
.
The Kubernetes volume abstraction solves both of these problems.
Docker has a concept of volumes, though it is somewhat looser and less managed. A Docker volume is a directory on a disk or in another container. Docker provides volume drivers, but the functionality is somewhat limited.
Type of Volumes
- awsElasticBlockStore: An
awsElasticBlockStore
volume mounts an Amazon Web Services (AWS) EBS volume into your pod. - azureDisk: The
azureDisk
volume type mounts a Microsoft Azure Data Disk into a pod. - azureFile: The
azureFile
volume type mounts a Microsoft Azure File volume (SMB 2.1 and 3.0) into a pod. - cephfs: A
cephfs
volume allows an existing CephFS volume to be mounted into your Pod. - cinder: The
cinder
volume type is used to mount the OpenStack Cinder volume into your pod. - configMap: A ConfigMap provides a way to inject configuration data into pods.
- fc (fibre channel): An
fc
volume type allows an existing fibre channel block storage volume to mount in a Pod. - gcePersistentDisk: A
gcePersistentDisk
volume mounts a Google Compute Engine (GCE) persistent disk (PD) into your Pod. - glusterfs: A
glusterfs
volume allows a Glusterfs (an open source networked filesystem) volume to be mounted into your Pod. - iscsi: An
iscsi
volume allows an existing iSCSI (SCSI over IP) volume to be mounted into your Pod. - local: A
local
volume represents a mounted local storage device such as a disk, partition or directory. Local volumes can only be used as a statically created PersistentVolume. Dynamic provisioning is not supported. Compared tohostPath
volumes,local
volumes are used in a durable and portable manner without manually scheduling pods to nodes. - nfs: An
nfs
volume allows an existing NFS (Network File System) share to be mounted into a Pod. - persistentVolumeClaim: A
persistentVolumeClaim
volume is used to mount a PersistentVolume into a Pod. - quobyte: A
quobyte
volume allows an existing Quobyte volume to be mounted into your Pod. - rbd: An
rbd
volume allows a Rados Block Device (RBD) volume to mount into your Pod. - secret:
secret
volume is used to pass sensitive information, such as passwords, to Pods. You can store secrets in the Kubernetes API and mount them as files for use by pods without coupling to Kubernetes directly. - storageOS: A
storageos
volume allows an existing StorageOS volume to mount into your Pod. - vsphereVolume: A
vsphereVolume
is used to mount a vSphere VMDK volume into your Pod. - hostPath: A
hostPath
volume mounts a file or directory from the host node’s filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.
Using subPath
Sometimes, it is useful to share one volume for multiple uses in a single pod. The volumeMounts.subPath
property specifies a sub-path inside the referenced volume instead of its root.
The following example shows how to configure a Pod with a LAMP stack (Linux Apache MySQL PHP) using a single, shared volume. This sample subPath
configuration is not recommended for production use.
The PHP application’s code and assets map to the volume’s html
folder and the MySQL database is stored in the volume’s mysql
folder. For example:
apiVersion: v1
kind: Pod
metadata:
name: my-lamp-site
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "rootpasswd"
volumeMounts:
- mountPath: /var/lib/mysql
name: site-data
subPath: mysql
- name: php
image: php:7.0-apache
volumeMounts:
- mountPath: /var/www/html
name: site-data
subPath: html
volumes:
- name: site-data
persistentVolumeClaim:
claimName: my-lamp-site-data
https://kubernetes.io/docs/concepts/storage/volumes/
Volume Plugin | ReadWriteOnce | ReadOnlyMany | ReadWriteMany |
---|---|---|---|
AWSElasticBlockStore | ✓ | – | – |
AzureFile | ✓ | ✓ | ✓ |
AzureDisk | ✓ | – | – |
CephFS | ✓ | ✓ | ✓ |
Cinder | ✓ | – | – |
CSI | depends on the driver | depends on the driver | depends on the driver |
FC | ✓ | ✓ | – |
FlexVolume | ✓ | ✓ | depends on the driver |
Flocker | ✓ | – | – |
GCEPersistentDisk | ✓ | ✓ | – |
Glusterfs | ✓ | ✓ | ✓ |
HostPath | ✓ | – | – |
iSCSI | ✓ | ✓ | – |
Quobyte | ✓ | ✓ | ✓ |
NFS | ✓ | ✓ | ✓ |
RBD | ✓ | ✓ | – |
VsphereVolume | ✓ | – | – (works when Pods are collocated) |
PortworxVolume | ✓ | – | ✓ |
ScaleIO | ✓ | ✓ | – |
StorageOS | ✓ | – | – |
https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/