Kubernetes Level 02 – Day 01: Kubernetes Shared Volumes¶
This document explains how to create a multi-container Pod where multiple containers share access to the same temporary storage. The purpose is to demonstrate how containers inside a single Pod can exchange data through a shared volume.
- Kubernetes Level 02 – Day 01: Kubernetes Shared Volumes
- Objective
- Understanding the Architecture
- Step 1: Create the Pod Manifest
- Step 2: Apply the Configuration
- Step 3: Test Shared Storage
- Create File in Container 1
- Read File from Container 2
- How emptyDir Works Internally
- Common Use Cases
- Key Outcome
Objective¶
Create a Pod that contains two containers using a shared emptyDir volume.
Configuration requirements:
- Pod Name:
volume-share-datacenter - Volume Name:
volume-share - Volume Type:
emptyDir
Container specifications:
Container 1:
- Name:
volume-container-datacenter-1 - Image:
fedora:latest - Mount Path:
/tmp/blog
Container 2:
- Name:
volume-container-datacenter-2 - Image:
fedora:latest - Mount Path:
/tmp/apps
Both containers should remain running so that the shared storage can be tested.
Understanding the Architecture¶
A Pod can contain multiple containers that share the same:
- Network namespace
- IPC namespace
- Volumes
When a volume is mounted into multiple containers, all containers see the same underlying storage even if it appears under different paths inside each container.
This pattern is commonly used in:
- Sidecar containers
- Log collectors
- Data processors
Step 1: Create the Pod Manifest¶
Create a YAML file describing the Pod.
vi pod.yaml
Manifest¶
apiVersion: v1
kind: Pod
metadata:
name: volume-share-datacenter
spec:
containers:
- name: volume-container-datacenter-1
image: fedora:latest
command: ["sleep", "3600"]
volumeMounts:
- name: volume-share
mountPath: /tmp/blog
- name: volume-container-datacenter-2
image: fedora:latest
command: ["sleep", "3600"]
volumeMounts:
- name: volume-share
mountPath: /tmp/apps
volumes:
- name: volume-share
emptyDir: {}
Important Details¶
emptyDir creates a temporary directory on the node when the Pod starts.
Both containers mount the same volume but at different paths:
/tmp/blog/tmp/apps
Even though the paths differ, the underlying storage is identical.
The command sleep 3600 ensures the containers stay alive. Without a long-running process, the container would exit immediately.
Step 2: Apply the Configuration¶
Create the Pod using the manifest.
kubectl apply -f pod.yaml
Verify the Pod status:
kubectl get pods
Expected output:
volume-share-datacenter 2/2 Running
Both containers should be running.
Step 3: Test Shared Storage¶
To confirm that the volume is shared, create a file from one container and read it from the other.
Create File in Container 1¶
kubectl exec -it volume-share-datacenter \
-c volume-container-datacenter-1 \
-- /bin/sh -c "echo 'Shared Volume Test' > /tmp/blog/blog.txt"
This writes a file inside the mounted directory.
Read File from Container 2¶
kubectl exec -it volume-share-datacenter \
-c volume-container-datacenter-2 \
-- cat /tmp/apps/blog.txt
Expected output:
Shared Volume Test
This confirms that both containers are reading and writing to the same volume.
How emptyDir Works Internally¶
emptyDir volumes behave as temporary storage attached to a Pod.
Lifecycle behavior:
- Created when the Pod is scheduled to a node
- Initially empty
- Accessible by all containers in the Pod
- Deleted automatically when the Pod is removed
The data survives container restarts but does not survive Pod deletion.
Common Use Cases¶
Shared volumes inside Pods are frequently used in these patterns:
Sidecar Pattern¶
A helper container performs a supporting task such as fetching configuration or logs, writing the output to a shared directory.
Log Processing¶
Application container writes logs to a shared volume while another container processes or ships the logs to a monitoring system.
Temporary Processing¶
Multiple containers cooperate to process intermediate data stored in shared storage.
Key Outcome¶
The Pod volume-share-datacenter runs two containers that share an emptyDir volume. Data written by one container becomes immediately accessible to the other container, demonstrating how Kubernetes enables container collaboration within the same Pod.