DEV Community

Madhavam Saxena
Madhavam Saxena

Posted on • Updated on

Volumes in Kubernetes- Part 1 (emptyDir)

Volumes:
State in kubernetes:
Data created and used by your application which must not be lost.
Types of data
1. User generated data
2. Intermediate data (either memory or temp. database)

Volumes in kubernetes are used to store the data of running pods. There are situations in live production environments when production pods get terminated, data inside those pods is important, now to avoid the loss of this data volumes are used in kubernetes.
Kubernetes can mount volumes in containers. We can set an instruction in the pod template which basically is part of deployment.yml that mounts a volume to the container that gets created with the pod which came up via deployment.yml.
Kubernetes provides various types of volumes:
Local Node: A directory on the worker node where the pod is running.
Cloud Provider Specific

Lifetime of a volume by default depends on the lifetime of a pod as volumes are part of the pod.

In the deployment.yml file:
We need to add the volume key inside the spec.template.spec. It basically is a list of volumes. We can give names to every volume that we create and these volumes will be accessible by all the containers inside that pod.
Under the name, we have to set up the volume type.

apiVersion: apps/v1
kind: Deployment
metadata:
    name: <name_of_deployment>
spec:
    replicas: 1
    selector:
        matchLabels:
            a: b
    spec:
        template:
            metadata:
                labels: 
                    a: b
            containers:
                - name: <name_of_container>
                  image: deocker_repo/<image_name>
            volumes:
                - name: <name_of_volumes>
                  emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

emptyDir: {}
EmptyDir is a type of volume that is used in kubernetes to store data.
This means we want to use all the default values and no configuration is declared instead default is used as it.
It simply creates a new empty directory whenever the pod starts and keeps the directory and writes the data till the lifespan of the pod. Containers can easily perform Write operation over it but if the pod dies, emptyDir dies with the pod i.e. this volume also terminates, and when a pod is created again a new empty directory is created.

Now, after creating a volume we need to bind the volume with the container, therefore we need to make it available inside of the container in container configuration with the volumeMounts keyword.
It takes the list. It takes mountPath i.e. which is the container internal path where the volume is mounted .
name keyword is used to mount the volume/s that we created above to the mountPath i.e. path internal of containers.

Top comments (0)