securityContext examples. For each task you’ll find the commands and YAML edits used to complete it, plus validation steps. Use these patterns to manage process UIDs and Linux capabilities for containers in a cluster.
Prerequisites:
- kubectl configured to the target cluster
- A running
ubuntu-sleeperPod (or the ability to create/replace it) - Basic familiarity with editing YAML files
1) Which user executes the sleep process in the ubuntu-sleeper Pod?
To determine the user a process runs as inside a container, exec into the Pod and run whoami (or id -u for numeric UID):
2) Edit the ubuntu-sleeper Pod to run the sleep process with UID 1010
Goal: change the Pod so the container processes run as UID 1010. We’ll add a pod-level securityContext.runAsUser to the manifest and recreate the Pod.
Steps:
- Export the current Pod manifest to a file:
- Edit
ubuntu-sleeper.yamland add the pod-levelsecurityContextunderspec::
- Replace the running Pod. This example deletes the Pod immediately then applies the edited manifest:
Immediate deletion with
--force does not wait for the container to terminate cleanly; the process may continue running on the node for a short time. Use with care.- Validate the UID inside the recreated Pod:
3) Given multi-pod.yaml, with what user are processes in the web container started?
Consider this multi-pod manifest excerpt:
- The Pod-level
securityContextsetsrunAsUser: 1001. - The
webcontainer defines a container-levelsecurityContextwithrunAsUser: 1002, which overrides the Pod-level value for that container.
web container: 1002
4) With what user are processes in the sidecar container started?
Since the sidecar container has no container-level securityContext, it inherits the Pod-level runAsUser: 1001.
Answer for sidecar container: 1001
Quick reference table:
5) Update ubuntu-sleeper to run as root and add the SYS_TIME capability
Goal: Ensure the container runs as root (default) and grants the SYS_TIME capability.
Approach:
- Remove any
runAsUser(root is default). - Add
SYS_TIMEto the containersecurityContext.capabilities.addlist.
ubuntu-sleeper.yaml so the container section contains:
6) Add the NET_ADMIN capability as well
To grant both SYS_TIME and NET_ADMIN, extend the add list:
Inspecting effective capabilities inside the container
The defaultubuntu image may not include tools to inspect capabilities. Two common approaches:
-
Install
libcap2-bininside theubuntucontainer and runcapsh --print:- Exec into the container:
kubectl exec -it ubuntu-sleeper -- bash - Install tools:
apt-get update && apt-get install -y libcap2-bin - Run:
capsh --print
- Exec into the container:
- Run a debug/utility container that already contains capability tools and mount the same namespaces if needed.
If you only need to verify capability granting, consider using a lightweight debug image (for example
nicolaka/netshoot) that includes diagnostic utilities. This avoids installing packages inside a running production container.Links and references
- Kubernetes securityContext docs
- Kubernetes API - PodSpec securityContext
- Capabilities(7) — Linux manual page