This article explores Kubernetes security contexts and demonstrates updating pod configurations to meet specific security requirements.
In this article, we explore various Kubernetes security context scenarios and demonstrate how to update pod configurations to meet specific security requirements. You’ll learn how to check the running user inside pods, update configurations to run processes as non-root or root with additional capabilities, and understand container-level versus pod-level security context inheritance.
Determining the User for Multiple Containers in a Pod
When dealing with pods that contain multiple containers, it’s important to understand how user contexts are inherited. Consider the following pod definition from the multi-pod.yaml file:
The pod-level security context sets runAsUser to 1001.
The web container defines its own securityContext with runAsUser: 1002.
Since the container-level security context takes precedence, the processes in the web container run as user 1002. The sidecar container, which lacks a container-specific security context, inherits the pod-level setting, so its processes run as user 1001.The final configuration is provided below for reference:
Updating the Ubuntu Sleeper Pod to Run as Root with SYS_TIME Capability
To modify the Ubuntu sleeper pod so that:
The process runs as the default root user.
The pod is granted the SYS_TIME capability,
follow these steps:
Edit the Pod Configuration
Open the current configuration file (e.g., ubuntu-sleeper.yaml), and remove any pod-level securityContext that enforces a non-root user.
Add Capabilities at the Container Level
Under the container specification responsible for running the sleep command, add a securityContext that includes the required capability:
To enhance the security context further by adding the NET_ADMIN capability in addition to SYS_TIME, follow these steps:
Modify the Container’s Security Context
Open the ubuntu-sleeper.yaml file and update the securityContext under the container section to include both capabilities:
Apply the Changes
Save the file and reapply the pod configuration by deleting the current pod and applying the updated configuration:
Copy
Ask AI
kubectl delete pod ubuntu-sleeper --forcekubectl apply -f ubuntu-sleeper.yaml
Verify the Update
Optionally, check that the pod is running with the updated capabilities:
Copy
Ask AI
kubectl get pods
A sample console interaction may look like this:
Copy
Ask AI
controlplane ~ ➜ vi ubuntu-sleeper.yamlcontrolplane ~ ➜ kubectl delete pod ubuntu-sleeper --forceWarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "ubuntu-sleeper" force deletedcontrolplane ~ ➜ kubectl apply -f ubuntu-sleeper.yamlpod/ubuntu-sleeper createdcontrolplane ~ ➜ kubectl get podsNAME READY STATUS RESTARTS AGEubuntu-sleeper 1/1 Running 0 10s
This completes the guide on updating Kubernetes pod configurations with various security contexts and capabilities. For more detailed information on Kubernetes security practices, see the Kubernetes Documentation.Happy securing!