Sub topic 11
Cluster-wide defaults and exemptions
This chapter assumes you can label a namespace with the pod-security.kubernetes.io labels and know what enforce, audit, and warn do. Here you will set a baseline for every namespace at the API server and carve out exceptions.
Why configure defaults
Namespace labels only protect namespaces that carry them. A namespace created without labels uses the cluster default, and out of the box that default is privileged, which allows everything. You can change the default in the API server's Pod Security Admission configuration. A new namespace is then covered from the moment it exists.
The admission configuration file
The Pod Security Admission plugin is built into the API server. You configure it with an AdmissionConfiguration file:
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
- name: PodSecurity
configuration:
apiVersion: pod-security.admission.config.k8s.io/v1
kind: PodSecurityConfiguration
defaults:
enforce: "baseline"
enforce-version: "latest"
audit: "restricted"
audit-version: "latest"
warn: "restricted"
warn-version: "latest"
exemptions:
usernames: []
runtimeClasses: []
namespaces: [kube-system]This file does two things:
- Defaults. Namespaces with no matching label are enforced at Baseline. They are audited and warned at Restricted.
- Exemptions. Requests that match an exemption skip Pod Security checks entirely.
You point the API server at the file with the --admission-control-config-file flag. The file must exist on the control plane node. With kubeadm, the API server runs as a static pod, so mount the file into that pod with a hostPath volume. Restart the API server after you change the file.
A label on a namespace overrides the default for that mode only. If a namespace sets enforce: restricted and nothing else, its audit and warn modes still use the configured defaults.
How a request is evaluated
The three kinds of exemption
| Field | Matches | Typical use |
|---|---|---|
| `namespaces` | Pods created in the listed namespaces | `kube-system` and other infrastructure namespaces |
| `usernames` | The authenticated user making the request | A trusted automation identity |
| `runtimeClasses` | Pods whose `runtimeClassName` is listed | A sandboxed runtime such as gVisor or Kata |
Keep these lists short. Each entry is a hole in the policy.
Gotchas
- Username exemptions rarely help with Deployments. When you create a Deployment, the pod is created by a controller, not by you. The pod request carries the controller's identity. Exempting your own user therefore does not exempt the pods that your Deployments create. Namespace exemptions work for this case.
- Exemptions skip all three modes. An exempted namespace gets no enforce, no audit, and no warn. You will not see warnings for it either.
- The file is not hot-reloaded. A mistake in the file can keep the API server from starting, so keep a working copy and check the YAML before you restart.
Choosing defaults
A common approach is to enforce a level that your existing workloads already pass, and use audit and warn to show how far you are from the next level up. The file above follows that approach: Baseline is enforced, and Restricted is reported. When teams have fixed their pods, raise enforce to restricted. Test that change first with the server-side dry run from the rollout chapter.
Use latest for the version fields only if you accept that policy meaning can change when you upgrade Kubernetes. Pin a version such as v1.30 if you want it to stay fixed.
Exercise
A team creates Deployments as the user ci-bot. You add ci-bot to exemptions.usernames, but their pods in a Baseline-enforced namespace are still rejected. Why, and what is a better option if the namespace must be exempt?
Show answer
Pods from a Deployment are created by the ReplicaSet controller, not by ci-bot, so the exempt username never appears on the pod creation request. Exempt the namespace through exemptions.namespaces instead. Another option is to fix the pod template so it meets Baseline, which keeps the policy in force.