SecurityContext
The primary goal of Security Context is to restrict the behavior of untrustworthy containers, shielding the system and other containers from their potential impact.
There are three methods provided by Kubernetes to configure Security Context:
Container-level Security Context: Applied solely to the specified container
Pod-level Security Context: Implemented on all containers and Volume within the Pod
Pod Security Policies (PSP): Applied across all Pods and Volumes within the cluster
The Nitty-Gritty of Container-level Security Context
Container-level Security Context only applies to the assigned container, impacting the Volume not. For instance, setting a container to run in privileged mode can be done like this:
Digging into Pod-level Security Context
Pod-level Security Context is applied to all containers inside a Pod, and it also influences the Volume, including fsGroup and selinuxOptions.
Understanding Pod Security Policies (PSP)
Pod Security Policies (PSP) serve as cluster-level Pod security strategies, automatically setting the Security Context for Pods and Volumes within the cluster.
Operating PSP requires the API Server to enable extensions/v1beta1/podsecuritypolicy
, and to configure the PodSecurityPolicy
admission controller.
Note: Due to a lack of flexibility, an imperfect authentication model, and cumbersome configuration updates, PodSecurityPolicy was officially deprecated in v1.21 and will be removed from the codebase in v1.25. Users currently using PodSecurityPolicy are suggested to migrate to Open Policy Agent.
API Version Comparison Table
Supported Controls
Example
To restrict a container's host port range to 8000-8080, you can do this:
To allow only the use of lvm and cifs etc. flexVolume plugins:
A Closer Look at SELinux
SELinux (Security-Enhanced Linux) is an implementation of mandatory access control. It operates under the principle of least privilege, using Linux Security Modules within the Linux kernel. Emmy award-winning SELinux was primarily developed by the United States National Security Agency, and was released to the open source developer community on December 22, 2000.
The security policy for processes can be set using runcon, while the - Z parameter in ls and ps can inspect the security policy applied to files or processes.
How to Enable or Disable SELinux?
You can edit the / etc/selinux/config file:
Enable: SELINUX=enforcing
Disable: SELINUX=disabled
Or use the command for temporary changes:
Enable: setenforce 1
Disable: setenforce 0
To check SELinux status:
Example
This generates the following HostConfig.Binds
for the docker container:
The appropriate Volume also has SELinux properly set:
Additional Reading
最后更新于