Kubernetes Volumes Explained

Kubernetes Volumes

  • Kubernetes volumes allow containers within a pod to access and share data through the file system.
  • Volumes provide a way for containers in a pod to share data or access a file system.
  • The file system can reside on the host server (worker nodes).

Importance of Volumes

  • Data persistence: Volumes help in persisting data.
  • Shared storage: They facilitate easy sharing of storage.

Types of Volumes (Deprecated)

  • AWS Elastic Block Store (EBS): Deprecated in EKS.
  • Azure Disk: Also deprecated.

Types of Volumes (Available)

  • Azure Files: For use with Azure.
  • ConfigMap: A way to configure.
  • EmptyDir: Focus of this lecture.

EmptyDir Volume

  • An emptyDir volume is created when a pod is assigned to a node.
  • As the name suggests, it's initially empty.
  • All containers in a pod can read and write the same files in the emptyDir volume.
  • The volume can be mounted at the same or different paths in each container.
  • When a pod is removed from the node, the data in the emptyDir is permanently deleted.
EmptyDir Explained
  • Consider a scenario with two worker nodes.
  • A pod is deployed into these nodes.
  • The pod contains two containers: Container A and Container B.
  • These containers may depend on each other or need to share files.
  • An emptyDir volume is created, acting like a disk mounted on both containers.
  • If a file is created in Container A, it is stored in the volume, and Container B can access it, and vice versa.
Analogy with EFS
  • This is similar to how Elastic File System (EFS) is used.
  • EFS is used to share files among multiple EC2 instances.
  • An EFS is created and mapped to two EC2 instances, allowing file sharing.
  • emptyDir in Kubernetes provides similar functionality but is specific to a pod.
Practical Demonstration: Deploying Two Containers in a Pod with EmptyDir Volume
  • Create a folder kubernetes-2025.
  • Open the folder in Visual Studio Code.
  • Create a subfolder called volumes.
  • Inside the volumes folder, create another folder called empty-dir.
  • Create a file named empty-dir.yaml inside the empty-dir folder.
Creating the Deployment YAML
  • Start with the basic deployment structure.
  • Name the deployment empty-directory.
  • Use the BusyBox image. BusyBox is a lightweight image that combines tiny versions of many common UNIX utilities into a single small executable.
  • BusyBox comes up, runs a command, then dies.
What is BusyBox?
  • BusyBox combines tiny versions of many common UNIX utilities into a single small executable.
  • It provides replacements for most of the utilities typically found in GNU, file utilities, shell utilities, etc.
  • The utilities in BusyBox have fewer options than their full-featured GNU counterparts.
  • GNU is just an image used to run commands, possibly for testing.
  • BusyBox comes up, runs your command, and then exits.
Applying the Initial YAML
  • Run kubectl apply -f empty-dir.yaml.
  • Check pods with kubectl get po. The pod will likely show CrashLoopBackOff because BusyBox doesn't do anything by default and exits.
Modifying the YAML for EmptyDir
  • To create a volume that will be attached to a container, define two containers.
  • Define the first container and the second container in the Pod spec.
Creating EmptyDir Volume
  • Under the spec section, define volumes:

    spec:
      volumes:
        - name: data101
          emptyDir: {}
        - name: data202
          emptyDir: {}
    
  • This creates two emptyDir volumes: data101 and data202.

Mounting the Volumes
  • Inside each container definition, use volumeMounts to specify how the volume should be mounted.

    volumeMounts:
      - name: data101
        mountPath: /pool
      - name: data202
        mountPath: /pool2
    
  • This mounts the data101 volume to the /pool path inside the container and the data202 volume to the /pool2 path.

  • Repeat this section for the second container, adjusting the mountPath as needed.

  • Define volumeMounts inside the container spec.

          volumeMounts:
            - name: data101
              mountPath: /jay
            - name: data202
              mountPath: /jee
    
  • The mount path for container two will be /jay and /jee.

Keeping BusyBox Running
  • To keep BusyBox running, use the command and args fields.

    command: ["/bin/sh"]
    args: ["-c", "touch /pool/bar.txt && sleep 3600"]
    
  • This command creates a file called bar.txt in the /pool directory (which is mounted to the data101 volume) and then sleeps for 3600 seconds (1 hour).

  • 3600<br/>seconds=1<br/>hour3600 <br />\newline seconds = 1 <br />\newline hour

Complete YAML Example
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: empty-directory
spec:
  replicas: 1
  selector:
    matchLabels:
      app: empty-directory
  template:
    metadata:
      labels:
        app: empty-directory
    spec:
      containers:
        - name: cont1
          image: busybox
          command: ["/bin/sh"]
          args: ["-c", "touch /pool/bar.txt && sleep 3600"]
          resources: {}
          volumeMounts:
            - name: data101
              mountPath: /pool
            - name: data202
              mountPath: /pool2
        - name: cont2
          image: busybox
          command: ["/bin/sh"]
          args: ["-c", "sleep 3600"]
          resources: {}
          volumeMounts:
            - name: data101
              mountPath: /jay
            - name: data202
              mountPath: /jee
      volumes:
        - name: data101
          emptyDir: {}
        - name: data202
          emptyDir: {}
```
Applying the YAML
  • Run kubectl apply -f empty-dir.yaml.
  • Check the pod status with kubectl get po. It should show two containers running.
Accessing the Containers
  • Log in to the first container:

    kubectl exec -it <pod-name> -c cont1 -- sh
    
  • Navigate to the /pool directory (where the data101 volume is mounted) using cd /pool.

  • You should see the bar.txt file.

  • Exit the container and log in to the second container:

    kubectl exec -it <pod-name> -c cont2 -- sh
    
  • Navigate to the /jay directory (where the data101 volume is mounted) using cd /jay.

  • You should also see the bar.txt file, confirming that both containers are sharing the same volume.

Creating a File in Container 2 and Viewing it in Container 1
  • In container two, create a new file using touch balaji.txt.
  • Exit container two and log back into container one.
  • Navigate to /pool in container one.
  • You should see balaji.txt there as well.

HostPath Volume

  • hostPath volumes mount a directory from the host node’s file system into a pod.
  • Unlike emptyDir, hostPath allows a pod to access files and directories on the host node.
  • You need to create the folder in the worker node.
  • It's just one container now, not two containers.
Diagram Explanation
  • Create the folder on the worker node and worker node two, the same folder, the same name, and we will now.
  • So we must create a folder.
  • Now it's just one container now, not two containers.
  • Now we must create the same folder in the two worker nodes because you know why?
  • We want to map mount this folder in our server worker node. We want to mount it to this port, to this container port running. That is what we want to do.
Configuration
  • Worker Node Requirements: must create the same folder.
  • Must create folder with the same name in both worker nodes because the scheduler can schedule pods in any of the nodes.
Disadvantages
  • Anything created on worker node one must be created on worker node 2 because we don't know where scheduling those space into your port.
  • Imagine, we will create this file in work on with one. I want to map mount this file to this port. Now scheduler, now schedule this port in work on with two, and this file is not in worker node two. Your deployment will be failing.
Creating the Deployment YAML
  • Now you know why I'm giving us an answer is good. I and mister was talking today. As I see, interview, you must the answer on they will would ask me. They are not looking for definition in interview is how have you done this?

    • What did you do?
    • And, how were you able to they just need to explain it.
  • Now, that is the name of the pod host path. You can give it any name. This can be any name. You know, we we wanna know the reason for this. Image, we can use any image. You might want to use busy box. You might want to use any image you you feel like using, you should use it. Any words. Any image you feel like using, you should use it. Okay?

  • Any image you feel like using, you should use it.

Modifying HostPath Volume
  • Instead of us to create different parts on our worker nodes, let's talk about the volume first. Okay.
  • Let's create the volume, the host part volume. How do we create it? The same way volumes. You understand now. Volumes.
Naming Volumes
  • Give the volumes a name like Jerry

  • volumes:
    - name: Jerry
      hostPath:
        path: /var/log
    
    • Creating a volume called Jerry and sharing /ver/log on the node

    • What type of volume are we creating? Host parts.

    • Now which part of the host do you want to share? I want to share VaLog. You know you know why I want to share VaLog.

    • Because every servers that means this is a space is a path in my worker nodes. That means there's in this worker node one. There's in this worker node two.

    • So that's why I'm using it. So anywhere scheduler schedule my busy box. I'm fine. Anywhere scheduler scheduled my busy box. It's fine.

Mount Volumes
  • Mount the volume by creating mount point yaml volumeMounts: - name: Jerry mountPath: /balaji
    • We are saying in this container, we want to mount this part of the volume to this part in the container.
    • This container does not have this part by default. It's gonna create it. This container does not have it, so it's gonna create it. So all you want us to okay. Let me change it. In the container, it's gonna be Balaji.
    • So if I go into Balaji, everything we have in logs will be in Bolaji. These logs is for the host worker nodes. That is the host part we are sharing to the container. Why I'm using it is because that particular part in the host is available in the two servers.
    • This part this part, val log, is available in the two server. Okay.
Verification
  • We can see these are everything we have in the master control plane. That means we have this same thing in the worker nodes. So imagine all of this is what you are going to be seeing in the container because this is our host part that we are sharing to the container. That is you can see the part the host part part is here.
    • So we want to share this part of our two worker nodes to the container busy box, and this is it.
    • We are mounting. What is the mount part in the container?
    • This. So whenever I see the first slash biology, that means I'm inside this path, and what is the volume and that. Is why we have volume in here in Jerry. Because this is the volume we want to mount, and this is the mount part of the volume. That is just it.
Example
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostpath
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hostpath
  template:
    metadata:
      labels:
        app: hostpath
    spec:
      containers:
      - name: megacont1
        image: busybox
        command: ["/bin/sh"]
        args: ["-c", "sleep 3600"]
        volumeMounts:
        - name: Jerry
          mountPath: /balaji
      volumes:
      - name: Jerry
        hostPath:
          path: /var/log
```
Implementation
  • Create the file name hostpath.yaml and persist the code above
  • bash kubectl apply -f hostpath.yaml
Logging in
  • Once completed do get pods, and finally exec into the pods to verify
    bash kubectl exec -it <podName> -- sh
  • Next, do a Ls and make sure biology persist
  • Finally do a Ls biology to read the /ver/log

Notes

  • The next volume will be Persistent Volume( PV) as well as persistent Volume claim(PVC)
  • The PVC will be implemented during the auto scaling lectures on Monday