Day 9: Authentication, RBAC and Admission
Goal: You give your application exactly the API permissions it needs and understand why the API server rejects a pod despite correct permissions. · Duration: approx. 75 min · Prerequisite: Day 8
Briefing
CKAD: “Understand authentication, authorization and admission control” (Application Environment, Configuration and Security, 25 %).
Request --> AuthN ---------> AuthZ ------------> Admission -------------> etcd
Who are you? May you do that? Is the object allowed?
certificate, RBAC: Role + mutating, then validating:
SA token RoleBinding PodSecurity, VAP, webhooks
401 403 403 / invalid
- A pod talks to the API as a ServiceAccount:
system:serviceaccount:<ns>:<name>. - RBAC only knows permissions, no denials. Without a matching binding:
no.
mkdir -p ~/ckad/tag09 && cd ~/ckad/tag09
k create ns tag09
k config set-context --current --namespace=tag09Drill 1: Who am I?
k auth whoamiExpected (shortened):
ATTRIBUTE VALUE
Username kubernetes-admin
Groups [kubeadm:cluster-admins system:authenticated]
You are logged in with a client certificate and are a member of a group with full permissions. Your application gets its own ServiceAccount – and initially nothing at all:
k create sa notes-app
k auth can-i list pods --as=system:serviceaccount:tag09:notes-appExpected:
serviceaccount/notes-app created
no
Save your progress? Sign up for free
Drill 2: Role and RoleBinding
k create role pod-reader --verb=get,list,watch --resource=pods
k create rolebinding notes-app-pod-reader --role=pod-reader \
--serviceaccount=tag09:notes-app
k auth can-i list pods --as=system:serviceaccount:tag09:notes-app
k auth can-i delete pods --as=system:serviceaccount:tag09:notes-app
k auth can-i list pods -n default --as=system:serviceaccount:tag09:notes-appExpected: yes, no,
no – reading yes, deleting no, and only in its own
namespace.
All permissions at once:
k auth can-i --list --as=system:serviceaccount:tag09:notes-app | grep -v '^ 'Expected (shortened):
Resources Non-Resource URLs Resource Names ...
selfsubjectreviews.authentication.k8s.io [] [] ...
...
pods [] [] ...
The lines with /api, /healthz etc. (hidden
here with grep -v) are allowed for every authenticated
client. On this cluster, Calico’s own policy types show up as well.
Remember the notation: with --serviceaccount it is
namespace:name, with --as
system:serviceaccount:namespace:name.
Save your progress? Sign up for free
Drill 3: Use built-in ClusterRoles in a namespace
Kubernetes ships with view, edit and
admin. Via a RoleBinding they apply only
in the binding’s namespace:
k create sa ci-bot
k create rolebinding ci-bot-edit --clusterrole=edit --serviceaccount=tag09:ci-bot
k auth can-i create deployments.apps --as=system:serviceaccount:tag09:ci-bot
k auth can-i get secrets --as=system:serviceaccount:tag09:ci-bot
k auth can-i create rolebindings --as=system:serviceaccount:tag09:ci-bot
k auth can-i create deployments -n default --as=system:serviceaccount:tag09:ci-botExpected: yes, yes,
no, no.
k create rolebinding notes-app-view --clusterrole=view --serviceaccount=tag09:notes-app
k auth can-i get configmaps --as=system:serviceaccount:tag09:notes-app
k auth can-i get secrets --as=system:serviceaccount:tag09:notes-app
k get rolebinding -o wideExpected:
rolebinding.rbac.authorization.k8s.io/notes-app-view created
yes
no
NAME ROLE AGE USERS GROUPS SERVICEACCOUNTS
ci-bot-edit ClusterRole/edit 7s tag09/ci-bot
notes-app-pod-reader Role/pod-reader 15s tag09/notes-app
notes-app-view ClusterRole/view 1s tag09/notes-app
Why? edit may read Secrets, view may not.
Whoever grants edit also hands out the passwords in the
namespace.
Save your progress? Sign up for free
Drill 4: A pod queries the API – 403, then 200
Every pod gets the token of its ServiceAccount mounted under
/var/run/secrets/kubernetes.io/serviceaccount.
k create sa lister
cat > api-client.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: api-client
spec:
serviceAccountName: lister
containers:
- name: curl
image: curlimages/curl:8.17.0
command: ["sleep", "3600"]
EOF
k apply -f api-client.yaml
k wait --for=condition=Ready pod/api-client --timeout=90s
k exec api-client -- ls /var/run/secrets/kubernetes.io/serviceaccountExpected:
serviceaccount/lister created
pod/api-client created
pod/api-client condition met
ca.crt
namespace
token
Query the ConfigMaps of its own namespace with token and CA:
k exec api-client -- sh -c 'D=/var/run/secrets/kubernetes.io/serviceaccount
curl -s --cacert $D/ca.crt -H "Authorization: Bearer $(cat $D/token)" \
https://kubernetes.default.svc/api/v1/namespaces/tag09/configmaps' \
| grep -E '"(kind|message|code)"'Expected:
"kind": "Status",
"message": "configmaps is forbidden: User \"system:serviceaccount:tag09:lister\"
cannot list resource \"configmaps\" in API group \"\" in the namespace \"tag09\"",
"kind": "configmaps"
"code": 403
Authenticated yes (the name is in the message), authorized no. Now bind the role and repeat the same command:
k create rolebinding lister-view --clusterrole=view --serviceaccount=tag09:lister
k exec api-client -- sh -c 'D=/var/run/secrets/kubernetes.io/serviceaccount
curl -s -o /dev/null -w "%{http_code}\n" --cacert $D/ca.crt \
-H "Authorization: Bearer $(cat $D/token)" \
https://kubernetes.default.svc/api/v1/namespaces/tag09/configmaps'Expected:
rolebinding.rbac.authorization.k8s.io/lister-view created
200
RBAC takes effect immediately – no pod restart needed.
Save your progress? Sign up for free
Drill 5: Admission – ValidatingAdmissionPolicy
RBAC asks “may you create Deployments?”, not “with how many replicas?”. That is checked by admission. A ValidatingAdmissionPolicy (VAP) is a CEL rule directly in the API server; the binding defines where it applies – here only in namespaces with a label:
cat > tag09-max-replicas.yaml <<'EOF'
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: tag09-max-replicas
spec:
failurePolicy: Fail
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["deployments"]
validations:
- expression: "object.spec.replicas <= 3"
message: "Höchstens 3 Replicas erlaubt"
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: tag09-max-replicas
spec:
policyName: tag09-max-replicas
validationActions: ["Deny"]
matchResources:
namespaceSelector:
matchLabels:
tag09-limits: "on"
EOF
k apply -f tag09-max-replicas.yaml
k create deploy big --image=nginx:1.29 --replicas=5 --dry-run=serverExpected:
validatingadmissionpolicy.admissionregistration.k8s.io/tag09-max-replicas created
validatingadmissionpolicybinding.admissionregistration.k8s.io/tag09-max-replicas created
deployment.apps/big created (server dry run)
The label is still missing. --dry-run=server runs
through admission but stores nothing.
k label ns tag09 tag09-limits=onThe API server needs one or two seconds to pick up new policies and labels – if you type faster, you still slip through.
k create deploy big --image=nginx:1.29 --replicas=5
k create deploy small --image=nginx:1.29 --replicas=2Expected:
error: failed to create deployment: deployments.apps "big" is forbidden:
ValidatingAdmissionPolicy 'tag09-max-replicas' with binding 'tag09-max-replicas' denied
request: Höchstens 3 Replicas erlaubt
deployment.apps/small created
Save your progress? Sign up for free
Drill 6: Admission – Pod Security
The built-in PodSecurity plugin checks pods against
the levels privileged, baseline or
restricted. It is switched on with a namespace label. First
test what would happen to the running pods:
k label ns tag09 pod-security.kubernetes.io/enforce=restricted --dry-run=serverExpected (shortened):
Warning: existing pods in namespace "tag09" violate the new PodSecurity enforce level
"restricted:latest"
Warning: api-client (and 2 other pods): allowPrivilegeEscalation != false, ...
namespace/tag09 labeled (server dry run)
The two others are the nginx pods of small. Running pods
are left untouched – the check only happens at creation:
k label ns tag09 pod-security.kubernetes.io/enforce=restricted
k run notes --image=172.16.0.126:30500/notes-api:1.0 --env=NOTES_API_KEY=lab-key-123Expected (shortened, wrapped):
Warning: existing pods in namespace "tag09" violate the new PodSecurity enforce ...
namespace/tag09 labeled
Error from server (Forbidden): pods "notes" is forbidden: violates PodSecurity
"restricted:latest": allowPrivilegeEscalation != false (container "notes" must set
securityContext.allowPrivilegeEscalation=false), unrestricted capabilities
(container "notes" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot !=
true (pod or container "notes" must set securityContext.runAsNonRoot=true),
seccompProfile (pod or container "notes" must set securityContext.seccompProfile.type
to "RuntimeDefault" or "Localhost")
The message is the repair manual. lab-key-123 is purely
a lab value.
cat > notes.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: notes
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: notes
image: 172.16.0.126:30500/notes-api:1.0
env:
- name: NOTES_API_KEY
value: lab-key-123
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
EOF
k apply -f notes.yaml
k wait --for=condition=Ready pod/notes --timeout=60sExpected:
pod/notes created
pod/notes condition met
runAsNonRoot works because the image runs as UID 65532.
nginx (runs as root) would additionally need runAsUser and
a different image (nginxinc/nginx-unprivileged) here.
Warning: For a Deployment you only get a
Warning; the Deployment is created – but its pods are not. The reason is shown byk get events --field-selector reason=FailedCreate.
Save your progress? Sign up for free
Drill 7: Which admission plugins are running?
Read only, change nothing – on cp1:
sudo grep -E 'authorization-mode|admission' \
/etc/kubernetes/manifests/kube-apiserver.yaml
k exec -n kube-system kube-apiserver-cp1 -- kube-apiserver -h \
| grep -o 'in addition to default enabled ones ([^)]*)'Expected (shortened):
- --authorization-mode=Node,RBAC
- --enable-admission-plugins=NodeRestriction
in addition to default enabled ones (NamespaceLifecycle, LimitRanger, ...
PodSecurity, ... MutatingAdmissionWebhook, ValidatingAdmissionPolicy,
ValidatingAdmissionWebhook, ResourceQuota)
So PodSecurity, ValidatingAdmissionPolicy
and ResourceQuota (Day 7) are active without any extra
work; kubeadm only additionally enables
NodeRestriction.
Save your progress? Sign up for free
Exercise: Speed Drills
- The (new) ServiceAccount
reportershould be allowed to read Deployments intag09(get, list). Target time: 60 s - May
ci-botdelete pods intag09? Target time: 15 s - Additionally set a warning for violations of
baselineontag09. Target time: 20 s
Solution
k create sa reporter
k create role deploy-reader --verb=get,list --resource=deployments.apps
k create rolebinding reporter-deploy-reader --role=deploy-reader \
--serviceaccount=tag09:reporter
k auth can-i list deployments.apps --as=system:serviceaccount:tag09:reporter
k auth can-i delete pods --as=system:serviceaccount:tag09:ci-bot
k label ns tag09 pod-security.kubernetes.io/warn=baselineExpected:
serviceaccount/reporter created
role.rbac.authorization.k8s.io/deploy-reader created
rolebinding.rbac.authorization.k8s.io/reporter-deploy-reader created
yes
yes
namespace/tag09 labeled
Save your progress? Sign up for free
Checkpoint
k auth can-i get secrets --as=system:serviceaccount:tag09:notes-appExpected: no
k get ns tag09 -L pod-security.kubernetes.io/enforce,tag09-limitsExpected:
NAME STATUS AGE ENFORCE TAG09-LIMITS
tag09 Active 5m restricted on
k get pod notes -o jsonpath='{.status.phase}{"\n"}'Expected: Running
Challenge
Scale small to 5 replicas with k scale. The
policy does not kick in – find the reason and close the gap.
Hint: k scale does not change the
Deployment but its scale subresource.
Solution
k scale deploy small --replicas=5
k scale deploy small --replicas=2
sed -i 's|\["deployments"\]|["deployments", "deployments/scale"]|' \
tag09-max-replicas.yaml
k apply -f tag09-max-replicas.yaml
k get validatingadmissionpolicy tag09-max-replicas \
-o jsonpath='{.spec.matchConstraints.resourceRules[0].resources}{"\n"}'k scale deploy small --replicas=5Expected:
deployment.apps/small scaled
deployment.apps/small scaled
validatingadmissionpolicy.admissionregistration.k8s.io/tag09-max-replicas configured
validatingadmissionpolicybinding.admissionregistration.k8s.io/tag09-max-replicas ...
["deployments","deployments/scale"]
The deployments "small" is invalid: : ValidatingAdmissionPolicy 'tag09-max-replicas'
with binding 'tag09-max-replicas' denied request: Höchstens 3 Replicas erlaubt
The expression matches both: the Scale object also has
spec.replicas.
Clean up – the policy is cluster-wide:
k config set-context --current --namespace=default
k delete ns tag09
k delete validatingadmissionpolicybinding tag09-max-replicas
k delete validatingadmissionpolicy tag09-max-replicasSave your progress? Sign up for free
Exam Tip
- After every RBAC task, prove that it works with
k auth can-i … --as=system:serviceaccount:<ns>:<sa>. Specify resources outside the core group with their group:deployments.apps. - If PodSecurity rejects a pod, transfer the message point by point
into the
securityContext. For Deployments, the errors are in the ReplicaSet’s events. - Docs: kubernetes.io → “Pod Security Standards” contains the
ready-made fields for
restricted.
Enterprise Note
- Humans log in via OIDC (Keycloak, Entra ID); bind permissions to groups, never to individuals.
automountServiceAccountToken: falseeverywhere an application does not need the API.- Set Pod Security via labels on all namespaces, first with
warnandaudit, thenenforce. - Complex rules (image provenance, mandatory labels) are handled by Kyverno, OPA Gatekeeper or VAPs with parameters; version policies in Git.