Volumes
Overview
Volumes provide persistent and shared storage for containers in a pod.
Volume Types
| Type | Description | Persistence |
|---|---|---|
emptyDir | Temp directory, deleted with pod | Pod lifetime |
hostPath | Mount from node filesystem | Node |
configMap | Mount ConfigMap as files | Cluster |
secret | Mount Secret as files | Cluster |
persistentVolumeClaim | Mount PVC | Persistent |
downwardAPI | Mount pod metadata | Pod lifetime |
emptyDir
Temporary storage, shared between containers:
yaml
apiVersion: v1
kind: Pod
metadata:
name: emptydir-demo
spec:
containers:
- name: writer
image: busybox
command: ["sh", "-c", "echo hello > /data/file && sleep 3600"]
volumeMounts:
- name: shared-data
mountPath: /data
- name: reader
image: busybox
command: ["sh", "-c", "cat /data/file && sleep 3600"]
volumeMounts:
- name: shared-data
mountPath: /data
volumes:
- name: shared-data
emptyDir: {}emptyDir in Memory
yaml
volumes:
- name: cache
emptyDir:
medium: Memory # Use RAM instead of disk
sizeLimit: 100MihostPath
Mount directory from node:
yaml
apiVersion: v1
kind: Pod
metadata:
name: hostpath-demo
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "cat /host-logs/* && sleep 3600"]
volumeMounts:
- name: logs
mountPath: /host-logs
readOnly: true
volumes:
- name: logs
hostPath:
path: /var/log
type: DirectoryhostPath Types
| Type | Description |
|---|---|
"" | No check |
DirectoryOrCreate | Create if missing |
Directory | Must exist |
FileOrCreate | Create file if missing |
File | Must exist |
ConfigMap as Volume
yaml
apiVersion: v1
kind: Pod
metadata:
name: configmap-volume
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: config-vol
mountPath: /etc/config
volumes:
- name: config-vol
configMap:
name: app-configMount Specific Keys
yaml
volumes:
- name: config-vol
configMap:
name: app-config
items:
- key: app.properties
path: application.properties
- key: logging.conf
path: log.confSecret as Volume
yaml
apiVersion: v1
kind: Pod
metadata:
name: secret-volume
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: secret-vol
mountPath: /etc/secrets
readOnly: true
volumes:
- name: secret-vol
secret:
secretName: app-secretsPersistentVolumeClaim
yaml
apiVersion: v1
kind: Pod
metadata:
name: pvc-demo
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: my-pvcsubPath
Mount specific file/directory without overwriting:
yaml
volumeMounts:
- name: config-vol
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf # Only mount this fileMultiple Mounts
yaml
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: config
mountPath: /etc/config
- name: secrets
mountPath: /etc/secrets
- name: data
mountPath: /data
volumes:
- name: config
configMap:
name: app-config
- name: secrets
secret:
secretName: app-secrets
- name: data
persistentVolumeClaim:
claimName: my-pvcRead-Only Mount
yaml
volumeMounts:
- name: secrets
mountPath: /etc/secrets
readOnly: trueUseful Commands
bash
# Check mounted volumes
kubectl describe pod <pod-name> | grep -A10 "Volumes"
# Verify mount inside pod
kubectl exec <pod-name> -- ls -la /path/to/mount
kubectl exec <pod-name> -- df -h