Sub topic 6
Filesystem and Block volume modes
This chapter assumes you can write a PersistentVolumeClaim (PVC), know what a PersistentVolume (PV) is, and have read the access modes chapter. It covers volumeMode, a field that decides whether a volume reaches your container as a mounted directory or as a raw device.
Two ways to present a volume
A PV or PVC sets spec.volumeMode to one of two values:
Filesystemis the default. Kubernetes formats the volume if it is empty, mounts it, and the container sees a directory.Blockhands the container an unformatted block device. No filesystem is created and nothing is mounted. The application reads and writes raw bytes.
Every example so far has used Filesystem without naming it, because you can omit the field.
When to use Block
Most workloads should stay with Filesystem. Choose Block when the application manages its own on-disk layout and a filesystem layer would only add overhead or get in the way. Examples are some databases and storage engines that want direct device access. Block mode also fits software that formats the device itself.
A raw block claim
This claim asks for a Block volume from the fast class defined in the StorageClass chapter:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: raw-pvc
spec:
accessModes:
- ReadWriteOnce
volumeMode: Block
storageClassName: fast
resources:
requests:
storage: 5GiUsing the device in a Pod
A block claim uses volumeDevices with a devicePath, not volumeMounts with a mountPath:
apiVersion: v1
kind: Pod
metadata:
name: raw-pod
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "ls -l /dev/xvda; sleep 3600"]
volumeDevices:
- name: data
devicePath: /dev/xvda
volumes:
- name: data
persistentVolumeClaim:
claimName: raw-pvcInside the container, /dev/xvda is a block device node. Running ls -l on it shows a b in the first column of the permissions.
Common errors
- Modes must match for binding. A
Blockclaim never binds to aFilesystemPV, and the reverse also fails. The claim staysPending. Checkkubectl describe pvcfor the reason. - Wrong Pod field. Using
volumeMountsfor aBlockclaim, orvolumeDevicesfor aFilesystemclaim, makes the Pod fail to start. - Driver support. Block mode needs a CSI driver that supports it. The AWS EBS CSI driver does. A
hostPathvolume does not. - Data loss on repurposing. If you reuse a raw volume, the application sees whatever bytes are on it. Delete and re-create it, or wipe it, before handing it to a different workload.
Exercise
A PVC with volumeMode: Block stays Pending even though a free 10Gi PV exists with matching size, access mode, and class. What is the most likely cause, and which field must you use in the Pod once it binds?
Show answer
The PV probably uses the default volumeMode: Filesystem, and modes must match for binding. Once a Block claim binds, the Pod must reference it under volumeDevices with a devicePath, not under volumeMounts.