Kubernetes Pod: Share a temporal Volume across containers

2 min read | by Jordi Prats

If we need to be able to share some data across containers (one generates the data and the other one consumes it) we can use an emptyDir to create a Volume to mount on both containers.

An emptyDir Volume stores data in a dynamically created directory on the k8s node. This directory only exists, along with it's data, as long as the pod exists on the node. Defining this Volume on a Pod allows us to mount it on as many containers as we want, for example:

apiVersion: v1 kind: Pod metadata:  name: shared-volume-pod spec:  containers:  - image: busybox  name: test-container1  volumeMounts:  - mountPath: /out  name: shared-volume  - image: busybox  name: test-container2  volumeMounts:  - mountPath: /in  name: shared-volume  volumes:  - name: shared-volume  emptyDir: {} 

By adding the medium attribute to emptyDir que can request the volume to be in memory instead of disk-based:

apiVersion: v1 kind: Pod metadata:  name: shared-volume-pod spec:  containers:  - image: busybox  name: test-container1  volumeMounts:  - mountPath: /out  name: shared-volume  - image: busybox  name: test-container2  volumeMounts:  - mountPath: /in  name: shared-volume  volumes:  - name: shared-volume  emptyDir:  medium: Memory 

Posted on 30/06/2021