If you store any data in a container/Pod the data is available until the container/Pod live. Once Container/Pod deleted, crashed, restarted you will lose the data.
If a Pod encapsulates multiple containers, there may be a need to share data across containers.
To solve this problem, Kubernetes come up with different storage options.
a. Pod Volumes: Used to allocate storage outside of the container
b. Persistent Volume (PV): Using this, Pods can connect to external storage. Persistent Volume claims (PVC) are used to request access to a specified storage.
c. ConfigMaps: Using this Pods can connect to Configuration files and variables.
How to define a Volume that is available to Pod?
Define the volume in spec.volumes.
How to mount a volume to the container?
Containers can mount the volume using spec.containers.volumeMounts property.
Volume Types
At the time of writing this post, Kubernetes support the following volume types.
a. awsElasticBlockStore
b. azureDisk
c. azureFile
d. cephfs
e. cinder
f. configMap
g. csi
h. downwardAPI
i. emptyDir
j. fc (fibre channel)
k. flexVolume
l. flocker
m. gcePersistentDisk
n. gitRepo (deprecated)
o. glusterfs
p. hostPath
q. iscsi
r. local
s. nfs
t. persistentVolumeClaim
u. projected
v. portworxVolume
w. quobyte
x. rbd
y. scaleIO
z. secret, storageos and vsphereVolume.
In this post, I am going to show the demo using ‘emptyDir’ volume type.
An emptyDir volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node. Containers in the Pod can read and write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each Container.
What will happen when a Pod deleted from the node?
Data in emptyDir will be deleted forever.
Let’s create a Pod with two containers and mount a volume that is shared across the containers.
Step 1: Create a Pod.
podVolumesDemo.yml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: hello-world
author: krishna
serviceType: user-service
spec:
containers:
- name: container1
image: busybox
command:
- sleep
- "300"
volumeMounts:
- mountPath: /container1-mount
name: my-volume
- name: container2
image: busybox
command:
- sleep
- "300"
volumeMounts:
- mountPath: /container2-mount
name: my-volume
volumes:
- name: my-volume
emptyDir: {}
Create Pod by executing the below command.
$kubectl create -f podVolumesDemo.yml
pod/my-pod created
Query all the pods.
$kubectl get pods
NAME READY STATUS RESTARTS AGE
my-pod 2/2 Running 0 77s
Step 2: Create some files in the mounted volume of container1 and make sure that these are available to container2.
Let’s create two files (hello.txt, demo.ppt) in the mounted directory.
kubectl exec -it my-pod -c container1 -- touch /container1-mount/hello.txt
kubectl exec -it my-pod -c container1 -- touch /container1-mount/demo.ppt
Let's confirm that the two files are available to container2
kubectl exec -it my-pod -c container2 -- ls -l /container2-mount
$kubectl exec -it my-pod -c container1 -- touch /container1-mount/hello.txt
$
$kubectl exec -it my-pod -c container1 -- touch /container1-mount/demo.ppt
$
$kubectl exec -it my-pod -c container2 -- ls -l /container2-mount
total 0
-rw-r--r-- 1 root root 0 Jun 8 09:15 demo.ppt
-rw-r--r-- 1 root root 0 Jun 8 09:15 hello.txt
You can delete the pod by executing the below command.
kubectl delete pod my-pod
$kubectl delete pod my-pod
pod "my-pod" deleted
No comments:
Post a Comment