In this lesson, we explore pod security in Kubernetes by examining a sample pod definition and discussing how Pod Security Policies (PSPs) were used to restrict insecure configurations. Although PSPs have been deprecated in favor of Pod Security Admission and Pod Security Standards (available since Kubernetes 1.25), understanding PSPs is still useful—especially when working with legacy clusters.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Sample Pod Definition
Below is a sample pod definition that creates a pod running an Ubuntu container. The configuration includes permissive settings often unsuitable for production environments:- The
privilegedflag is set toTrue, granting container processes elevated privileges. - The container runs as root (
runAsUser: 0). - It adds specific capabilities like
CAP_SYS_BOOT. - A HostPath volume is used, exposing part of the host’s filesystem.
The Role of Pod Security Policies
Pod Security Policies were designed to validate pod creation requests against a set of pre-configured security rules. When the PSP admission controller is enabled, it intercepts pod creation requests and rejects those that do not comply with the defined policies. For example, a pod configured with theprivileged flag set to True can be denied by an appropriately configured PSP.
When a violation is detected, the system rejects the pod creation request and returns an error message, preventing insecure pods from running.
Enabling PSP on the API Server
PSP functions as an Admission Controller. To enable it, add thePodSecurityPolicy plugin to the API server’s list of admission plugins. An example snippet from the API server’s startup command is shown below:
Defining a Pod Security Policy
To enforce security, you can create a PSP that, for instance, disallows pods with the privileged flag enabled. Start by reviewing the original pod definition for context:privileged flag.
You can further customize the policy to enforce stricter measures. For instance, the following definition disallows running as root, mandates dropping the CAP_SYS_BOOT capability, adds a default capability, and only permits specific volume types:
It is important to understand that while PSPs can add default values to pod definitions, the new Pod Security Admission and Pod Security Standards do not support this mutating behavior.
How the Admission Controller Works
When a pod creation request is submitted, the PSP admission controller:- Queries all available Pod Security Policy objects.
- Validates the pod against the defined rules.
- Denies requests that conflict with the established policies (e.g., a pod using a disallowed
privilegedflag).
default Service Account is used if none is specified. Then, you need to create a Role and RoleBinding to grant the Service Account permission to use the specific PSP. For example:
Summary and Challenges
To summarize:- Pod Security Policies validate and potentially modify pod definitions based on strict security rules.
- Enabling PSPs requires changes both at the API server level (by enabling the admission controller) and at the cluster level (by creating the necessary PSP objects and RBAC permissions).
- An incomplete setup can result in the unintentional denial of all pod creation requests.
- Binding PSP access to specific Service Accounts might interfere with the functionality of controllers (like Deployments) if not correctly configured.
Due to the complexities and effort required to manage PSP configurations, they were deprecated in Kubernetes 1.21 and removed entirely in 1.25. The newer Pod Security Admission and Pod Security Standards provide a more streamlined approach to securing your cluster.
