Volumes
A Kubernetes volume is essentially a directory accessible to all containers running in a pod. In contrast to the container-local filesystem, the data in volumes is preserved across container restarts. The medium backing a volume and its contents are determined by the volume type:
- node-local types such as
emptyDirorhostPath - file-sharing types such as
nfs - cloud provider-specific types like
awsElasticBlockStore,azureDisk, orgcePersistentDisk - distributed file system types, for example
glusterfsorcephfs - special-purpose types like
secret,gitRepo
A special type of volume is PersistentVolume, which we will cover elsewhere.
Let’s create a pod with two containers that use an emptyDir volume to exchange data:
cat << EOF > pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: sharevol
spec:
containers:
- name: c1
image: centos:7
command:
- "bin/bash"
- "-c"
- "sleep 10000"
volumeMounts:
- name: xchange
mountPath: "/tmp/xchange"
- name: c2
image: centos:7
command:
- "bin/bash"
- "-c"
- "sleep 10000"
volumeMounts:
- name: xchange
mountPath: "/tmp/data"
volumes:
- name: xchange
emptyDir: {}
EOF
kubectl create -f pod.yaml
pod "sharevol" created
kubectl get pods sharevol -o wide
NAME READY STATUS RESTARTS AGE IP NODE
sharevol 2/2 Running 0 18m 10.244.128.70 my-k8s-01
We first exec into one of the containers in the pod, c1, check the volume mount and generate some data
kubectl exec sharevol -c c1 -i -t -- bash
[root@sharevol /]#
mount | grep xchange
/dev/vda1 on /tmp/xchange type ext4 (rw,relatime,data=ordered)
echo 'some data' > /tmp/xchange/data
When we now exec into c2, the second container running in the pod, we can see the volume mounted at /tmp/data and are able to read the data created in the previous step
kubectl exec sharevol -c c2 -i -t -- bash
[root@sharevol /]#
mount | grep /tmp/data
/dev/vda1 on /tmp/data type ext4 (rw,relatime,data=ordered)
cat /tmp/data/data
some data