DevOps Kubernetes

Storage in Kubernetes

k8s storage

Data cannot be stored in the pod, when the pod is deleted or is terminated the data within it does not stay on the system. To provide long-term and temporary storage to Pods in the cluster, Kubernetes provides different types of storage mechanisms.

In this article, we will see examples of only 2 types of storage mechanisms.

  1. hostPath:
    A hostPath volume mounts a file or directory from the host node’s filesystem into your Pod. This type of volume can be used to allow a Pod to specify whether a given hostPath should exist prior to the Pod running or whether it should be created. This volume type is not something that most Pods will need. DirectoryOrCreate type will create an empty directory if it does not exist with permission set to 0755 and Directory type helps to make sure a directory exists at the given path before that pod is created.
  2. emptyDir:
    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. By default, emptyDir volumes are stored on the type of storage our environment has. We can also set the emptyDir.medium field to Memory to tell Kubernetes to mount a RAM-backed filesystem

To know about other storage mechanisms in Kubernetes, click here.

Pre-requisites

  1. Kubernetes Cluster with at least 1 worker node.
    If you want to learn to create a Kubernetes Cluster, click here. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18.04 EC2 Instances.

What will we do?

  1. Create a pod with a volume of different types mounted in it

Create a pod with a volume of different types mounted in it

Host path volume with type: Directory

Create an object definition file for mounting an existing directory in the pod.

vim volume-hostpath-1.yml
apiVersion: v1
kind: Pod
metadata:
  name: volume-hostpath-example-1
spec:
  containers:
  - image: nginx
    name: my-container
    volumeMounts:
    - mountPath: /opt/mounted-here
      name: my-volume-1
  volumes:
  - name: my-volume-1
    hostPath:
      path: /dir
      type: Directory

volume-hostpath-1-definition

Get a list of pods and create a pod that will mount “/opt/mounted-here” within the pod on “/dir” from the host.

kubectl  get pods
kubectl  create -f volume-hostpath-1.yml
kubectl  get pods
kubectl  describe pod volume-hostpath-example-1

create-pod-and-get-its-details

Now if you describe the pod you will see that the pod creation has failed. The Mount failed because the directory “/dir” does not exist on the host. To mount a volume in the pod with “type: Directory”, the host directory must exist.

Now let’s create a directory “/dir” on host i.e. on the worker node.

sudo mkdir /dir

Once we have created a directory on the worker nodes, we can delete the previously created pod and try to recreate a new pod.

kubectl  delete -f volume-hostpath-1.yml
kubectl  create -f volume-hostpath-1.yml
kubectl  get pods
kubectl  describe pod volume-hostpath-example-1

create-dir-on-host-and-then-create-a-pod

This time you can see that the pod has been created successfully and under volumes the mounted path “/dir” can be seen.

Now let’s login into the pod and create a file

kubectl  get pods
kubectl  exec -it volume-hostpath-example-1  /bin/bash
touch /opt/mounted-here/new-file

The file we created in the pod now can be seen from the worker node under the mounted directory “/dir”

ll /dir/ #Execute this command on the worker node

login-into-pod-and-create-a-file

This time let’s delete the pod we created and see if the file still exists on the host directory “/dir” or not.

exit
kubectl  get pods
kubectl delete pod volume-hostpath-example-1

List the files under “/dir” on the worker node.

ll /dir/ #Execute this command on the worker node

delete-the-pod-and-verify-file-on-host

We can see that even after deleting the pod the file still exists under “/dir” on the worker node. This means that the data persists even after the pod is deleted or terminated.

In this example, we observed that the host directory that needs to be mounted within the port must exist.

Host path volume with type: DirectoryOrCreate

To make sure that a host directory is available before mounting we can use “type: DirectoryOrCreate” instead of “type: Directory” in hostpath volume.

Create a new file with type “type: DirectoryOrCreate”.

vim volume-hostpath-2.yml
apiVersion: v1
kind: Pod
metadata:
  name: volume-hostpath-example-2
spec:
  containers:
  - image: nginx
    name: my-container
    volumeMounts:
    - mountPath: /opt/mounted-here
      name: my-volume-2
  volumes:
  - name: my-volume-2
    hostPath:
      # directory location on host
      path: /mount-this
      type: DirectoryOrCreate

volume-hostpath-2-definition

Get a list of existing pods and create a new pod with the file created in the above state.

kubectl  get pods
kubectl create -f volume-hostpath-2.yml
kubectl  get pods
kubectl  describe pod volume-hostpath-example-2

create-a-pod-and-get-its-details

When we describe the pod it can be seen that the pod has successfully been created. And the directory “/mount-this” which does not exist on the host/worker node got created on the host while mounting it in the volume.

Go to the worker nodes and check if the directory has been created or not

ll /mount-this/ #Execute this command on the worker node

Now create a new file on the worker nodes in the directory “/mount-this” that is mounted in the pod.

sudo touch /mount-this/created-on-host #Execute this command on the worker node

Let’s login to the pod and check “/opt/mounted-here/” directory to see if the file “/mount-this/created-on-host” we created on the work node exists in the pod.

kubectl  get pods
kubectl  exec -it volume-hostpath-example-2 /bin/bash
ls -l /opt/mounted-here/

create-file-on-host-and-verify-in-the-pod

Now delete the pod and see if the file is still exists on the worker node at  “/mount-this/”

exit
kubectl  get pods
kubectl delete pod volume-hostpath-example-2

Execute the following command to list the file on the worker node at “/mount-this/”

sudo ls -lt /mount-this/ #Execute this command on the worker node
delete-the-pod-and-verify-file-on-host

In this example, we saw that even if the host directory does not exist it gets created on the host machine before it is mounted in the pod.

emptyDir volume

Create an object definition file using the following.

vim volume-emptydir.yml
apiVersion: v1
kind: Pod
metadata:
  name: volume-emptydir-example
spec:
  containers:
  - image: nginx
    name: my-container
    volumeMounts:
    - mountPath: /opt/this
      name: my-volume-3
  volumes:
  - name: my-volume-3
    emptyDir: {}

volume-emptydir-definition  

Get a list of pods and create a new pod to mount a volume with type “emptyDir”

kubectl  get pods
kubectl  create -f volume-emptydir.yml #Create an object using the file
kubectl  get pods
kubectl  describe pod volume-emptydir-example #Get details  of the specified pod

create-a-pod-and-describe-its-details

In the above screenshot, it can be seen that the pod has been created and the volume is available.

Now login to the pod and create a directory under the mount path “/opt/this”. This time we do not have any volume from the host.

kubectl exec -it volume-emptydir-example /bin/bash #Login into the Pod

login-into-the-pod-and-see-no-folder-on-the-host

Now we can delete the pod if it is no longer required.

kubectl get pods #Get a list of Pods
kubectl  delete pod volume-emptydir-example #Delete the specified pod

Conclusion

In this article, we saw the steps and example files to create pods with a volume of type hostPath and emptyDir. We saw how “type:DirectoryOrCreate” under hostPath volume type can help us mount a directory that does not exist on the worker node/host.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !