Volume
A volume is essentially a directory backed by a storage medium the storage medium. The storage medium and its content are determined by the volume type.
- Containers are short lived in nature
- All data shared inside a container is deleted if the container crashes. However the kubelet will restart it and clean state, which means that it will not have any of the old data.
- To overcome this problem, kubernetes uses Volume.
- In kubernetes, a volume is attached to a pod and shared among the container of that pod.
- The volume has the same life span as the pod and it outlives the containers of the pod this allows data to be preserved across container within the pod even if the container restarts.
![image]
Volume Types
A volume tight besides the properties of the directory like size, content, etc some example of volume types are -
- node-local Types such as EmptyDir and HostPath
- File sharing type such as NFS
- Cloud provider-specific types like AWS Elastic Block Store, Azure disk
- Distributed file system types for example glusterfs or cephfs
- Special purpose type of volume like secret, gitrepo
EmptyDir
- Use this when we want to share contents between multiple containers on same pod and not to the host machine.
- An EmptyDir volume is first created when a pod is assigned to a node and exists as long as the pod is running on that node
- As the name says, it is initially empty
- Containers in the pod can all read and write the same files in the EmptyDir through that volume can be mounted at the same or different paths in each containers
- When a pod is removed from a node for any reason, the data in the EmptyDir deleted forever.
- A container crashing doesn't remove a pod from node, so the data in an EmptyDir volume is safe across container even if the container crashes.
[!image]
kind: Pod
apiVersion: v1
metadata:
name: example-13
spec:
containers:
- name: c1
image: centos
command: ["/bin/bash", "-c", "sleep 15000"]
volumeMounts:
- name: xchange
mountPath: "/tmp/xchange"
- name: c2
image: centos
command: ["/bin/bash", "-c", "sleep 10000"]
volumeMounts:
- name: xchange
mountPath: "/tmp/data"
volumes:
- name: xchange
emptyDir: {}
The volumeMounts name and the volumes name must be matched then only it will be accessible. Mountpath may vary.
Verify the pods
kubectl get pods
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-13 2/2 Running 0 9s
Create a file in emptyDir
volume at mounted path from a container and verify from the other container
kubectl exec -it example-13 -c c1 -- /bin/bash
kubectl exec -it example-13 -c c2 -- ls /tmp/data
controlplane $ kubectl exec -it example-13 -c c1 -- ls /tmp/xchange
c01_file.txt
controlplane $ kubectl exec -it example-13 -c c2 -- ls /tmp/data
c01_file.txt
As you can see the file are being shared within the pod which accessible by all the containers presesnt inside that.
HostPath
- Use these when we want to access the content of a pot or container from the host machine.
- A host path volume mounts a file or directory from the host nodes file system into your pod.
kind: Pod
apiVersion: v1
metadata:
name: example-13
spec:
containers:
- name: c1
image: centos
command: ["/bin/bash", "-c", "sleep 15000"]
volumeMounts:
- name: xchange
mountPath: "/tmp/xchange"
volumes:
- name: xchange
hostPath:
path: /tmp/data
The volumeMounts name and the volumes name must be matched then only it will be accessible. Mountpath may vary.
Verify the pods
kubectl get pods
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-13 1/1 Running 0 13s
Create a file in shared volume from a container using mounted path and verify from the node
kubectl exec -it example-13 -- touch /tmp/xchange/c01_file.txt
controlplane $ kubectl exec -it example-13 -- touch /tmp/xchange/c01_file.txt
controlplane $ ls /tmp/data
c01_file.txt