DEV Community

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

Posted on • Edited 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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay