Sub topic 12
Moving from PodSecurityPolicy
This chapter assumes you know the three Pod Security Standards profiles, the pod-security.kubernetes.io namespace labels, and basic RBAC. It covers how to replace PodSecurityPolicy (PSP) with Pod Security Admission (PSA) on a cluster that still uses PSP.
Why the move is required
PSP was deprecated in Kubernetes 1.21 and removed in 1.25. A cluster on 1.25 or later has no PSP admission plugin. Clusters on older versions should migrate before upgrading, because the upgrade removes PSP enforcement and leaves only what PSA enforces.
PSA is not a drop-in replacement. The two systems differ in how they work:
| Aspect | PodSecurityPolicy | Pod Security Admission |
|---|---|---|
| Scope | Chosen per user or service account through RBAC | Chosen per namespace through labels |
| Policy content | Custom rules you write | Three fixed profiles |
| Mutation | Can set defaults on pods | Validates only, never changes pods |
| Rollout aids | None | `warn`, `audit`, and server-side dry run |
Gaps to close first
Before you switch, check which of your PSPs rely on features PSA does not have:
- Mutation. If a PSP injects
runAsUser,fsGroup,seccompProfile, or default capabilities, PSA will not do that. Put these fields in the pod specs, or use a mutating webhook such as Kyverno. - Custom rules. Limits on
allowedHostPaths, host port ranges, or specific volume types have no PSA equivalent. Use a separate policy tool for them. - Per-identity rules. A PSP can give one service account more freedom than another in the same namespace. PSA cannot. Either split the workloads into separate namespaces or use PSA exemptions.
Migration steps
The Kubernetes documentation describes this order. The diagram shows it.
- Remove the reliance on PSP-only features listed above.
- Pick a profile for each namespace. Start from what the pods need, not from what the PSP currently allows.
- Add
warnandauditlabels first. Users see warnings and the audit log records violations, but nothing is blocked. - Run a server-side dry run to see which running pods would violate the target level:
kubectl label --dry-run=server --overwrite ns --all \
pod-security.kubernetes.io/enforce=baseline- Fix the workloads that appear in the warnings. If you chose a level that is too strict, go back to step 2.
- Add the
enforcelabel to each namespace. - Review how new namespaces get created. A namespace with no labels falls back to the cluster default, and that default is
privilegedunless you configure otherwise.
Switching PSP off safely
Do not disable the PSP plugin while PSPs still restrict pods. Once PSA enforces the right levels, replace your restrictive PSPs with one permissive PSP and bind it to all service accounts. Then PSA is the only effective control. After a period without problems, disable the PSP admission plugin on the API server. On 1.25 and later this step is already done for you.
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: allow-all
spec:
privileged: true
allowPrivilegeEscalation: true
allowedCapabilities: ["*"]
volumes: ["*"]
hostNetwork: true
hostPorts:
- min: 0
max: 65535
hostIPC: true
hostPID: true
runAsUser:
rule: RunAsAny
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAnyThis manifest only works on clusters before 1.25, because it uses the removed policy/v1beta1 API.
Common mistakes
- Assuming that a pod accepted today will be accepted after the switch. PSP mutation can hide missing fields.
- Labeling only application namespaces. System namespaces such as
kube-systemoften needprivilegedor an exemption in the AdmissionConfiguration. - Enforcing
restrictedeverywhere at once. Start withbaselineand move up namespace by namespace.
Exercise
Your PSP sets runAsUser: 1000 on all pods in a namespace through its MustRunAs default behavior. You move the namespace to PSA with enforce: restricted. Pods start failing. Why, and what are two ways to fix it?
Show answer
PSA only validates and never mutates, so the user ID that PSP used to inject is no longer set. The Restricted profile requires runAsNonRoot: true, so pods without a non-root user are rejected. Fix it by adding runAsNonRoot: true and runAsUser: 1000 to the pod specs or Deployment templates, or by using a mutating webhook (for example Kyverno) that sets those fields.