How to enforce MustRunAsNonRoot policy in K8S cluster in AKS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Running containers as non-root is a baseline Kubernetes security control that reduces privilege exposure. In AKS, enforcement is typically done with Pod Security Admission labels and optionally Azure Policy for centralized governance. The practical goal is to block pods that run as root unless explicitly exempted.
Understand Current Kubernetes Policy Model
Older PodSecurityPolicy mechanisms are deprecated and removed in modern Kubernetes releases. Current clusters commonly use:
- Pod Security Admission namespace labels,
- Gatekeeper or Azure Policy constraints for policy-as-code.
For non-root enforcement, focus on pod and container security context fields such as runAsNonRoot and runAsUser.
Enforce with Pod Security Admission in AKS
Label namespaces with a Pod Security level that enforces non-root requirements. The restricted profile is the strictest built-in baseline.
This causes non-compliant pods to be denied in that namespace.
Example compliant deployment snippet:
Image must support that user ID. If image hardcodes root-only behavior, pod admission may fail.
Add Azure Policy for Cluster-Wide Governance
In AKS, Azure Policy can audit or deny workloads that violate rules. This is useful when managing many namespaces and teams.
High-level flow:
- enable Azure Policy add-on for AKS,
- assign built-in or custom policies for non-root enforcement,
- monitor compliance and remediation in Azure Policy dashboards.
Policy assignment via Azure CLI is typically done at subscription or resource group scope.
Then assign a policy initiative that includes non-root constraints for Kubernetes workloads.
Validate Enforcement with Test Pods
Use a root pod manifest to verify deny behavior.
Apply it:
A correctly enforced namespace or policy should reject this pod.
Now test a compliant version with non-root UID and verify admission succeeds.
CI and Manifest Guardrails
Add checks in CI so violations are caught before deployment. Tools such as kubectl --dry-run=server, conftest, or policy engines can validate manifests pre-merge.
This prevents policy surprise during release and keeps developer feedback loops short.
Handle Exceptions with Controlled Namespaces
Some legacy workloads may need temporary exceptions. Instead of weakening cluster-wide policy, isolate them in dedicated namespaces and document sunset timelines.
Operational pattern:
- strict enforcement in default workload namespaces,
- explicit exception namespace with stronger monitoring,
- regular review to remove exception workload debt.
This keeps overall posture strong while allowing controlled migration.
Common Pitfalls
A common mistake is setting runAsNonRoot: true without using an image that has a valid non-root user path. The pod still fails at runtime or admission due to user mismatch.
Another issue is applying strict namespace labels to system namespaces unintentionally. Keep platform namespaces separated and label only intended workload namespaces.
Developers also rely only on audit mode and assume enforcement. Audit and warn modes do not block workloads. Use enforce mode where required.
Summary
- Enforce non-root execution in AKS with Pod Security Admission and governance policies.
- Use namespace labels such as
enforce=restrictedfor built-in admission checks. - Configure pod and container security contexts explicitly for non-root operation.
- Validate with both failing and compliant test manifests.
- Add CI policy checks to catch security context issues before deployment.
Related reading
- How to ensure kubernetes cronjob does not restart on failure
- How to enter a pod as root?
- How to estimate Kubernetes Resources for a Pod
- How to exec into a container and view file if container is in CrashLoopBackOff state
- How to execute a sql script file in a Kubernetes Pod?
- How to exempt a directory when using readOnlyRootFilesystem in kubernetes?
- How to explicitely define an Endpoint of an Kubernetes Service
- How to expose a headless Kafka service for a StatefulSet externally in Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.