CKA prep, by exam domain · Lesson 6 of 12 · about 50 minutes

RBAC: exactly the right access, proven

Role-based access control (RBAC) on the practice cluster: a ServiceAccount that can only read pods, the Role that can't reach nodes, a built-in ClusterRole reused in one namespace, and a human user created from a certificate signing request. Every permission proven with kubectl auth can-i.

Exam domain: Cluster architecture, installation and configuration · 25% of the exam

By the end you'll know

  • What a ServiceAccount, Role, RoleBinding, ClusterRole and ClusterRoleBinding each do
  • How to prove a permission with k auth can-i, and the typo that still gets a confident "no"
  • Why a Role can be created for nodes and still grant nothing
  • How to reuse a built-in ClusterRole in one namespace, and what edit really includes
  • How to create a user from scratch: a key, a certificate signing request, and a kubeconfig context
  • How to read a Forbidden error as a description of the missing permission

Before you start

  • You need: the cluster from Lesson 2 (now on v1.37 after Lesson 5), all nodes Ready.
  • You should know: the basics from the namespaces and access lesson in the Kubernetes track.
  • The output below is from real runs on November 26, 2026, in New York, on an Apple silicon Mac.

RBAC is part of the Cluster Architecture domain, 25% of the exam, and RBAC work comes down to one request: "give this account exactly this access." Exactly is the word that matters. Too little and something breaks; too much is a security hole. So every step below ends with proof.

Nothing breaks in this lesson. You build access one step at a time, in a namespace of its own:

BASH
k create ns rbac-lab
k create deploy web -n rbac-lab --image=nginx:alpine --replicas=2

The five objects

ObjectAnswersScope
ServiceAccountWho: an identity for apps and scriptsOne namespace
RoleWhat: which verbs on which resourcesOne namespace
RoleBindingGives a Role (or a ClusterRole) to someone, in its namespaceOne namespace
ClusterRoleWhat, without a namespace: can cover nodes and other cluster-wide objectsCluster-wide
ClusterRoleBindingGives a ClusterRole to someone, everywhereCluster-wide

Humans aren't on the list. Kubernetes has no user objects: a user is just a name inside a certificate the cluster trusts. Step 4 makes one.

Step 1: an account that can only read pods

(cp1)

BASH
k create sa app-reader -n rbac-lab
k create role pod-reader -n rbac-lab --verb=get,list,watch --resource=pods
k create rolebinding app-reader-binding -n rbac-lab --role=pod-reader --serviceaccount=rbac-lab:app-reader
  • k create sa: a ServiceAccount (sa is the short name).
  • --verb=get,list,watch: read one pod, list them, and follow changes. No create, delete or update.
  • --resource=pods: pods only.
  • --serviceaccount=rbac-lab:app-reader: the account, as NAMESPACE:NAME.

My first try at the binding said -n rbac:

OUTPUT
error: failed to create rolebinding: namespaces "rbac" not found

A RoleBinding has to live in a namespace that exists, and it only works there.

Prove it with can-i

BASH
k auth can-i list pods -n rbac-lab --as=system:serviceaccount:rbac-lab:app-reader
  • k auth can-i VERB RESOURCE: answers yes or no.
  • --as=...: asks as another identity. That works because you're the cluster admin, who may impersonate anyone.
  • system:serviceaccount:NAMESPACE:NAME: a ServiceAccount's full username, in exactly that form.
QuestionAnswerWhy
list pods -n rbac-labyesThe Role grants list on pods
delete pods -n rbac-labnodelete isn't in the Role
list deployments -n rbac-labnoThe Role covers pods only
list pods -n defaultnoThe Role and its binding only work in rbac-lab

One typo taught the most important thing about can-i:

OUTPUT
vagrant@cp1:~$ k auth can-i deployments pods -n rbac-lab --as=system:serviceaccount:rbac-lab:app-reader
Warning: verb 'deployments' is not a known verb

no

A typo still got a confident-looking no. There's no verb called deployments, so of course it isn't allowed. An unexpected no deserves a second look: read any warning above it, and check the order, verb first, then resource, exactly two words. Three words gets a clear error instead:

OUTPUT
error: you must specify two arguments: verb resource or verb resource/resourceName.

Everything at once: --list

BASH
k auth can-i --list -n rbac-lab --as=system:serviceaccount:rbac-lab:app-reader
OUTPUT
Resources                                       Non-Resource URLs                      Resource Names   Verbs
selfsubjectreviews.authentication.k8s.io        []                                     []               [create]
selfsubjectaccessreviews.authorization.k8s.io   []                                     []               [create]
selfsubjectrulesreviews.authorization.k8s.io    []                                     []               [create]
pods                                            []                                     []               [get list watch]
clustertrustbundles.certificates.k8s.io         []                                     []               [get list watch]
                                                [/.well-known/openid-configuration/]   []               [get]
                                                [/api/*]                               []               [get]
                                                [/api]                                 []               [get]
...
                                                [/healthz]                             []               [get]
...
                                                [/version]                             []               [get]

Only the pods line is yours. The rest comes with every account: the three self...reviews let any account ask what it can do (that's how can-i works for it), the URLs let it discover the API and check health, and clustertrustbundles is read-only certificate data. Look past them for the lines your Role added.

Step 2: the Role that can't reach nodes

Try granting node access with a plain Role:

OUTPUT
vagrant@cp1:~$ k create role node-try -n rbac-lab --verb=list --resource=nodes
role.rbac.authorization.k8s.io/node-try created
vagrant@cp1:~$ k create rolebinding node-try -n rbac-lab --role=node-try --serviceaccount=rbac-lab:app-reader
rolebinding.rbac.authorization.k8s.io/node-try created
vagrant@cp1:~$ k auth can-i list nodes --as=system:serviceaccount:rbac-lab:app-reader
Warning: resource 'nodes' is not namespace scoped

no

Accepted, accepted, and it grants nothing. A Role only works inside its namespace, and nodes don't belong to one (Lesson 2's cluster-wide objects). Nothing errors when you create it; the only hint is that warning. Delete the useless pair:

BASH
k delete rolebinding node-try -n rbac-lab
k delete role node-try -n rbac-lab

Then do it properly:

BASH
k create clusterrole node-viewer --verb=get,list,watch --resource=nodes
k create clusterrolebinding app-reader-nodes --clusterrole=node-viewer --serviceaccount=rbac-lab:app-reader
  • No -n: neither object belongs to a namespace.
OUTPUT
vagrant@cp1:~$ k auth can-i list nodes --as=system:serviceaccount:rbac-lab:app-reader
Warning: resource 'nodes' is not namespace scoped

yes
vagrant@cp1:~$ k auth can-i delete nodes --as=system:serviceaccount:rbac-lab:app-reader
Warning: resource 'nodes' is not namespace scoped

no

The warning appears here too because kubectl attaches your current namespace to every can-i question. For cluster-wide resources, ignore it. "Allow listing nodes" (or PersistentVolumes, or namespaces) always means a ClusterRole and a ClusterRoleBinding.

Step 3: reuse a built-in ClusterRole in one namespace

Kubernetes ships ready-made ClusterRoles: view, edit, admin and cluster-admin. A RoleBinding can point at a ClusterRole, and its permissions then apply only in the RoleBinding's namespace. That's the most common pattern in real clusters, and an easy one to miss.

BASH
k create sa deployer -n rbac-lab
k create rolebinding deployer-edit -n rbac-lab --clusterrole=edit --serviceaccount=rbac-lab:deployer
  • --clusterrole=edit (not --role=): a RoleBinding that points at the built-in edit.
QuestionAnswerWhy
create deployments -n rbac-labyesedit manages apps
create deployments -n defaultnoA RoleBinding keeps it to rbac-lab
get secrets -n rbac-labyesedit includes Secrets
create rolebindings -n rbac-labnoedit has no RBAC permissions

The role itself confirms both surprises:

OUTPUT
vagrant@cp1:~$ k describe clusterrole edit | grep -E "secrets|rolebindings"
  secrets                                      []                 []              [get list watch create delete deletecollection patch update]
  • grep -E: extended patterns, where | means "or".
  • Secrets, with every verb. Anyone with edit can read every password in the namespace. When a task says read-only, use view, which leaves Secrets out.
  • No rolebindings line at all, so edit can't grant anyone more access, itself included. Only admin manages RBAC inside a namespace.

Step 4: a user from scratch

A user is a name in a certificate signed by the cluster's certificate authority (CA). Make a key and a certificate signing request (CSR):

BASH
openssl genrsa -out dev.key 2048
openssl req -new -key dev.key -out dev.csr -subj "/CN=dev-user/O=dev-team"
  • genrsa: a 2048-bit private key. It never leaves this machine.
  • req -new: the CSR, asking for the key to be certified.
  • /CN=dev-user/O=dev-team: the CN (common name) becomes the username, the O (organization) a group.

Hand the CSR to the cluster:

BASH
cat <<EOF | k apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: dev-user
spec:
  request: $(base64 -w0 < dev.csr)
  signerName: kubernetes.io/kube-apiserver-client
  expirationSeconds: 86400
  usages:
  - client auth
EOF
k get csr dev-user
  • cat <<EOF | k apply -f -: feeds the lines up to EOF to kubectl as a file. -f - reads from the input.
  • $(base64 -w0 < dev.csr): the CSR, base64-encoded on one line (-w0: no wrapping).
  • signerName: kubernetes.io/kube-apiserver-client: a certificate for logging in to the API server.
  • expirationSeconds: 86400: valid for one day.
OUTPUT
certificatesigningrequest.certificates.k8s.io/dev-user created
NAME       AGE   SIGNERNAME                            REQUESTOR          REQUESTEDDURATION   CONDITION
dev-user   0s    kubernetes.io/kube-apiserver-client   kubernetes-admin   24h                 Pending

REQUESTOR: kubernetes-admin is you: the admin kubeconfig logs in under that name. Approve it, and collect the certificate:

BASH
k certificate approve dev-user
k get csr dev-user -o jsonpath='{.status.certificate}' | base64 -d > dev.crt
openssl x509 -in dev.crt -noout -subject -issuer -enddate
  • k certificate approve: the admin's yes. The cluster's CA signs it within a second, and get csr then shows Approved,Issued.
  • The JSONPath line: the signed certificate is stored base64-encoded in the CSR's status. base64 -d decodes it.
  • openssl x509 ... -noout: prints facts about the certificate, not the certificate.
OUTPUT
subject=O = dev-team, CN = dev-user
issuer=CN = kubernetes
notAfter=Nov 27 16:38:51 2026 GMT

Signed by the cluster's CA (CN = kubernetes), valid for a day. Now a login for it, as a new context in your kubeconfig:

BASH
k config get-clusters
k config set-credentials dev-user --client-certificate=dev.crt --client-key=dev.key --embed-certs=true
k config set-context dev-user@kubernetes --cluster=kubernetes --user=dev-user --namespace=rbac-lab
  • get-clusters: the cluster's name in your kubeconfig. kubeadm calls it kubernetes.
  • --embed-certs=true: copies the certificate and key into the kubeconfig, so it no longer depends on the files.
  • set-context: pairs the user with the cluster, with rbac-lab as the default namespace.

Step 5: read the Forbidden error, then grant

OUTPUT
vagrant@cp1:~$ k --context=dev-user@kubernetes get pods
Error from server (Forbidden): pods is forbidden: User "dev-user" cannot list resource "pods" in API group "" in the namespace "rbac-lab"
  • --context=: runs one command as dev-user, without switching your default context.

That error is a complete description of the missing permission:

PartMeans
ForbiddenLogged in fine, not allowed. Unauthorized would mean the login itself failed.
User "dev-user"The certificate's CN, as the username
cannot listThe verb
resource "pods"The resource
API group ""The core group: pods, Services, ConfigMaps
in the namespace "rbac-lab"Where

Grant it, but to the group, reusing step 1's Role. A group binding covers everyone whose certificate carries O=dev-team, now and later:

BASH
k create rolebinding dev-team-read -n rbac-lab --role=pod-reader --group=dev-team
OUTPUT
vagrant@cp1:~$ k --context=dev-user@kubernetes get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-6bd469df5c-7xn6k   1/1     Running   0          120m
web-6bd469df5c-qtmwv   1/1     Running   0          120m
vagrant@cp1:~$ k --context=dev-user@kubernetes get deployments
Error from server (Forbidden): deployments.apps is forbidden: User "dev-user" cannot list resource "deployments" in API group "apps" in the namespace "rbac-lab"
vagrant@cp1:~$ k --context=dev-user@kubernetes delete pod --all
Error from server (Forbidden): pods "web-6bd469df5c-7xn6k" is forbidden: User "dev-user" cannot delete resource "pods" in API group "" in the namespace "rbac-lab"
Error from server (Forbidden): pods "web-6bd469df5c-qtmwv" is forbidden: User "dev-user" cannot delete resource "pods" in API group "" in the namespace "rbac-lab"
  • Pods work, through the group. dev-user was never named.
  • Deployments live in API group "apps", not the core group. k create role --resource=deployments fills that in for you.
  • delete pod --all failed pod by pod: it could list them (allowed), then each delete was refused.

Last, ask the cluster who dev-user is:

OUTPUT
vagrant@cp1:~$ k --context=dev-user@kubernetes auth whoami
ATTRIBUTE                                           VALUE
Username                                            dev-user
Groups                                              [dev-team system:authenticated]
Extra: authentication.kubernetes.io/credential-id   [X509SHA256=0dbde3b3...]

system:authenticated is a group every logged-in identity belongs to. The credential-id is a fingerprint of the certificate used.

How the drills work

Each drill is a task to do from a blank terminal. Try it first, without looking. Then open "Show the fast way" to compare your commands, and read the notes under them: they cover the part people usually get wrong. Repeat a drill a few days later until you can do it cold, because the exam leaves little time to look things up.

The drills

Give a ServiceAccount permission to list pods in one namespace, and nothing else. Prove both halves.

Show the fast way
BASH
k create sa NAME -n NS
k create role pod-reader -n NS --verb=get,list,watch --resource=pods
k create rolebinding NAME-binding -n NS --role=pod-reader --serviceaccount=NS:NAME
k auth can-i list pods -n NS --as=system:serviceaccount:NS:NAME          # yes
k auth can-i delete pods -n NS --as=system:serviceaccount:NS:NAME        # no
k auth can-i list pods -n default --as=system:serviceaccount:NS:NAME     # no

Prove the "nothing else" half too. --serviceaccount= takes NAMESPACE:NAME, and --as= needs the full system:serviceaccount:NAMESPACE:NAME. An unexpected no can be a typo: check for a warning, and that it's verb then resource.

Allow a ServiceAccount to list nodes.

Show the fast way
BASH
k create clusterrole node-viewer --verb=get,list,watch --resource=nodes
k create clusterrolebinding NAME-nodes --clusterrole=node-viewer --serviceaccount=NS:NAME
k auth can-i list nodes --as=system:serviceaccount:NS:NAME               # yes

Nodes, PersistentVolumes and namespaces are cluster-wide, so only a ClusterRole with a ClusterRoleBinding reaches them. A Role for nodes is accepted and grants nothing. k api-resources --namespaced=false lists every cluster-wide type.

Give a ServiceAccount read-only access to everything in one namespace, using a built-in role.

Show the fast way
BASH
k create rolebinding NAME-view -n NS --clusterrole=view --serviceaccount=NS:NAME
k auth can-i list deployments -n NS --as=system:serviceaccount:NS:NAME   # yes
k auth can-i get secrets -n NS --as=system:serviceaccount:NS:NAME        # no
k auth can-i list deployments -n default --as=system:serviceaccount:NS:NAME   # no

A RoleBinding to a ClusterRole applies it in one namespace only. Read-only means view: edit can read and change Secrets. Neither can manage RBAC; admin can, within the namespace.

Create a user named jane in group devs, with a certificate the cluster signs, and a kubeconfig context to log in as her.

Show the fast way
BASH
openssl genrsa -out jane.key 2048
openssl req -new -key jane.key -out jane.csr -subj "/CN=jane/O=devs"
# CertificateSigningRequest: request = base64 -w0 of jane.csr,
# signerName kubernetes.io/kube-apiserver-client, usages: client auth
k certificate approve jane
k get csr jane -o jsonpath='{.status.certificate}' | base64 -d > jane.crt
k config set-credentials jane --client-certificate=jane.crt --client-key=jane.key --embed-certs=true
k config set-context jane@kubernetes --cluster=kubernetes --user=jane
k --context=jane@kubernetes auth whoami

CN becomes the username, O the group. Copy the CertificateSigningRequest YAML from the docs ("Certificates and Certificate Signing Requests") and fill in the request. A new user can log in but do nothing: expect Forbidden until something is bound to the user or the group.

On exam day

  • Read the task for the exact verbs and resources, and grant only those. "Read" usually means get, list and watch.
  • Check the namespace on every Role and RoleBinding. A binding in the wrong namespace does nothing.
  • Cluster-wide resources need ClusterRoles, and a cluster-wide grant needs a ClusterRoleBinding.
  • Prove both halves with can-i: what should work, and what shouldn't.
  • Copy the CSR YAML from the docs, and check the CSR shows Approved,Issued before extracting the certificate.

Clean up

(cp1)

BASH
k delete ns rbac-lab
k delete clusterrolebinding app-reader-nodes
k delete clusterrole node-viewer
k delete csr dev-user
k config delete-context dev-user@kubernetes
k config delete-user dev-user
rm dev.key dev.csr dev.crt
  • k delete ns rbac-lab removes the ServiceAccounts, Roles and RoleBindings in it. The ClusterRole and ClusterRoleBinding are cluster-wide, so they need their own deletes.
  • k config delete-context and delete-user remove dev-user from your kubeconfig.

In real life

  • Bind to groups, not users. People join and leave teams; a group binding stays right without edits.
  • Most companies don't hand out client certificates. Users log in through an identity provider (OIDC, OpenID Connect), and their groups come from there. RBAC works the same way.
  • Certificates can't be revoked in Kubernetes. A leaked one works until it expires, which is why short lifetimes like this lesson's one day matter.
  • Audit who has cluster-admin and edit regularly. k get clusterrolebindings -o wide shows who holds each ClusterRole.

Spot the problem

Work out what's wrong before you read the explanation.

"I gave it node access, but it still can't list nodes." Someone creates a Role in default with --resource=nodes, binds it to a ServiceAccount, and can-i list nodes says no. Nothing errored. What's wrong? Nodes are cluster-wide, and a Role can only grant access inside its namespace. It needs a ClusterRole and a ClusterRoleBinding.

"The read-only account can read our passwords." A team binds the built-in edit ClusterRole to a monitoring account "so it can see everything". A security review finds it can read every Secret in the namespace. What should it have been? view. edit includes reading and changing Secrets; view deliberately leaves them out.

"can-i says no, but the binding is right." Someone runs k auth can-i pods list --as=... and gets no, even though the Role grants list on pods. What's wrong? The order: it's can-i VERB RESOURCE. With the words swapped, kubectl warns that pods isn't a known verb and answers no anyway. Read the warning.

Keep this

ToUse
An identity for an appk create sa NAME -n NS
Permissions in one namespacek create role NAME -n NS --verb=get,list --resource=pods
Cluster-wide permissionsk create clusterrole NAME --verb=list --resource=nodes
Grant in one namespacek create rolebinding NAME -n NS --role=ROLE (or --clusterrole=) plus --serviceaccount=NS:SA, --user= or --group=
Grant everywherek create clusterrolebinding NAME --clusterrole=ROLE --serviceaccount=NS:SA
Prove one permissionk auth can-i VERB RESOURCE -n NS --as=system:serviceaccount:NS:SA
List all permissionsk auth can-i --list -n NS --as=...
What a role containsk describe clusterrole NAME
Approve a user's CSRk certificate approve NAME
Act as another context oncek --context=NAME ...
Who am Ik auth whoami

Where you are now

You've built every kind of access the curriculum covers, and proved each one: a ServiceAccount that reads pods and nothing else, cluster-wide access with a ClusterRole, a built-in role reused in one namespace, and a user created from a certificate signing request and granted access through a group. You can also read a Forbidden error as the exact permission it's missing. Lesson 7 is next: Helm, Kustomize and CRDs.

Check yourself

  1. A task says: allow ServiceAccount monitor in namespace ops to list nodes. You create a Role and a RoleBinding in ops with --resource=nodes. can-i list nodes says no. What's needed?

  2. A monitoring account in namespace shop must read everything there except Secrets. Which binding fits?

  3. You run k auth can-i pods list --as=system:serviceaccount:ns:app and get a warning, then no. The Role grants list on pods. Why no?

  4. A user's certificate has the subject CN=amy, O=finance. Which binding gives her access through her group?

  5. kubectl says: Error from server (Forbidden): deployments.apps is forbidden: User bob cannot create resource deployments in API group apps in the namespace dev. What does it tell you?

  6. Which built-in ClusterRole lets an account create RoleBindings within one namespace, when bound with a RoleBinding?

Key terms

  • RBAC (role-based access control): Kubernetes' permission system: roles that list allowed actions, and bindings that grant them.
  • ServiceAccount: an identity for apps and scripts, in one namespace.
  • Role / ClusterRole: a set of allowed verbs on resources, in one namespace or cluster-wide.
  • RoleBinding / ClusterRoleBinding: grants a role to users, groups or ServiceAccounts, in one namespace or everywhere.
  • Verb: an action, like get, list, watch, create, update, delete.
  • API group: a family of resources. Pods are in the core group "", Deployments in apps.
  • CSR (certificate signing request): a request for the cluster's CA to sign a key, used to create users.
  • CN / O: a certificate's common name (the username) and organization (a group).
  • Impersonation: acting as another identity with --as, allowed for admins.
  • Forbidden vs Unauthorized: logged in but not allowed, versus not logged in at all.

Go deeper