DEV Community

Cover image for Understanding Kubernetes Volume Types (EmptyDir, ConfigMap, Secret, HostPath)
kubefeeds
kubefeeds

Posted on

Understanding Kubernetes Volume Types (EmptyDir, ConfigMap, Secret, HostPath)

Kubernetes volumes provide a way for containers running in Pods to access and share data. Each volume type in Kubernetes serves a specific purpose, enabling different use cases such as temporary storage, configuration management, secret handling, or mounting host directories.

This article explores key Kubernetes volume types: EmptyDir, ConfigMap, Secret, and HostPath.

1. EmptyDir Volume

Overview

  • An EmptyDir volume is created when a Pod is assigned to a node and lasts as long as the Pod runs.
  • It provides temporary storage that is initially empty.
  • Commonly used for temporary scratch space or data sharing between containers in the same Pod.

Key Features

  • Data is deleted when the Pod is deleted or moved to another node.
  • Can use memory-backed storage for faster performance.

Example: EmptyDir Volume

apiVersion: v1
kind: Pod
metadata:
  name: emptydir-pod
spec:
  containers:
  - name: app-container
    image: busybox
    command: ["sh", "-c", "echo Hello > /data/hello.txt; sleep 3600"]
    volumeMounts:
    - mountPath: /data
      name: temp-storage
  volumes:
  - name: temp-storage
    emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

2. ConfigMap Volume

Overview

  • A ConfigMap volume allows injecting configuration data into a Pod as files or environment variables.
  • Useful for decoupling configuration from application code.

Key Features

  • Data is stored in Kubernetes ConfigMaps and mounted as files or directories.
  • Changes to the ConfigMap can propagate to running Pods. Example: ConfigMap Volume

Create a ConfigMap:

   kubectl create configmap app-config --from-literal=app.name=MyApp
Enter fullscreen mode Exit fullscreen mode

Mount the ConfigMap:

   apiVersion: v1
   kind: Pod
   metadata:
     name: configmap-pod
   spec:
     containers:
     - name: app-container
       image: busybox
       command: ["sh", "-c", "cat /config/app.name; sleep 3600"]
       volumeMounts:
       - mountPath: /config
         name: config-volume
     volumes:
     - name: config-volume
       configMap:
         name: app-config
Enter fullscreen mode Exit fullscreen mode

3. Secret Volume

Overview

  • A Secret volume securely provides sensitive data like passwords, tokens, or keys to Pods.
  • Data is encrypted at rest and mounted as files or injected as environment variables.

Key Features

  • Built-in security for sensitive data.
  • Supports base64-encoded strings.

Example: Secret Volume

Create a Secret:

kubectl create secret generic app-secret --from-literal=api-key=12345
Enter fullscreen mode Exit fullscreen mode

Mount the Secret:

  apiVersion: v1
   kind: Pod
   metadata:
     name: secret-pod
   spec:
     containers:
     - name: app-container
       image: busybox
       command: ["sh", "-c", "cat /secrets/api-key; sleep 3600"]
       volumeMounts:
       - mountPath: /secrets
         name: secret-volume
     volumes:
     - name: secret-volume
       secret:
Enter fullscreen mode Exit fullscreen mode

Read the complete blog

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more