Home / How To / Storage in Kubernetes

Storage in Kubernetes



Data can not be stored in the pod, when the pod is deleted or terminated, the information in it is not left on the system. To provide long-term and temporary storage for Pods in the cluster, Kubernetes provides various types of storage mechanisms.

In this article, we will look at examples of just two types of storage mechanisms.

  1. Host path:
    A hostPath volume mounts a file or directory from the host node’s file system to your Pod. This type of volume can be used to allow a pod to specify whether a given hostPath should exist before the Pod is run or whether it should be created. This type of volume is not something that most skis need. DirectoryOrCreate type creates an empty directory if it does not exist with permissions set to 0755 and Directory Type helps to ensure that a directory exists at the given path before the pod is created.
  2. emptyDir:
    An emptyDir volume is only created when a Pod is assigned to a node and exists as long as the Pod is running on that node. By default, emptyDir volumes are stored on the type of storage that our environment has. We can also set the field emptyDir.medium to Memory to tell Kubernetes to mount a RAM-backed file system

Click here to learn more about other storage mechanisms in Kubernetes.

Conditions

  1. Kubernetes Cluster with at least 1 working node.
    Click here to learn how to create a Kubernetes cluster. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18.04 EC2 instances.

What should 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 volume with type: Catalog

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

v-host path definition-1

Get a list of pods and create a pod that will mount “/ opt / mounted-here” in 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-the-details

If you now describe the pod, you will see that the creation of the pod has failed. The mountain failed because the directory “/ dir” is not on host. To mount a volume in the pod with “type: directory”, the host directory must exist.

Let us now create a directory “/ dir” of values, ie. on the worker node.

sudo mkdir /dir

Once we have created a directory of the worker codes, 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 log in to 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 can now be viewed from the worker node under the mounted directory “/ dir”

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

sign-in pod-and-create-a-file

This time we delete the line we created and see if the file is still in 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 the line has been deleted, the file is still under “/ dir” on the worker node. This means that data remains even after the line has been deleted or terminated.

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

Host volume with type: DirectoryOrCreate

To ensure that a host directory is available before mounting, we can use “type: DirectoryOrCreate” instead of “type: Directory” in the host track volume.

Create a new file with the 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

v-host path-2-definition

Obtain 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-the-details

When we describe the pod, you can see that the pod has been successfully created. And the directory “/ mount-this” which is not in the host / worker node was created on the host while it was mounted 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 codes in the directory “/ mount-this” which is mounted in the pod.

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

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

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

create-file-on-cough-and-control-in-the-pod

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

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

Run 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 is created on the host machine before it is mounted in the pod.

tomDirvolym

Create an object definition file with 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-definition emptydir advertisement

Get a list of pods and create a new pod to mount a volume with the 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-its-describe details

In the screenshot above you can see that the pod has been created and the volume is available.

Log in now to the pod and create a directory under the “/ opt / this” mounting path. This time we have no volume from the host.

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

sign-in-the-pod-and-see-no-folder-on-the-host

Now we can remove the pod if it is no longer needed.

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

Conclusion

In this article, we looked at the steps and sample files for creating podcasts with a hostPath and emptyDir volume. We saw how “type: DirectoryOrCreate” under hostPath volume type can help us mount a directory that is not on the workers’ node / host.


Source link