Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

AppArmor in Kubernetes: confine a Pod with a profile

By updated

Improve this page

AppArmor limits what a container can reach on the node, files, capabilities and network, even if its process runs as root. Where RBAC controls access to Kubernetes resources, AppArmor controls access to the system that runs them. This guide covers how to apply it in Kubernetes; for the Linux basics, see the Linux AppArmor guide.

AppArmor has a twin that it does not replace: seccomp filters system calls, whereas AppArmor filters resources. The two apply at the same time, and the section "Combine AppArmor and seccomp" shows how.

AppArmor belongs to the System Hardening domain of the CKS certification, which weighs 10% of the exam. Three skills are expected there and structure this guide: apply a profile through securityContext, know what RuntimeDefault covers, and tell the current syntax, valid since 1.30, from the annotations it replaces.

What you will learn

  • Check that AppArmor is active on a node with aa-enabled and aa-status
  • Load a profile on a node with apparmor_parser -r -W
  • Apply a profile to a Pod or a container through securityContext.appArmorProfile, choosing between the Unconfined, RuntimeDefault and Localhost types
  • Recognize the legacy annotation syntax, deprecated since 1.30
  • Check that a profile is really applied with /proc/1/attr/current
  • Combine AppArmor and seccomp, and know what privileged: true cancels

Prerequisites

  • Kubernetes 1.30+ cluster for securityContext.appArmorProfile (the field is stable since 1.31)
  • Linux nodes with AppArmor enabled (aa-enabled answers Yes)
  • kubectl access with the right to create pods

The second point is not a formality, and it rules out most labs. On a cluster whose nodes are containers, such as kind or k3d, AppArmor is not available, and the kubelet does not just ignore it: it rejects the Pod.

Output of kubectl describe pod
Status: Failed
Reason: AppArmor
Message: Pod was rejected: Cannot enforce AppArmor: AppArmor is not enabled on the host

The message is the same whether the requested profile is RuntimeDefault or a named profile, and it also shows up as a Warning event. If you meet it, your manifest is not the problem: the node is. This page therefore needs a virtual machine, whereas seccomp, the next lesson, can be practised on a local cluster.

AppArmor, access control profiles

AppArmor restricts a process's access to files, directories, capabilities and network calls through declarative profiles loaded into the kernel.

Check AppArmor on the nodes

AppArmor is a feature of the node's kernel, not of Kubernetes: if the module is not active, no manifest will make it appear. Always start with this check, before even writing a profile. The file /sys/kernel/security/apparmor/profiles is the most reliable source, and it is the one the kubelet reads to decide whether to accept a Pod that asks for a Localhost profile.

Terminal window
# On the node (via ssh or node debug)
aa-enabled # answers Yes or No
aa-status # List the loaded profiles
apparmor_parser -V # Version
# Available profiles
ls /etc/apparmor.d/
cat /sys/kernel/security/apparmor/profiles

On a Debian or Ubuntu distribution, aa-enabled answers Yes out of the box. On RHEL, Rocky or AlmaLinux, the answer will be No: these distributions use SELinux, and the AppArmor profiles of this page do not carry over.

Structure of an AppArmor profile

An AppArmor profile reads line by line as a list of paths followed by permissions: r read, w write, x execute, i inherit the current profile. Anything not explicitly allowed is denied. The two deny lines of the example are therefore redundant on paper, but they make the intent readable and take precedence over a more permissive rule inherited from an #include.

#include <tunables/global>
profile k8s-app-readonly flags=(attach_disconnected) {
#include <abstractions/base>
# Allow read/execute on the binary
/usr/bin/myapp rix,
# Read-only on /etc
/etc/** r,
# Read/write on /tmp only
/tmp/** rw,
# Forbid all network access
deny network,
# Forbid ptrace (blocks introspection tools)
deny ptrace,
}

Load a profile on the node

Kubernetes never loads an AppArmor profile for you: it only asks the runtime to apply a profile that is already present in the node's kernel. This step must therefore be repeated on every eligible node, and replayed after each reboot unless you dropped the file in /etc/apparmor.d/ (in that case, the apparmor service reloads it at startup).

Terminal window
# -r replaces the profile if it is already loaded, -W updates the cache
sudo apparmor_parser -r -W /etc/apparmor.d/k8s-app-readonly
# Check that it is loaded
sudo aa-status | grep k8s-app-readonly

A profile loads in enforce mode by default. To load it in observation mode, add flags=(complain) to its declaration or pass -C to apparmor_parser.

Apply AppArmor to a Pod, current syntax (v1.30+)

Since Kubernetes 1.30, AppArmor is configured through securityContext.appArmorProfile:

pod-apparmor-v130.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-apparmor
spec:
securityContext:
appArmorProfile:
type: Localhost
localhostProfile: k8s-app-readonly # Name of the profile loaded on the node
containers:
- name: app
image: nginx:1.31-alpine@sha256:72ba65eb42c10344912a84ff42408db7d34f2feb642204570ab8fc5ffd29f1d3
securityContext:
appArmorProfile: # Can be overridden per container
type: RuntimeDefault # Use the runtime's default profile

The three types available for appArmorProfile.type:

TypeMeaning
UnconfinedNo AppArmor profile (not recommended)
RuntimeDefaultThe runtime's default profile (its name varies with the runtime)
LocalhostA specific profile loaded on the node

The exact name of the RuntimeDefault profile depends on the runtime installed on the node. With containerd, the Kubernetes documentation shows cri-containerd.apparmor.d in the output of /proc/1/attr/current; docker-default is the historical name on the Docker side. Never hard-code this name in a manifest: ask for RuntimeDefault and let the runtime resolve it.

Pod vs container priority

AppArmor and seccomp can be set at Pod level and at container level. The container field always wins:

MechanismPod levelContainer levelPriority
Seccompspec.securityContext.seccompProfilespec.containers[].securityContext.seccompProfileContainer > Pod
AppArmorspec.securityContext.appArmorProfilespec.containers[].securityContext.appArmorProfileContainer > Pod

This lets you apply a default profile to the Pod and override it for a specific container (init container, sidecar).

Legacy syntax, annotations (before v1.30)

You will still meet this syntax in existing repositories and in exam questions. Two differences matter: the annotation goes on the Pod metadata and not in securityContext, and it explicitly names the targeted container in its key, which rules out any declaration at the level of the whole Pod.

pod-apparmor-legacy.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-apparmor-legacy
annotations:
# Format: container.apparmor.security.beta.kubernetes.io/<container-name>
container.apparmor.security.beta.kubernetes.io/app: localhost/k8s-app-readonly
# Other possible values: runtime/default or unconfined
spec:
containers:
- name: app
image: nginx:1.31-alpine@sha256:4a73073bd557c65b759505da037898b61f1be6cbcc3c2c3aeac22d2a470c1752

The container.apparmor.security.beta.kubernetes.io/ annotation has been deprecated since v1.30 and will be removed in a future version. On new clusters, use securityContext.appArmorProfile. You will still come across it in older manifests: know how to read it and convert it.

Setting both forms on the same Pod is not a transition solution. If their values differ, the API server rejects the Pod at creation (apparmor type in annotation and field must match), and neither one takes precedence. If they are identical, the Pod is accepted. To migrate, replace the annotation with the field instead of keeping both.

Combine AppArmor and seccomp

The two are complementary mechanisms, they apply at the same time, and that is how they are set in production. The manifest below declares both, plus the three settings of securityContext that always go with them:

pod-hardened.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-hardened
spec:
securityContext:
seccompProfile:
type: RuntimeDefault # Seccomp: filter the syscalls
runAsNonRoot: true
runAsUser: 1000
containers:
- name: app
image: nginx:1.31-alpine@sha256:4a73073bd557c65b759505da037898b61f1be6cbcc3c2c3aeac22d2a470c1752
securityContext:
appArmorProfile:
type: RuntimeDefault # AppArmor: restrict file/network access
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]

This Pod applies defense in depth. Each line covers a different surface, and none replaces the others:

  • Seccomp RuntimeDefault: about 44 dangerous syscalls blocked out of more than 300
  • AppArmor RuntimeDefault: the runtime's profile (cri-containerd.apparmor.d with containerd)
  • capabilities: drop: ALL: zero Linux capabilities
  • readOnlyRootFilesystem: true: immutable filesystem
  • image referenced by digest: the executed content can no longer change under the same tag

This cancellation can be observed directly, with the same runtime as the cluster's. A normal container returns Seccomp: 2 and an AppArmor profile docker-default (enforce); the same container started with --privileged returns Seccomp: 0 and unconfined, with the default profile. No error message is emitted: the protection disappears silently.

Check that the profiles are applied

Never rely on the manifest to conclude that a profile is active. The quickest command is the one documented by Kubernetes: read the attribute of the container's process 1 from the Pod itself, with no need for SSH access to the node.

Terminal window
# The simplest way: read the PID 1 attribute from inside the container
kubectl exec pod-hardened -- cat /proc/1/attr/current
# Expected: cri-containerd.apparmor.d (enforce) or k8s-app-readonly (enforce)

If the Pod has no shell, or if you are investigating a container that has already stopped, you have to go through the node:

Terminal window
# Check the active AppArmor profiles on a node
ssh node01
sudo aa-status
# Confirm a container's profile (via its PID)
# 1. Find the container's PID on the node
crictl inspect <container-id> | jq '.info.pid'
# 2. Read the profile
cat /proc/<pid>/attr/current
# Test that a profile really blocks (complain mode → enforce)
sudo aa-complain /etc/apparmor.d/k8s-app-readonly # Logs without blocking
sudo aa-enforce /etc/apparmor.d/k8s-app-readonly # Actually blocks

The suffix in parentheses is what counts: (enforce) means violations are denied, (complain) that they are usually logged without being blocked. A profile left in complain after a tuning phase gives the illusion of hardening: most forbidden accesses go through.

Two exceptions, tested on Kubernetes v1.37.2: a deny rule also applies in complain mode, without leaving any trace in the log, so complain mode does not let you spot what it blocks. And if the profile contains no signal rule, the runtime can no longer stop the container.

Enforce at scale with Pod Security Admission

The restricted profile of Pod Security Admission does not mutate Pods: it injects no profile, it rejects those that explicitly declare Unconfined. The detail of this mechanism on the seccomp side, along with the kubelet's seccompDefault setting, lives on the seccomp page.

namespace-pss.yaml
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/audit-version: latest

The Pod Security Standards also frame AppArmor: with restricted, only RuntimeDefault and Localhost are allowed. Use Kyverno or Gatekeeper if you want to go further, for example to explicitly require a Localhost profile or to manage exceptions per namespace:

kyverno-require-apparmor.yaml
apiVersion: policies.kyverno.io/v1
kind: ValidatingPolicy
metadata:
name: require-apparmor-profile
spec:
validationActions: [Deny]
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
validations:
- expression: >-
object.spec.containers.all(c,
(has(c.securityContext) && has(c.securityContext.appArmorProfile) &&
c.securityContext.appArmorProfile.type in ['RuntimeDefault', 'Localhost'])
||
(has(object.spec.securityContext) && has(object.spec.securityContext.appArmorProfile) &&
object.spec.securityContext.appArmorProfile.type in ['RuntimeDefault', 'Localhost'])
)
message: "Every container must declare an appArmorProfile of type RuntimeDefault or Localhost."

The CEL expression first tests the container's securityContext, then the Pod's: this is exactly the order of precedence that Kubernetes applies. It deliberately does not read the deprecated annotations: the policy requires the appArmorProfile field, the syntax to adopt.

Troubleshooting

The first two rows of this table describe the same oversight seen from two different places: the profile is not loaded on the node where the Pod was placed. Always check the landing node (kubectl get pod -o wide) and not a random node, because a heterogeneous cluster may have the profile on only part of the fleet.

SymptomLikely causeFix
Pod stays PendingAppArmor profile does not exist on the nodeaa-status on the node, check the profile name
Error: failed to create containerd taskProfile not loadedapparmor_parser -r /etc/apparmor.d/<profile>
Application denied with no clear messageProfile too strict, or a deny rule (it also blocks in complain mode, without a trace in the log)Switch the profile to complain mode, read dmesg | grep -i apparmor and journalctl -k; if the denial persists without a trace, look for a deny rule in the profile
The container no longer stops when Kubernetes asks it toThe profile contains no signal rule: the runtime can no longer send it a signalAdd a signal rule to the profile, then reload it with apparmor_parser -r
Pod rejected with "apparmor type in annotation and field must match"Deprecated annotation and securityContext.appArmorProfile set together with different valuesAt creation, the API server requires both to be identical: remove the annotation and keep only the field
aa-enabled returns noAppArmor not enabled in the kernelCheck GRUB_CMDLINE_LINUX="apparmor=1 security=apparmor"

Limitations

These five points explain why AppArmor is rarely deployed everywhere in a real cluster. Points 1, 2 and 4 come from the infrastructure: the node's system, its runtime and the access you are given. Points 3 and 5 come from your own profiles and manifests. Remember the third above all: as soon as you leave RuntimeDefault, you add a profile to maintain on every node, and this operational debt often costs more than the security gain it brings.

  • Linux only: AppArmor does not work on Windows nodes, nor on a node whose kernel does not enable it
  • Runtime support: the RuntimeDefault profile depends on the node's containerd or CRI-O implementation
  • Named profile = manual distribution: it must be loaded on every node where the Pod can be scheduled, which is expensive in a heterogeneous cluster
  • Managed environments (EKS, GKE, AKS): access to the nodes to load a profile is often limited or impossible
  • privileged: true cancels the default hardening: with containerd, the RuntimeDefault profile is not applied and seccomp is disabled

Key takeaways

  • AppArmor controls access to files, capabilities and network through named profiles
  • The node must enable AppArmor: otherwise the kubelet rejects the Pod, it does not run it unprotected
  • The container level wins over the Pod level
  • Since Kubernetes 1.30: use securityContext.appArmorProfile, the annotations are deprecated, the field is stable since 1.31
  • The profile must be loaded on every node where the Pod can run
  • Only the (enforce) suffix protects; (complain) logs and lets through everything except deny rules
  • privileged: true cancels the default profiles without any message
  • Seccomp is complementary, not an alternative: apply them together

Knowledge check

The questions cover what really discriminates on the CKS exam: the appArmorProfile field versus the annotations it replaces, the priority between Pod level and container level, and what happens to a Pod when the profile is not loaded on the node. A missed answer points to the section to reread.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
8 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

Put it into practice

This confinement cannot be demonstrated on a cluster of containers, which shares the host's kernel: the lab runs on a virtual machine where the profile is really loaded. You load an AppArmor profile on the node, apply it to a container through the securityContext field, and prove that the confinement works by watching a forbidden write fail.