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.

Depending on what is backing the volume and potentially additional semantics present, we differentiate the types of volumes:

  • Node-local volumes, such as emptyDir or hostPath

  • Generic networked volumes, such as nfs, glusterfs, or cephfs

  • Cloud provider–specific volumes, such as awsElasticBlockStore, azureDisk, or gcePersistentDisk

  • Special-purpose volumes, such as secret or gitRepo

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 networked volumes, or cloud-provider–specific ones if you run Kubernetes in a public cloud setting.

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:           centos:7
    command:
      - "bin/bash"
      - "-c"
      - "sleep ...

Get Kubernetes Cookbook 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.