Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Pod Security Admission at restricted

After this lesson: I can make a namespace refuse pods that break the restricted standard.

60 min lesson7 commandsKubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can make a namespace refuse pods that break the restricted standard.

Key words

WordWhat it meansExample
Pod Security Standardsthree levels of pod safety Kubernetes defines
Privilegedthe level that allows anything
Baselinethe level that blocks known escapes
Restrictedthe level that asks for every hardening setting
Admissionthe API server's check before it stores an object
Namespace labela key and value that turns a level onpod-security.kubernetes.io/enforce=restricted

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Three 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

LevelRefusesFits
privilegedNothingThe cluster's own system pods
baselinePrivileged, host namespaces, hostPathMost ordinary apps
restrictedAlso root, kept capabilities, no seccomp, escalationApps you control

The cells students fill in are left blank in this preview.

Try it: a check before storing

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try 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

Command
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=120s

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Ask what restricted would break

Command
kubectl label --dry-run=server --overwrite namespace payments pod-security.kubernetes.io/enforce=restricted

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Enforce restricted, pinned to this version

Command
kubectl label --overwrite namespace payments pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.35 pod-security.kubernetes.io/warn=restricted

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try to start a privileged pod

Command
kubectl run priv -n payments --image=busybox:1.37.0 --privileged -- sleep 3600

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Write a pod that meets restricted

Command
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.yaml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start it, and look at both pods

Command
cd ~/ch && kubectl apply -f till.yaml && kubectl wait -n payments --for=condition=Ready pod/till --timeout=120s && kubectl get pods -n payments

Shown 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

Command
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 -3

Shown 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".

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.