Ephemeral Volumes

If pod dies data can die with it. While containers are alive they need to share some data.

image.png

Step 1 : Creating K8s Cluster in AWS using eksctl

eksctl create cluster --name pv-cluster --region ap-south-1 --nodes 3 --node-type t2.medium

Step 2 : Starting a deployment with one pod having 2 containers that shares some data

manifest.yml

apiVersion: apps/v1
kind: Deployment # Starting a deployment
metadata:
  name: shared-data-deployment 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: shared-data-pod # All pods labelled with it , will be part of this deployment
  template:
    metadata:
      labels:
        app: shared-data-pod # Attching label to pod
    spec:
      containers:
        - name: writer # This container writes to shared-data
          image: busybox # Process that's running infinitely
          command: ["/bin/sh","-c","echo 'Hello from writer container' > /data/hello.txt; sleep 3600" ] # Run a shell command writes `Hello..' to /data/hello.txt every 1 hr
          volumeMounts: # In this container I want this volume to be mounted at mountPath
            - name: shared-data 
              mountPath: /data
        - name: reader
          image: busybox 
          command: ["/bin/sh","-c","cat /data/hello.txt; sleep 3600"] # Read from /data every 1hr
          volumeMounts:
            - name: shared-data 
              mountPath: /data 
      volumes: # Describe volumes that containers have access to 
        - name : shared-data
          emptyDir: {} # Type of volume
kubectl apply -f manifest.yml

Step 3 : Check if one container is reading and other is writing to file

# ssh into writer container and check if data is written to  /data/hello.txt
kubectl exec -it shared-data-deployment-69fb8d7c8d-jx4xq -c writer -- sh

# Check the logs of reader container to see if data gets logged
kubectl logs -f shared-data-deployment-69fb8d7c8d-jx4xq - reader

image.png

image.png

You can see the data is written in writer container and read by reader container i.e sharing of data by 2 containers inside same pod is happening

image.png

image.png