Volumes in Kubernetes

Volumes in Kubernetes

The Pods created in Kubernetes are transient in nature. When a pod is created to process data and then deleted, the data processed by it gets deleted as well. Thus, we need a volume for the pod. The data generated by the pod is now stored in the volume, and even after the pod is deleted, the data remains.

Volumes and Mounts

apiVersion: v1
kind: Pod
metadata:
  name: number-generator
spec:
  containers:
    - name: alpine
      image: alpine
      command: ["/bin/sh", "-c"]
      args: ["shuf -i 0-100 -n 1 >> /opt/number.out;"]

This is a simple pod that generates a random number between 1 and 100 and writes that to a file at /opt/number.out and then gets deleted along with the random number. To retain the number generated by the pod, we created a volume and a volume needs storage. When you create a volume you can choose to configure it storage in different ways.

  • hostPath: Any files created in the volume would be stored in the directory data.
apiVersion: v1
kind: Pod
metadata:
  name: number-generator
spec:
  containers:
    - name: alpine
      image: alpine
      command: ["/bin/sh", "-c"]
      args: ["shuf -i 0-100 -n 1 >> /opt/number.out;"]

  volumes:
    - name: number-generator-volume
      hostPath:
          path: /data
          type: Directory

Once the volume is created to access if from a container we should mount the volume to a directory inside the container.

  • We can use volumeMounts field in each container
apiVersion: v1
kind: Pod
metadata:
  name: number-generator
spec:
  containers:
    - name: alpine
      image: alpine
      command: ["/bin/sh", "-c"]
      args: ["shuf -i 0-100 -n 1 >> /opt/number.out;"]
      volumeMounts:
        - mountPath: /opt
          name: number-generator-volume

  volumes:
    - name: number-generator-volume
      hostPath:
          path: /data
          type: Directory

Now even if the pod is deleted, the file with number still lives on the node.

volumes:
  - name: number-generator-volume
    hostPath:
       path: /data
       type: Directory

the volume option we used hostPath option to configure a directory and the host has storage space for the volume. That works fine on a single node. However, it is not recommended for use in a multi node cluster.

You need to configure some kind of external replicated cluster storage solution. Kubernetes supports several types of standard storage solutions such as NFS, FlusterFS, Flocker, FibreChannel, CephFS, ScaleIO, or public cloud Solutions AWS EBS, Azure Disk or file, Goole Persistent Disk.

For example, to configure an AWS Elastic Block Store volume as the storage or the volume, we replace hostPath to awsElasticBlockStore

volumes:
  - name: number-generator-volume
    awsElasticBlockStore:
        volumeID: <volume-id>
        fsType: ext4

Volumes in Docker

Docker Containers are meant to last only for a short period of time. They are called upon when required to process data and destroyed once finished. The same is for the data within the container Persistent data processed by the containers we attach a volume to the containers when they are created. The data are processed by the container is now placed in this volume thereby retaining it permanently. Even if the container is deleted the data generated or processed by it remains.

Leave a Reply

Your email address will not be published.

ANOTE.DEV