securityContext configurations in Kubernetes pods and containers, demonstrating how to control process ownership and Linux capabilities. You’ll see how to:
- Determine the user that runs a process inside a container
- Override container user IDs with
runAsUser - Grant specific capabilities (e.g.,
SYS_TIME,NET_ADMIN)
1. Which user executes the sleep process in the Ubuntu Sleeper pod?
Run whoami locally and inside the container:
sleep process runs as root by default.
2. Edit the Ubuntu Sleeper pod to run the process as UID 1010
- Export the existing Pod manifest:
- In
ubuntu-sleeper.yaml, add a container-levelsecurityContext: - Delete and recreate the Pod:
- Verify inside the container:
Using
--force deletes the Pod immediately. In production clusters, prefer a graceful rollout (e.g., updating a Deployment).sleep process now runs as UID 1010.
3. Which user starts processes in the web container of multi-pod.yaml?
web container runs as 1002.
4. Which user starts processes in the sidecar container?
Since the sidecar container has no runAsUser block, it inherits from the Pod:
| Container | runAsUser |
|---|---|
| web | 1002 |
| sidecar | 1001 |
sidecar container runs as 1001.
5. Update Ubuntu Sleeper to run as root and add the SYS_TIME capability
- Remove any
runAsUserlines inubuntu-sleeper.yaml. - Under the container’s
securityContext, add theSYS_TIMEcapability: - Apply the changes:
Granting
SYS_TIME allows processes to modify the system clock. Only use this capability if absolutely necessary.6. Add the NET_ADMIN capability to the Ubuntu Sleeper pod
Extend the same securityContext to include both capabilities: