kubernetes volumes
- files created in a container are ephemeral, i.e. if a kubernetes pod is restarted, the newly created containers will not have access to the files from the previous containers
kubernetes volumesmitigates the issue of sharing files
Volume types:
emptyDir: initial empty directoryhostPath: mounted directory from the cluster node filesystemnfs: a shared NFSgcePersistentDisk,awsElasticBlokStore,azureDisk: volumes exposed by cloud providerscinder,cephfs,iscsi,flocker…: network storageConfigMaps,Secrets,downwardAPI: volumes specialized for exposingKubernetesresourcesPersistentVolumeClaim: a way to dynamically allocate volumes
EmptyDir
- the lifecycle of an
EmptyDirvolume is linked to the kubernetes pod, i.e. if the kubernetes pod is destroyed, so is the volume - useful for containers in the same kubernetes pod that needs to collaborate together
---
apiVersion: v1
kind: Pod
metadata:
name: shared-vol
spec:
volumes:
- name: my-data
emptyDir: { }
containers:
- name: log2fs
image: foobar/k8s-training-nginx-log2fs: 1.19-alpine-v1
volumeMounts:
- name: my-data
mountPath: /var/log/nginx
- name: shell
image: debian: 10-slim
command: [ "bash" , "-c" , "sleep infinity"]
volumeMounts:
- name: my-data
mountPath: /dataHostPath
- sometime, a kubernetes pod needs to access to the cluster node filesystem, e.g. a kubernetes pod created by a kubernetes DaemonSet to archive logs
- those volumes are persistent, but it’s not reliable because your kubernetes pod may not be run in the same node
---
apiVersion: v1
kind: Pod
metadata:
name: hostpath-vol
spec:
volumes:
- name: my-data
hostPath:
path: /data/my-data
containers:
- name: log2fs
image: foobar/k8s-training-nginx-log2fs: 1.19-alpine-v1
volumeMounts:
- name: my-data
mountPath: /var/log/nginxPersistent Volumes and Persistent Volumes Claims
- abstraction layer to provision and consume volumes
PersistentVolumeis a storage space dedicated for cluster adminsPersistentVolumeClaimis a storage space requested by the cluster usersPVCare similar to kubernetes pod- kubernetes pods consumes resources (CPU & RAM) of the cluster node
PVCconsumes cluster storage resource
PVCprovidesPVwith some criteria:- storage size
- access type (RW, RO, …)
PVCdo not expose the way it provision the storage to the usersStorageClassare a way to expose different types of available volumes
Lifecycle of PV and PVC
PVcan be provisionedstaticallyordynamicallystatic PVare pre-provisioned by the adminsdynamic PVare provisioned on the fly by the cluster using theStorageClass
- a
PVCmust specify aStorageClass - an empty
StorageClass(i.e."") is equivalent to astatic PV - ⚠️ if no
PVmatches the request, it will be on stand-by indefinitely (or until its deletion) - ⚠️ deleting a kubernetes pod does not delete the associated
PVC- the
ReclaimPolicyassociated to thePVdetermine what happens to thePVonce it’s freed:retained: thePVis no longer used but can be associated to anotherPVCrecycled: thePVis cleaned, i.e. its data are deleted, and once the deletion is finished, thePVcan be associated to aPVCdeleted: thePVis deleted
- the
Access modes
ReadWriteOnce(RWO): the volume can be mounted in RW for a single nodeReadOnlyMany(ROX): the volume can be mounted in RO for multiple nodesReadWriteMany(RWX): the volume can be mounted in RW for multiple nodes
title: A volume can only be mounted in one mode at a time!PV example
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0003
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
nfs:
path: /tmp
server: 172.17.0.2
mountOptions:
- hard
- nfsvers=4.1PVC example
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
storageClassName: slow
selector:
matchLabels:
release: "stable"
matchExpressions:
- { key: environment , operator: In , values: [ dev ] }PVC usage:
---
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
volumes:
- name: mypd
persistentVolumeClaim:
claimName: myclaim
containers:
- name: myfrontend
image: nginx
volumeMounts:
- name: mypd
mountPath: /var/www/htmlStorageClass example
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard
provisioner: kubernetes.io/aws - ebs
parameters:
type: gp2
reclaimPolicy: Retain
mountOptions:
- debug