Enforcing a Read-Only File System
One effective method to maintain container immutability is by ensuring that the pod’s file system remains read-only after startup. This can be implemented through the security context in the pod definition. Consider the following configuration for an Nginx pod:readOnlyRootFilesystem: true field in the security context ensures that the Nginx container starts with a read-only root file system, preventing any unauthorized copying or writing. However, this configuration might disrupt application functionality. For example, deploying the pod as configured above could result in an error because Nginx typically requires write permissions for certain directories.
If you create the pod with this configuration, you may see the following output:
/var/run (to store runtime data) and /var/cache/nginx (for caching). The pod logs will indicate failures when it attempts to write to these directories.
!!! note “Important”
Before enforcing a read-only file system, ensure your applications do not depend on writing to the root file system during runtime.
Using Volumes to Allow Limited Write Access
To resolve these issues, mount volumes on the directories that require write access. In the example below, we use anemptyDir volume since the data does not need to persist after the pod terminates. The updated configuration is as follows:
/var/cache/nginx and /var/run directories inside the container become writable through the mounted volumes, while the rest of the file system remains read-only. Once recreated, the pod should initialize successfully.
Testing the Immutable Container with Privileged Mode
In some cases, you might want to observe the behavior of an immutable container even when it’s running in privileged mode. Although using the privileged flag is generally discouraged, this example demonstrates that the read-only root file system still prevents modifications, even for a privileged container. Create a pod with the configuration below:/proc pseudo file system, such as modifying the swappiness value, can impact the host machine. This example reinforces the importance of avoiding the privileged flag to maintain container immutability.
!!! warning “Security Warning”
Avoid using the privileged flag unless absolutely necessary. Privileged containers can perform actions that inadvertently affect the host system and compromise security.
Best Practices for Container Immutability
To ensure that your containers remain immutable, follow these best practices:| Best Practice | Description |
|---|---|
| Read-Only Root File System | Set containers with a read-only root file system to prevent in-place modifications. |
| Limited Write Volumes | Mount volumes (e.g., emptyDir or persistent volumes) only on directories that require write access. |
| Avoid Privileged Mode | Refrain from using the privileged flag to limit the container’s impact on the host system. |
| Non-Root Containers | Run containers as non-root users whenever possible to minimize risks. |
| Enforce Security Policies | Use Pod Security Policies (PSPs) to enforce immutability and other security best practices. |