Sub topic 6
How Pod Security Admission works
This chapter assumes you know basic kubectl usage, Pods, Deployments and Namespaces, and what an admission controller is. It explains what Pod Security Admission (PSA) does when a Pod reaches the API server, and where it stops.
What PSA is
Pod Security Admission is a built-in admission controller. It has been enabled by default since Kubernetes 1.23 and reached stable in 1.25. It replaces PodSecurityPolicy, which was removed in 1.25.
PSA does one job. It compares a Pod's spec with one of the three Pod Security Standards profiles (Privileged, Baseline, Restricted) and then allows, warns about, or rejects the Pod. The profiles are fixed. You can't edit them or write custom rules. You choose which profile applies to which namespace, and what happens on a violation.
Where it sits in the request path
PSA is a validating admission controller. It runs after authentication, authorization and any mutating admission, so it sees the Pod as it will be stored. It never changes a Pod. It only accepts it or rejects it.
What PSA checks
PSA reads labels on the Pod's namespace. Each label sets a mode and a level:
- The mode says what PSA does on a violation:
enforcerejects the Pod,auditrecords an annotation in the audit log, andwarnreturns a warning to the user. - The level says which profile to check against:
privileged,baselineorrestricted.
A label has the form pod-security.kubernetes.io/<mode>: <level>. You can set all three modes on one namespace, each with a different level. The next chapter covers the labels in detail.
This namespace rejects Baseline violations and reports Restricted violations as warnings and audit entries:
apiVersion: v1
kind: Namespace
metadata:
name: payments
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restrictedIf a namespace has no labels, PSA uses the cluster defaults. Out of the box those are privileged, so nothing is blocked.
Seeing it work
Apply the namespace above, then try a Pod that uses hostNetwork and a privileged container:
apiVersion: v1
kind: Pod
metadata:
name: privileged-demo
namespace: payments
spec:
hostNetwork: true
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
securityContext:
privileged: trueThe API server refuses it with a Forbidden error that names the violated controls, such as host namespaces (hostNetwork=true) and privileged. The Pod is never stored, so nothing is scheduled.
What PSA checks and when
PSA evaluates Pod create and update requests. A few details matter in practice:
- Most Pod updates are not checked, because only changes to security-relevant fields can matter. Pods that already exist are not evicted when you tighten a label.
- It also checks the Pod template of workload resources, but only in
auditandwarnmodes. Enforcement happens on the Pod itself. - Exemptions can skip checks for certain usernames, runtime classes or namespaces. These are set in the API server configuration.
Limits of the design
PSA is stateless and namespace-scoped. It can't make different decisions for two Pods in one namespace, and it can't grant exceptions to one service account. If you need that, you use separate namespaces or an additional policy tool, covered in a later chapter.
Exercise
A namespace has the labels enforce=baseline and warn=restricted. A user creates a Pod that passes Baseline but breaks one Restricted control. What happens?
Show answer
The Pod is created, because it meets the enforce level. The user receives a warning that names the Restricted control the Pod violates. Nothing is rejected.