Chapter 8. Volumes and Configuration Data
A volume in Kubernetes is a directory accessible to all containers running in a pod, with the additional guarantee that the data is preserved across restarts of individual containers.
We can distinguish between a few types of volumes:
-
Node-local ephemeral volumes, such as
emptyDir
-
Generic networked volumes, such as
nfs
orcephfs
-
Cloud provider–specific volumes, such as
AWS EBS
orAWS EFS
-
Special-purpose volumes, such as
secret
orconfigMap
Which volume type you choose depends entirely on your use case. For example, for a temporary scratch space, an emptyDir
would be fine, but when you need to make sure your data survives node failures, you’ll want to look into more resilient alternatives or cloud provider–specific solutions.
8.1 Exchanging Data Between Containers via a Local Volume
Problem
You have two or more containers running in a pod and want to be able to exchange data via filesystem operations.
Solution
Use a local volume of type emptyDir
.
The starting point is the following pod manifest, exchangedata.yaml, which has two containers (c1
and c2
) that each mount the local volume xchange
into their filesystem, using different mount points:
apiVersion
:
v1
kind
:
Pod
metadata
:
name
:
sharevol
spec
:
containers
:
-
name
:
c1
image
:
ubuntu:20.04
command
:
-
"bin/bash"
-
"-c"
-
"sleep
10000"
volumeMounts
:
-
name
:
xchange
mountPath
:
"/tmp/xchange"
-
name
:
c2
image
:
ubuntu:20.04
command
:
-
"bin/bash"
-
"-c"
-
"sleep ...
Get Kubernetes Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.