A pod spec copied from a forum
A tutorial says "if it does not work, add privileged: true". A developer does, and the pod works.
A privileged container is close to root on the node itself. One bug in it, and the whole node and every pod on it are at risk.
The one rule
Warning
Everything in this module happens in the cluster of your own lab. Testing a cluster you do not own needs the owner's written permission. In the UK, without it, it can break the Computer Misuse Act 1990.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can make a namespace refuse pods that break the restricted standard.
Key words
| Word | What it means | Example |
|---|---|---|
| Pod Security Standards | three levels of pod safety Kubernetes defines | |
| Privileged | the level that allows anything | |
| Baseline | the level that blocks known escapes | |
| Restricted | the level that asks for every hardening setting | |
| Admission | the API server's check before it stores an object | |
| Namespace label | a key and value that turns a level on | pod-security.kubernetes.io/enforce=restricted |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsThree levels, three modes, one label each
Kubernetes defines three levels: privileged, baseline and restricted. A namespace label chooses one, and the API server checks every new pod against it.
Each level has three modes. Enforce refuses the pod. Warn tells whoever sent it. Audit writes a note in the audit log. A namespace with no label allows anything.
What each level refuses
| Level | Refuses | Fits |
|---|---|---|
| privileged | Nothing | The cluster's own system pods |
| baseline | Privileged, host namespaces, hostPath | Most ordinary apps |
| restricted | Also root, kept capabilities, no seccomp, escalation | Apps you control |
The cells students fill in are left blank in this preview.
Try it: a check before storing
pod = {"privileged": True, "runAsNonRoot": False, "drop": []}
problems = []
if pod["privileged"]:
problems.append("privileged")
if not pod["runAsNonRoot"]:
problems.append("runAsNonRoot != true")
if "ALL" not in pod["drop"]:
problems.append("unrestricted capabilities")
print("refused:" if problems else "allowed", ", ".join(problems))
# Make the pod pass: change the three values.
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsTry the label before you enforce it
Admission is built into your cluster's API server. You start an ordinary pod in a new namespace first, as an older app would be.
A server dry run of the label changes nothing and lists the pods that would break the level. Read it before you enforce.
Start an old-style pod in payments
kubectl create namespace payments; kubectl run legacy -n payments --image=busybox:1.37.0 -- sleep 3600; kubectl wait -n payments --for=condition=Ready pod/legacy --timeout=120sShown here, not run. Press Start lab to type it into your own lab’s terminal.
Ask what restricted would break
kubectl label --dry-run=server --overwrite namespace payments pod-security.kubernetes.io/enforce=restrictedShown here, not run. Press Start lab to type it into your own lab’s terminal.
Enforce restricted, pinned to this version
kubectl label --overwrite namespace payments pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.35 pod-security.kubernetes.io/warn=restrictedShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try to start a privileged pod
kubectl run priv -n payments --image=busybox:1.37.0 --privileged -- sleep 3600Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsWrite a pod that meets restricted
mkdir -p ~/ch && printf '%s\n' 'apiVersion: v1' 'kind: Pod' 'metadata:' ' name: till' ' namespace: payments' 'spec:' ' securityContext:' ' runAsNonRoot: true' ' runAsUser: 10001' ' seccompProfile:' ' type: RuntimeDefault' ' containers:' ' - name: main' ' image: busybox:1.37.0' ' command: ["sleep", "3600"]' ' securityContext:' ' allowPrivilegeEscalation: false' ' capabilities:' ' drop: ["ALL"]' > ~/ch/till.yamlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start it, and look at both pods
cd ~/ch && kubectl apply -f till.yaml && kubectl wait -n payments --for=condition=Ready pod/till --timeout=120s && kubectl get pods -n paymentsShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think enforcing a level stops pods that already run. Admission only checks new objects, and legacy is still running. Run the dry run first, then replace the pods it names.
A refused pod can hide behind a Deployment
You rarely start pods by hand. A Deployment makes a ReplicaSet, and the ReplicaSet makes the pods. If its pods break the level, the Deployment is stored and no pod ever starts.
The only sign is a FailedCreate event on the ReplicaSet. That event is also how you detect a refusal.
Store a Deployment whose pods break restricted
kubectl create deployment loose -n payments --image=busybox:1.37.0 -- sleep 3600; sleep 10; kubectl get deployment loose -n payments; kubectl get events -n payments --field-selector reason=FailedCreate -o custom-columns=KIND:.involvedObject.kind,MESSAGE:.message | cut -c1-140 | head -3Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Detect refusals, then fix the pod
Watch for FailedCreate events and for warnings when files are applied. With audit mode on, the audit log notes every pod that would break the level, as lesson 6 shows.
The fix is the pod, never the label: add the security context the refusal names.
Assignment: check a pod like admission
Write a simplified Pod Security check.
violations(pod) gives the names in ORDER the pod breaks: a host namespace (hostNetwork, hostPID or hostIPC), a privileged container, a hostPath volume, allowPrivilegeEscalation not false, "ALL" not dropped, runAsNonRoot not true, and a seccompProfile type that is not RuntimeDefault or Localhost. Container settings win over the pod's. level(pod) answers "restricted", "baseline" or "privileged".
ORDER = ["host namespaces", "privileged", "hostPath volumes",
"allowPrivilegeEscalation != false", "unrestricted capabilities",
"runAsNonRoot != true", "seccompProfile"]
def violations(pod):
# The names in ORDER that the pod breaks (a simplified restricted standard).
return []
def level(pod):
# "restricted" with no violations, "baseline" when none of the first three,
# otherwise "privileged".
return "restricted"
Make a free account to save your work and track your progress.
Exit quiz
Have a go