Mastering Kubernetes Pod Configuration: Security Contexts
Reviewing the available options for configuring security contexts. Then creating multiple pods with differing security contexts to observe the effects of each.
Walkthrough
Explain the available Pod-level security context field
Explain the available container-level security context fields
pod-no-security-context.yaml
The pod simply runs a container that sleeps.
Create the Pod and use exec to list the available devices in the container. There are only a minimal number of devices available in the container and none that can do any harm. For the sake of what you will do next, notice there are no block devices. In particular, there is no nvme0n1p1 device that is the host's file system disk.
pod-privileged.yaml
Delete the previous Pod and create a similar Pod that has a privileged container
List the devices available in the container. All of the host devices are available including the host file system disk nvme0n1p1. This could be a major security breach and shows the importance of carefully considering if you should ever use a privileged container.
pod-runas.yaml
Create another pod that includes a Pod security context as well as a container security context. The Pod security context enforces that container processes do not run as root (runAsNonRoot) and sets the user ID of the container process to 1000. The container securityContext sets the container process' user ID to 2000 and sets the root file system to read-only.
Open a shell in the container. Notice that the shell prompt is $ and not # indicating that you are not the root user.
List the running processes in the container. The USER ID is 2000 illustrating that it is not root and that the container security context overrides the setting in the Pod security context when both security contexts include the same field. Whenever possible you should not run as root.
Attempt to create a file in the /tmp directory
Delete the pod resource
Last updated