Before using pod security admission, understand Kubernetes Pod Security Standards. These standards define different isolation levels for pods. They let you define how you want to restrict the behavior of pods in a clear, consistent fashion. Kubernetes offers a built-in pod security admission controller to enforce the pod security standards. Pod security restrictions are applied at the namespace level when pods are created.
The pod security standard defines three security policy levels:
Level |
Description |
---|---|
privileged |
Unrestricted policy, providing the widest possible level of permissions, typically aimed at system- and infrastructure-level workloads managed by privileged, trusted users, such as CNIs and storage drivers. |
baseline |
Minimally restrictive policy that prevents known privilege escalations, typically targeted at non-critical workloads. This policy disables capabilities such as hostNetwork and hostPID. |
restricted |
Heavily restricted policy, following current Pod hardening best practices. |
Pod security admission is applied at the namespace level. The controller restricts the security context and other parameters in the pod or container in the namespace. The privileged policy does not verify the securityContext field of the pod and container. The baseline and restricted policies have different requirements on securityContext. For details, see Pod Security Standards.
Setting security context: Configure a Security Context for a Pod or Container
Kubernetes defines three types of labels for pod security admission (see Table 2). You can set these labels in a namespace to define the pod security standard level to be used. However, do not change the pod security standard level in system namespaces such as kube-system. Otherwise, pods in the system namespace may be faulty.
Mode |
Target Object |
Description |
---|---|---|
enforce |
Pods |
Policy violations will cause the pod to be rejected. |
audit |
Workloads (such as Deployment and job) |
Policy violations will trigger the addition of an audit annotation to the event recorded in the audit log, but are otherwise allowed. |
warn |
Workloads (such as Deployment and job) |
Policy violations will trigger a user-facing warning, but are otherwise allowed. |
Pods are often created indirectly, by creating a workload object such as a Deployment or job. To help catch violations early, both the audit and warning modes are applied to the workload resources. However, the enforce mode is applied only to the resulting pod objects.
You can label namespaces to enforce pod security standards. Assume that a namespace is configured as follows:
apiVersion: v1 kind: Namespace metadata: name: my-baseline-namespace labels: pod-security.kubernetes.io/enforce: privileged pod-security.kubernetes.io/enforce-version: v1.25 pod-security.kubernetes.io/audit: baseline pod-security.kubernetes.io/audit-version: v1.25 pod-security.kubernetes.io/warn: restricted pod-security.kubernetes.io/warn-version: v1.25 # The label can be in either of the following formats: # pod-security.kubernetes.io/<MODE>: <LEVEL> # pod-security.kubernetes.io/<MODE>-version: <VERSION> # The audit and warn modes inform you of which security behaviors are violated by the load.
Namespace labels indicate which policy level to apply for the mode. For each mode, there are two labels that determine the policy used:
Optional, which pins the policy to a given Kubernetes version.
If pods are deployed in the preceding namespace, the following security restrictions apply:
If you use pod security policies in a cluster earlier than v1.25 and need to replace them with pod security admission in a cluster of v1.25 or later, follow the guide in Migrate from PodSecurityPolicy to the Built-In PodSecurity Admission Controller.