Day 3: RBAC
Goal: You create users via the Certificates API and give them and ServiceAccounts exactly the permissions they need. · Duration: approx. 60 min · Prerequisite: Day 2
Briefing
CKA: “Manage role based access control (RBAC)” (part of the 25 %).
Subject Binding Rules
User / Group / ---> RoleBinding (namespace) Role (namespace)
ServiceAccount ClusterRoleBinding (all) ClusterRole (cluster-wide)
- Kubernetes has no user objects. A user is the
CNof a client certificate, groups are theOfields. - A RoleBinding may also bind a ClusterRole – then its rules apply only in the binding’s namespace.
Drill 1: Key and CSR for jane
mkdir -p ~/cka/tag03 && cd ~/cka/tag03
k create ns tag03
openssl genrsa -out jane.key 2048
openssl req -new -key jane.key -subj "/CN=jane/O=dev" -out jane.csrHand the CSR to Kubernetes – with the signer for client certificates:
cat <<EOF | k apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: jane
spec:
request: $(base64 -w0 jane.csr)
signerName: kubernetes.io/kube-apiserver-client
expirationSeconds: 86400
usages:
- client auth
EOF
k get csr janeExpected:
NAME AGE SIGNERNAME REQUESTOR ... CONDITION
jane 0s kubernetes.io/kube-apiserver-client kubernetes-admin ... Pending
Save your progress? Sign up for free
Drill 2: Approve and fetch the certificate
k certificate approve jane
k get csr jane -o jsonpath='{.status.certificate}' | base64 -d > jane.crt
openssl x509 -in jane.crt -noout -subject -enddateExpected:
certificatesigningrequest.certificates.k8s.io/jane approved
subject=O = dev, CN = jane
notAfter=Nov 18 21:25:10 2026 GMT
Now create a context for jane:
k config set-credentials jane --client-certificate=jane.crt \
--client-key=jane.key --embed-certs=true
k config set-context jane --cluster=kubernetes --user=jane --namespace=tag03
k --context=jane auth whoamiExpected (shortened):
ATTRIBUTE VALUE
Username jane
Groups [dev system:authenticated]
k --context=jane get podsExpected:
Error from server (Forbidden): pods is forbidden: User "jane" cannot list
resource "pods" in API group "" in the namespace "tag03"
Authenticated yes, authorized no – exactly as it should be.
Save your progress? Sign up for free
Drill 3: Role and RoleBinding
k create role pod-reader -n tag03 --verb=get,list,watch --resource=pods
k create rolebinding jane-pod-reader -n tag03 --role=pod-reader --user=jane
k run web -n tag03 --image=nginx:1.29
k --context=jane get podsExpected:
NAME READY STATUS RESTARTS AGE
web 0/1 ContainerCreating 0 0s
Check the limits without switching context:
k auth can-i delete pods -n tag03 --as=jane
k auth can-i list pods -n default --as=janeExpected: no twice.
Save your progress? Sign up for free
Drill 4: ClusterRole for a group
Nodes are cluster-wide objects – that requires a ClusterRole and a
ClusterRoleBinding. The group dev is bound, not jane
personally:
k create clusterrole node-reader --verb=get,list --resource=nodes
k create clusterrolebinding dev-node-reader --clusterrole=node-reader --group=dev
k --context=jane get nodesExpected:
NAME STATUS ROLES AGE VERSION
cp1 Ready control-plane 17m v1.36.6
worker1 Ready <none> 17m v1.36.6
worker2 Ready <none> 17m v1.36.6
Save your progress? Sign up for free
Drill 5: Bind a ClusterRole in a namespace
The built-in ClusterRole edit allows almost everything
except RBAC. Via a RoleBinding it applies only in
tag03:
k create rolebinding jane-edit -n tag03 --clusterrole=edit --user=jane
k --context=jane create deployment app --image=nginx:1.29
k auth can-i create deployments -n default --as=janeExpected:
rolebinding.rbac.authorization.k8s.io/jane-edit created
deployment.apps/app created
no
All bindings in the namespace at a glance:
k get rolebinding -n tag03 -o wideExpected:
NAME ROLE AGE USERS GROUPS SERVICEACCOUNTS
jane-edit ClusterRole/edit 9s jane
jane-pod-reader Role/pod-reader 11s jane
Save your progress? Sign up for free
Drill 6: ServiceAccount with read permissions
Programs in the cluster authenticate as a ServiceAccount:
k create sa deployer -n tag03
k create rolebinding deployer-view -n tag03 --clusterrole=view \
--serviceaccount=tag03:deployer
k auth can-i list pods -n tag03 --as=system:serviceaccount:tag03:deployer
k auth can-i create pods -n tag03 --as=system:serviceaccount:tag03:deployerExpected: yes, then
no.
Remember the notation: for --serviceaccount it is
namespace:name, for --as it is
system:serviceaccount:namespace:name.
Save your progress? Sign up for free
Drill 7: Directly against the API with a token
TOKEN=$(k create token deployer -n tag03 --duration=10m)
API=https://172.16.0.126:6443
curl -sk -H "Authorization: Bearer $TOKEN" $API/api/v1/namespaces/tag03/pods \
| grep -m1 '"kind"'
curl -sk -H "Authorization: Bearer $TOKEN" $API/api/v1/namespaces/kube-system/pods \
| grep -E '"(message|code)"'Expected:
"kind": "PodList",
"message": "pods is forbidden: User \"system:serviceaccount:tag03:deployer\" cannot
list resource \"pods\" in API group \"\" in the namespace \"kube-system\"",
"code": 403
k create token creates short-lived tokens. Long-lived
token Secrets are only needed for legacy systems.
Save your progress? Sign up for free
Exercise: Speed Drills
- May the ServiceAccount
defaultinkube-systemread Secrets? Target time: 20 s - Create a Role
cm-adminintag03that may create, update and delete ConfigMaps. Target time: 30 s - Show all rules of the Role
pod-reader. Target time: 20 s
Solution
k auth can-i get secrets -n kube-system --as=system:serviceaccount:kube-system:default
k create role cm-admin -n tag03 --verb=create,update,patch,delete --resource=configmaps
k describe role pod-reader -n tag03Expected (excerpt):
no
role.rbac.authorization.k8s.io/cm-admin created
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
pods [] [] [get list watch]
Save your progress? Sign up for free
Checkpoint
k auth can-i list nodes --as=jane --as-group=devExpected: yes (plus the warning
resource 'nodes' is not namespace scoped – harmless).
Without --as-group the group dev is missing:
when impersonating, the API does not know the certificate.
k auth can-i delete nodes --as=jane --as-group=devExpected: no
k auth can-i create deployments -n tag03 --as=janeExpected: yes
Challenge
The ServiceAccount deployer should be allowed to list
Deployments in all namespaces – but not change anything
else about them.
Hint: Deployments live in the API group
apps.
Solution
k create clusterrole deploy-lister --verb=list --resource=deployments.apps
k create clusterrolebinding deployer-deploy-lister --clusterrole=deploy-lister \
--serviceaccount=tag03:deployer
k auth can-i list deployments -A --as=system:serviceaccount:tag03:deployer
k auth can-i delete deployments -A --as=system:serviceaccount:tag03:deployerExpected: yes, then
no.
Clean up:
k delete ns tag03
k delete clusterrolebinding dev-node-reader deployer-deploy-lister
k delete clusterrole node-reader deploy-lister
k delete csr jane
k config delete-context jane
k config delete-user janeSave your progress? Sign up for free
Exam Tip
k auth can-i … --as=…is the fastest proof that your solution works – use it after every RBAC task.- For resources outside the core group, specify the group:
deployments.apps,ingresses.networking.k8s.io. - CSR tasks: do not forget
signerName: kubernetes.io/kube-apiserver-clientandusages: client auth, otherwise no certificate is issued.
Enterprise Note
- In production, people log in via OIDC (Keycloak, Entra ID), not with client certificates – those cannot be revoked.
- Bind permissions to groups, never to individuals.
- Audit
cluster-adminbindings regularly, e.g. withkubectl get clusterrolebindings -o wide. - Do not automount ServiceAccount tokens where they are not needed
(
automountServiceAccountToken: false).