This article provides practical examples and guidance on managing security contexts for pods and containers in Kubernetes.
In this article, we address common questions about managing security contexts for pods and containers in Kubernetes. Learn how to verify which user is executing a process, modify pod configurations to run under a specific user ID, and adjust container capabilities. These step-by-step examples are designed to help you secure your containerized applications effectively.
1. Checking the User Running the Sleep Process in the Ubuntu Sleeper Pod
To verify which user is running the sleep process in your Ubuntu sleeper pod, follow these steps:
List the running pods to identify the pod name.
Execute the command inside the pod using the whoami command to check the current user.
Execute the following commands:
Copy
Ask AI
# List all pods; look for the pod named "ubuntu-sleeper"kubectl get pods# Execute a command within the pod to display the current userkubectl exec ubuntu-sleeper -- whoami
The output confirms that the sleep process is running as the root user:
Copy
Ask AI
controlplane ~ ➜ whoamirootcontrolplane ~ ➜ kubectl get podNAME READY STATUS RESTARTS AGEubuntu-sleeper 1/1 Running 0 7ms58s
If you see root as the output, it indicates that no user override has been set, and the container is running with root privileges.
Consider a pod definition file named multi-pod.yaml that includes multiple containers with different security contexts set at the pod and container levels. Below is the configuration snippet:
After these steps, verify that the pod runs with both the SYS_TIME and NET_ADMIN capabilities enabled.
You have now successfully adjusted the security contexts for your Ubuntu sleeper pod. This guide covered verifying container users, configuring pods to run with specific user IDs, and modifying container capabilities to enhance security.