Kubernetes
Pod File Sharing
Kubernetes Volumes
File Mounting
Container Orchestration

What's the best way to share/mount one file into a pod?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Sharing or mounting a file into a Kubernetes pod is a common challenge in container orchestration. In this article, we'll explore different methods to achieve this, highlighting their advantages and use cases. Kubernetes provides several mechanisms for mounting data, and choosing the right strategy depends on factors like data persistence, lifecycle management, and access patterns.

1. ConfigMaps and Secrets

ConfigMaps and Secrets are Kubernetes-native objects designed to manage configuration and sensitive data, respectively. Both can be mounted as files inside a pod, providing an easy way to share data.

1.1 ConfigMaps

A ConfigMap can be used to inject configuration data or non-sensitive information into a pod. Here's an example:

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4  name: my-config
5data:
6  my-file: |
7    This is the content of the file.

To mount this ConfigMap as a file in a pod:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: my-pod
5spec:
6  containers:
7  - name: my-container
8    image: nginx
9    volumeMounts:
10    - name: config-volume
11      mountPath: /etc/config/my-file
12      subPath: my-file
13  volumes:
14  - name: config-volume
15    configMap:
16      name: my-config

1.2 Secrets

Secrets work similarly but are used for sensitive data such as passwords and tokens:

yaml
1apiVersion: v1
2kind: Secret
3metadata:
4  name: my-secret
5type: Opaque
6data:
7  my-sensitive-file: VGhpcyBpcyBzZWNyZXQ=

Mounting the Secret:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: my-pod
5spec:
6  containers:
7  - name: my-container
8    image: nginx
9    volumeMounts:
10    - name: secret-volume
11      mountPath: /etc/secret/my-sensitive-file
12      subPath: my-sensitive-file
13  volumes:
14  - name: secret-volume
15    secret:
16      secretName: my-secret

2. EmptyDir

EmptyDir is an ephemeral storage solution used when both the data and pod lifecycle align. The volume is initially empty, and data written to it is erased upon pod termination.

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: my-pod
5spec:
6  containers:
7  - name: my-container
8    image: nginx
9    volumeMounts:
10    - name: ephemeral-volume
11      mountPath: /mnt/tmp
12  volumes:
13  - name: ephemeral-volume
14    emptyDir: {}

3. HostPath

HostPath volumes allow pods to use files or directories on the host node. It's a simple approach but ties the pod to a specific node, limiting portability.

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: my-pod
5spec:
6  containers:
7  - name: my-container
8    image: nginx
9    volumeMounts:
10    - name: host-volume
11      mountPath: /var/local/data
12  volumes:
13  - name: host-volume
14    hostPath:
15      path: /data/on/host

4. Persistent Volume (PV) and Persistent Volume Claim (PVC)

For data needing persistence beyond individual pod lifecycles, PVs and PVCs offer a robust solution. A PV is a resource backed by a storage asset, and a PVC is a request for that storage.

4.1 Example of PV and PVC

yaml
1apiVersion: v1
2kind: PersistentVolume
3metadata:
4  name: pv-example
5spec:
6  capacity:
7    storage: 1Gi
8  accessModes:
9  - ReadWriteOnce
10  hostPath:
11    path: /mnt/data
yaml
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4  name: pvc-example
5spec:
6  accessModes:
7  - ReadWriteOnce
8  resources:
9    requests:
10      storage: 1Gi

Mounting the PVC in a pod:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: my-pod
5spec:
6  containers:
7  - name: my-container
8    image: nginx
9    volumeMounts:
10    - name: persistent-storage
11      mountPath: /data
12  volumes:
13  - name: persistent-storage
14    persistentVolumeClaim:
15      claimName: pvc-example

Key Considerations

  1. Security: Handle Secrets with strict access controls. Do not expose them unnecessarily.
  2. Lifecycle: Select volumes based on the data's lifecycle requirements. Persistent Volumes are ideal for data that must survive pod restarts.
  3. Portability: Use solutions like ConfigMaps and Secrets to keep configurations portable across environments.
  4. Performance: Consider the performance implications of your storage solutions, especially with network-backed storage.

Summary Table

Volume TypeUse CaseData PersistenceExample Use Case
ConfigMapConfiguration managementNoEnvironment configuration
SecretSensitive data storageNoAPI keys, SSL certificates
EmptyDirEphemeral, short-lived dataNoScratch space, caches
HostPathDev/test with host-specified storageNode-specificLocal development
Persistent VolumePersistent, durable storageYes (managed externally)Database storage, logs

Each method has its own pros and cons, making the selection dependent on specific application requirements. Proper understanding and implementation ensure seamless execution of Kubernetes workloads, with optimized data sharing and storage management strategies.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.