How to exempt a directory when using readOnlyRootFilesystem in kubernetes?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When readOnlyRootFilesystem is enabled, the container root layer cannot be written. You cannot mark a subdirectory inside that same root layer as writable directly. The correct approach is to mount a writable volume at the path your app needs, effectively replacing that directory with writable storage.
How Exemption Really Works
In Kubernetes, the root filesystem and mounted volumes are separate filesystems. With root set to read-only, write attempts fail unless they target a mounted volume that allows writes. So directory exemption is done by mounting emptyDir, persistent volume, or projected storage at that specific path.
Base Security Context with Read-Only Root
Start with a strict container security context.
At this stage, any write to root paths such as /var/tmp fails unless a writable volume is mounted.
Exempt One Directory with emptyDir
For scratch space or cache files that can be lost on restart, use emptyDir.
Now /tmp is writable while root stays read-only.
Exempt Persistent Data Directory
If data must survive pod restart, mount a persistent volume claim.
This is the preferred pattern for stateful workloads.
Use subPath for Narrower Mount Scope
When one shared volume backs multiple paths, subPath can isolate directories.
Be careful that subPath directories exist and permissions are correct, or container startup can fail.
Permissions and Ownership
Writable volume mounts often fail due to uid and gid mismatch. If your app runs as non-root, set pod fsGroup or pre-create volume permissions in init workflows.
Also ensure your app writes only to intended mount paths. Hidden writes to default locations such as /root or /var/run can still fail under read-only root.
Validate at Runtime
After deployment, verify behavior quickly:
First command should succeed. Second should fail, confirming root remains read-only.
Deployment Policy and Validation
If your cluster enforces Pod Security standards or admission policies, verify that writable mounts and security settings remain compliant. A useful practice is adding a policy check in CI with your chosen admission tooling and then running an integration smoke test that writes only to approved mount paths. This confirms your manifest both satisfies security controls and still meets runtime write requirements.
Common Pitfalls
A common mistake is expecting Kubernetes to whitelist a writable path inside root without a volume mount. That is not supported. Another issue is mounting writable storage but forgetting application configuration still points writes to old read-only paths. Teams also overlook permissions on mounted volumes, especially with non-root containers, causing false assumptions that read-only root is broken. Finally, mounting over system-critical paths can hide required files, so choose mount targets carefully.
Summary
- You cannot directly exempt a root subdirectory without mounting a volume.
- Keep
readOnlyRootFilesystemenabled for baseline hardening. - Mount
emptyDirfor ephemeral writes and PVCs for persistent writes. - Align user permissions with mounted storage ownership.
- Test writable and non-writable paths to verify security intent.
Related reading
- How to explicitely define an Endpoint of an Kubernetes Service
- How to expose a headless Kafka service for a StatefulSet externally in Kubernetes
- How to expose a headless service for a StatefulSet cassandra cluster externally in Kubernetes
- How to expose a Kubernetes service on a specific Nodeport?
- How to expose Kafka from Docker to the outside world?
- How to fix dial unix /var/run/docker.sock connect permission denied when group permissions seem correct?
- How to find out the currently logged-in user in Spring Boot?
- How to find out the MySQL root password

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.