Skip to main content
This guide walks through practical Kubernetes 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-sleeper Pod (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):
Example output:
Answer: root

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:
  1. Export the current Pod manifest to a file:
  1. Edit ubuntu-sleeper.yaml and add the pod-level securityContext under spec::
  1. 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.
  1. Validate the UID inside the recreated Pod:
Expected output:

3) Given multi-pod.yaml, with what user are processes in the web container started?

Consider this multi-pod manifest excerpt:
  • The Pod-level securityContext sets runAsUser: 1001.
  • The web container defines a container-level securityContext with runAsUser: 1002, which overrides the Pod-level value for that container.
Answer for 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_TIME to the container securityContext.capabilities.add list.
Edit ubuntu-sleeper.yaml so the container section contains:
Apply the change:
Validate Pod recreation:

6) Add the NET_ADMIN capability as well

To grant both SYS_TIME and NET_ADMIN, extend the add list:
Apply the update:
Confirm Pod is created:

Inspecting effective capabilities inside the container

The default ubuntu image may not include tools to inspect capabilities. Two common approaches:
  • Install libcap2-bin inside the ubuntu container and run capsh --print:
    1. Exec into the container: kubectl exec -it ubuntu-sleeper -- bash
    2. Install tools: apt-get update && apt-get install -y libcap2-bin
    3. Run: capsh --print
  • 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.

Watch Video