Skip to main content
πŸ“¬ Get weekly Production AI insightsPractical notes on Kubernetes, AI infrastructure and platform engineering. No spam.Subscribe free
Platform Engineering

Kubernetes Pod Security Standards Explained

Enforce pod security with built-in admission control β€” replace PodSecurityPolicy with Pod Security Standards, OPA Gatekeeper, and Kyverno.

Luca Berton

Β· The Production AI Expert

Published Β·1 min read

The PodSecurityPolicy Deprecation

PodSecurityPolicy (PSP) was removed in Kubernetes 1.25. The replacement: Pod Security Standards (PSS) β€” built-in, no extra controllers needed.

Three Security Levels

LevelWhat It AllowsUse Case
PrivilegedEverythingSystem/infra pods (CNI, storage drivers)
BaselineSane defaults, blocks known escalationsGeneral workloads
RestrictedStrictest, runs as non-root, no capabilitiesSecurity-sensitive

Enable Per-Namespace

apiVersion: v1
kind: Namespace
metadata:
  name: production
  labels:
    # Enforce restricted (reject violations)
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: latest
    # Warn on baseline violations (log but allow)
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/audit: restricted
---
apiVersion: v1
kind: Namespace
metadata:
  name: kube-system
  labels:
    # System namespace needs privileged
    pod-security.kubernetes.io/enforce: privileged

What Restricted Blocks

# ❌ This pod will be REJECTED in a restricted namespace
apiVersion: v1
kind: Pod
spec:
  containers:
    - name: app
      image: myapp
      securityContext:
        privileged: true          # ❌ Blocked
        runAsUser: 0              # ❌ Must be non-root
        allowPrivilegeEscalation: true  # ❌ Blocked
      volumeMounts:
        - name: host
          mountPath: /host
  volumes:
    - name: host
      hostPath:                   # ❌ Blocked
        path: /
# βœ… This pod PASSES restricted
apiVersion: v1
kind: Pod
spec:
  securityContext:
    runAsNonRoot: true
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: app
      image: myapp
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop: ["ALL"]
        readOnlyRootFilesystem: true
        runAsUser: 1000

Migration from PSP

# 1. Audit what would break
kubectl label ns production pod-security.kubernetes.io/warn=restricted --dry-run=server

# 2. Check violations
kubectl get events --field-selector reason=FailedCreate -A | grep "violates PodSecurity"

# 3. Fix pods, then enforce
kubectl label ns production pod-security.kubernetes.io/enforce=restricted

Kyverno (Policy-as-Code Alternative)

For more granular control than built-in PSS:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: require-non-root
spec:
  validationFailureAction: Enforce
  rules:
    - name: check-non-root
      match:
        resources:
          kinds: [Pod]
          namespaces: [production]
      validate:
        message: "Containers must run as non-root"
        pattern:
          spec:
            containers:
              - securityContext:
                  runAsNonRoot: true
                  allowPrivilegeEscalation: false

OPA Gatekeeper

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredSecurityContext
metadata:
  name: must-run-as-nonroot
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
    namespaces: ["production"]
  parameters:
    runAsNonRoot: true
    allowPrivilegeEscalation: false

Comparison

FeatureBuilt-in PSSKyvernoOPA Gatekeeper
No extra installβœ…βŒβŒ
Granularity3 levels onlyAny policyAny policy
MutationβŒβœ… (auto-fix)⚠️
Generate resourcesβŒβœ…βŒ
Learning curveLowMediumHigh (Rego)
PerformanceNative (fastest)FastMedium
  1. Start with PSS β€” label all namespaces (baseline for dev, restricted for prod)
  2. Add Kyverno β€” for policies PSS can’t express (image allowlists, label requirements)
  3. Audit mode first β€” warn before enforce
  4. Exempt system namespaces β€” kube-system, monitoring, cert-manager need privileged

Free 30-min Production AI consultation

Book Now