Labs ICT
Pro Login

Volumes

Attaching storage to containers

Volumes — Attaching Storage to Containers

A Volume is a directory accessible to containers in a Pod. Unlike Docker volumes, Kubernetes volumes have a lifecycle tied to the Pod — they exist as long as the Pod exists.

When a Pod is deleted, the volume is destroyed. For data that must survive Pod restarts, use Persistent Volumes.

Volume Types


  Kubernetes supports many volume types:
  ┌─────────────────┬──────────────────────────────────┐
  │  Volume Type    │  Description                     │
  ├─────────────────┼──────────────────────────────────┤
  │  emptyDir       │  Empty dir, deleted with pod     │
  │  hostPath       │  File from the host node         │
  │  configMap      │  Config data as a volume         │
  │  secret         │  Secret data as a volume         │
  │  persistentVolumeClaim │  Abstraction over storage │
  │  nfs            │  NFS share                       │
  │  awsElasticBlockStore  │  AWS EBS volume           │
  │  gcePersistentDisk     │  GCP persistent disk      │
  └─────────────────┴──────────────────────────────────┘

EmptyDir Volumes


apiVersion: v1
kind: Pod
metadata:
  name: shared-data
spec:
  containers:
    - name: writer
      image: busybox
      command: ["sh", "-c", "echo hello > /data/greeting; sleep 3600"]
      volumeMounts:
        - name: data
          mountPath: /data
    - name: reader
      image: busybox
      command: ["sh", "-c", "cat /data/greeting; sleep 3600"]
      volumeMounts:
        - name: data
          mountPath: /data
  volumes:
    - name: data
      emptyDir: {}

The writer and reader containers share the same emptyDir volume. Data written by one is visible to the other.

HostPath Volumes


volumes:
  - name: host-logs
    hostPath:
      path: /var/log
      type: Directory

HostPath mounts a file or directory from the host node into the Pod. It is useful for system-level tools but is generally discouraged in production because it ties the Pod to a specific node.

ConfigMap and Secret Volumes


volumes:
  - name: config
    configMap:
      name: my-config
  - name: credentials
    secret:
      secretName: my-secret

These mount ConfigMaps and Secrets as files inside the container. They are perfect for injecting configuration and sensitive data without baking them into images.