There will be
always cases where we need to save data on to a drive or disk. Pods when
stopped will destroy the data stored inside the container. Node reboots also
clear any data from the Ram based disks. There are many times where we
need some shared spacer have containers
that process data and hand it off to another container before they die.
Kubernetes provides
us a way to manage storage for containers. A Volume is a directory accessible
to all running containers in the pod, with a guarantee that the data is
preserved. In this article we will see how we can create volumes and attach
them to the pods. For the demo we will use NFS as our additional drive.
Depending the type
of volumes, we differentiate the types of volumes as,
- Node-local volumes, such as emptyDir or hostPath
- Generic networked
volumes, such as nfs, glusterfs, or cephfs
- Cloud provider–specific volumes, such as
awsElasticBlockStore, azureDisk, or
gcePersistentDisk
- Special-purpose volumes, such as secret or
gitRepo
Empty directory - One of the easiest ways to achieve improved
persistence amid container crashes and data sharing within a pod is to use the
emptydir volume. This volume type can be used with either the storage volumes
of the node machine itself or an optional RAM disk for higher performance.
emptyDir volume is created when the pod is assigned to the node and
exists until the pod runs on that node.As the name says , the volume is initially empty and pod can
read and write contents to the volume.
Volume with Directory Type -
[root@manja17-I13330 kubenetes-config]# cat emptyDir-mysql.yml
apiVersion: v1
kind: Pod
metadata:
name:
"mysql"
labels:
name: "lbl-k8s-mysql"
spec:
containers:
- image:
docker.io/jagadesh1982/testing-service
name: "testing"
imagePullPolicy:
IfNotPresent
ports:
-
containerPort: 3306
volumeMounts:
- mountPath:
/data-mount
name: data
volumes:
- name: data
emptyDir: {}
Login to the container and see how it goes,
[root@manja17-I13330 kubenetes-config]# kubectl exec mysql -c testing
-it bash
root@mysql:/usr/src/app# cd /data-mount
root@mysql:/data-mount# pwd
/data-mount
root@mysql:/data-mount# touch hello
root@mysql:/data-mount# echo "hello world" >> ./hello
root@mysql:/data-mount# exit
exit
Volume with Memory Type - By default emptyDir volumes are stored on
whatever medium is backing the node. It that is an SSD disk the node restarts
will still have the data available. There is another type of backend “memory”
which tells k8 to mount a tmpfs ( Ram backed file System ). While this is fast
, any node restart clears the contents.
Lets create a Volume type with
[root@manja17-I13330 kubenetes-config]# cat emptyDir-memory-pod.yml
apiVersion: v1
kind: Pod
metadata:
name:
"mysql"
labels:
name:
"lbl-k8s-mysql"
spec:
containers:
- image:
docker.io/jagadesh1982/testing-service
name: "testing"
imagePullPolicy:
IfNotPresent
ports:
-
containerPort: 3306
volumeMounts:
- mountPath:
/data-mount
name: data
volumes:
- name: data
emptyDir:
medium:
Memory
[root@manja17-I13330 kubenetes-config]# kubectl exec mysql -c testing
-it bash
root@mysql:/# cd data-mount/
root@mysql:/data-mount# pwd
/data-mount
root@mysql:/# exit
Exit
In the next Volumes we will see how to mount a NFS location to the container.