Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Kubernetes RBAC: manage access securely

By updated

Improve this page

Logo Kubernetes

RBAC (Role-Based Access Control) decides, for every request the API server receives, who may perform which action on which resource. This lesson teaches all the RBAC of the course: the four objects, the kubectl create role and kubectl create rolebinding commands, the built-in view, edit and admin roles, reusing a ClusterRole in a single namespace, then proof with kubectl auth can-i. It is for anyone who can create a Pod and a namespace on the course's Kubernetes cluster, version 1.37. The examples grant rights to ServiceAccounts, the identity of applications.

What you will learn

  • Tell the four RBAC objects apart, and above all Role from ClusterRole
  • Create a Role and a RoleBinding with kubectl create, then read the YAML they produce
  • Reuse a built-in ClusterRole (view, edit, admin) in a single namespace
  • Check rights with kubectl auth can-i, --list and --as
  • Read a Forbidden message, which names the subject, the verb and the resource that were denied

What RBAC does not provide: users

Kubernetes has no User object: the kubectl create user command does not exist. A human identity is only a name that the API server reads from the CN field of a client certificate signed by the cluster CA, or that an external identity provider passes to it. Human users are therefore created at level 4, in the module on cluster administration, with the CertificateSigningRequest API, which the lesson Kubernetes cluster PKI details.

ServiceAccounts, on the other hand, are real Kubernetes objects: they are the identity of applications, and this lesson works with them. Their full name, the one that RBAC and --as expect, is written system:serviceaccount:<namespace>:<name>. The next lesson, ServiceAccounts for developers, explains how a Pod receives this identity and its token.

How RBAC works and its key concepts

RBAC rests on four resources: Role, ClusterRole, RoleBinding and ClusterRoleBinding. A Role or a ClusterRole describes permissions. A RoleBinding or a ClusterRoleBinding grants them to a subject: a service account, a user or a group. These are two separate actions.

RBAC is enabled by default on current Kubernetes distributions, nothing to install. One command confirms it on any cluster:

Terminal window
kubectl api-versions | grep rbac
Output
rbac.authorization.k8s.io/v1

Also keep in mind the principle that governs everything else: RBAC denies by default. Anything not explicitly granted is forbidden, and there is no deny rule to write, unlike what many firewalls offer.

Role and ClusterRole

These two objects define permissions. A Role belongs to a namespace and only describes rights on the resources of that namespace. A ClusterRole belongs to no namespace, which lets it describe rights on resources that have no namespace, such as nodes or persistent volumes.

The real scope of a ClusterRole is decided by the binding that goes with it, not by the ClusterRole itself. Paired with a ClusterRoleBinding, it applies to the whole cluster. Paired with a RoleBinding, the same ClusterRole only applies in the namespace of that binding. This third case lets you write a read role once and grant it namespace by namespace.

A Role is made of rules. Each rule names an API group ("" stands for the core group, the one of Pods), resources and verbs:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: equipe
name: lire-pods
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]

A ClusterRole has the same shape, without namespace in the metadata:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: lire-pods-cluster
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]

RoleBinding and ClusterRoleBinding

A binding associates a role with subjects. The RoleBinding acts in one namespace: it can target a Role of that namespace or a ClusterRole, and the rights stay limited to that namespace. The ClusterRoleBinding grants a ClusterRole on the whole cluster, and can only target a ClusterRole.

Here is a RoleBinding that grants the previous Role to the lecteur ServiceAccount of the equipe namespace:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: lecteur-lire-pods
namespace: equipe
subjects:
- kind: ServiceAccount
name: lecteur
namespace: equipe
roleRef:
kind: Role
name: lire-pods
apiGroup: rbac.authorization.k8s.io

And here is a ClusterRoleBinding that grants a ClusterRole to the same ServiceAccount, this time in all namespaces:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: lecteur-lire-pods-partout
subjects:
- kind: ServiceAccount
name: lecteur
namespace: equipe
roleRef:
kind: ClusterRole
name: lire-pods-cluster
apiGroup: rbac.authorization.k8s.io

A ClusterRoleBinding is only needed when the need is truly global: resources without a namespace (nodes, persistent volumes) or an identical right in every namespace. In the other cases, a RoleBinding limits the damage.

Verbs: specifying the possible actions

Verbs define the actions allowed on resources. These are the most used.

VerbAction
getRead a named resource
listList resources
watchObserve changes in real time
createCreate a resource
updateModify an existing resource
patchModify part of a resource, which is what kubectl apply and kubectl edit do
deleteDelete a resource

get and list are two distinct rights: being able to read a Pod by its name does not give the right to list the Pods of the namespace.

The built-in ClusterRoles: view, edit, admin

Kubernetes creates three ClusterRoles at installation, designed to be reused with a RoleBinding: view (read-only, without Secrets), edit (read and write on most objects of a namespace) and admin (edit, plus the management of rights in the namespace). Using them saves you from rewriting long, fragile lists of rules. Be careful: edit and admin also give access to the Secrets of the namespace, for reading and writing, while view excludes them. Reusing edit therefore amounts to handing over the passwords of the namespace. The demonstration below uses view.

The demonstration: give a ServiceAccount access

This demonstration runs on the course's kind cluster, with your administrator kubeconfig. It chains nine steps and one addition: create two accounts, see that they have nothing, grant rights with kubectl create, prove what passes and what stays denied, then clean up. All published outputs were captured on a kind cluster running version 1.37.

Step 1: Create a namespace and two ServiceAccounts

The equipe namespace holds both accounts: lecteur will receive a hand-written Role, auditeur a built-in ClusterRole.

Terminal window
kubectl create namespace equipe
kubectl create serviceaccount lecteur -n equipe
kubectl create serviceaccount auditeur -n equipe
Output
namespace/equipe created
serviceaccount/lecteur created
serviceaccount/auditeur created

Step 2: Check that an account with no binding has nothing

Before granting a right, check that it is missing. kubectl auth can-i asks the API server, and --as asks it on behalf of another subject: here the lecteur service account. Your administrator identity is allowed to impersonate another one, which is not the case for an ordinary account.

Terminal window
kubectl auth can-i list pods --as system:serviceaccount:equipe:lecteur -n equipe
Output
no

The answer is no and the exit code of the command is 1: kubectl auth can-i returns 1 for a denial, which makes it usable in a script.

Step 3: Create a Role with kubectl create role

Instead of writing the YAML, kubectl create role generates the Role in one command. You give the verbs with --verb and the resources with --resource.

Terminal window
kubectl create role lire-pods --verb=get,list,watch --resource=pods -n equipe
Output
role.rbac.authorization.k8s.io/lire-pods created

The generated YAML shows what the command wrote. It contains a single rule: the empty API group (the core group), the pods resource and the three requested verbs.

Terminal window
kubectl get role lire-pods -n equipe -o yaml
Output
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: "2026-11-12T19:24:53Z"
name: lire-pods
namespace: equipe
resourceVersion: "1262"
uid: e22a6616-7716-42e7-b455-bbb904852be7
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch

Step 4: Bind the Role with kubectl create rolebinding

The Role alone grants nothing. kubectl create rolebinding associates it with the ServiceAccount, which --serviceaccount designates in the form namespace:name.

Terminal window
kubectl create rolebinding lecteur-lire-pods --role=lire-pods --serviceaccount=equipe:lecteur -n equipe
Output
rolebinding.rbac.authorization.k8s.io/lecteur-lire-pods created

The -o wide option of kubectl get shows the target role and the subject of each binding, so you do not have to open the YAML.

Terminal window
kubectl get rolebinding -n equipe -o wide
Output
NAME ROLE AGE USERS GROUPS SERVICEACCOUNTS
lecteur-lire-pods Role/lire-pods 0s equipe/lecteur

Step 5: Prove what passes and what stays denied

A proof of least privilege has two sides: the right that was granted works, and the neighboring rights stay denied. Three questions show it, for the granted verb, a verb that was not granted and another namespace.

Terminal window
kubectl auth can-i list pods --as system:serviceaccount:equipe:lecteur -n equipe
kubectl auth can-i delete pods --as system:serviceaccount:equipe:lecteur -n equipe
kubectl auth can-i list pods --as system:serviceaccount:equipe:lecteur -n default
Output, one line per command
yes
no
no

The first answer is yes: the Role does its job. The second is no: the delete verb was never granted. The third is no too: a Role only applies in its namespace, and default is not equipe.

Step 6: Trigger a real denial and read the Forbidden message

can-i asks without acting. To see what the account actually receives, attempt the action with --as:

Terminal window
kubectl delete pod nimporte-lequel --as system:serviceaccount:equipe:lecteur -n equipe
Output
Error from server (Forbidden): pods "nimporte-lequel" is forbidden: User "system:serviceaccount:equipe:lecteur" cannot delete resource "pods" in API group "" in the namespace "equipe"

Reading a Forbidden message

A Forbidden message names the denied subject (system:serviceaccount:equipe:lecteur), the verb (delete), the resource (pods, in the empty API group) and the namespace (equipe). For a resource without a namespace, such as a node, the message ends with at the cluster scope instead of the namespace. It therefore already contains the diagnosis: you only have to find, among these elements, the one that no Role of the subject covers. Here, it is the delete verb, absent from the lire-pods Role. Read the whole message before changing anything.

Step 7: List all rights with can-i --list

Guessing through successive attempts is not necessary: --list asks the API server for all the rules that apply to the subject in a namespace.

Terminal window
kubectl auth can-i --list --as system:serviceaccount:equipe:lecteur -n equipe
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]
[/.well-known/openid-configuration] [] [get]
[/api/*] [] [get]
[/api] [] [get]
[/apis/*] [] [get]
[/apis] [] [get]
[/healthz] [] [get]
[/healthz] [] [get]
[/livez] [] [get]
[/livez] [] [get]
[/openapi/*] [] [get]
[/openapi] [] [get]
[/openid/v1/jwks/] [] [get]
[/openid/v1/jwks] [] [get]
[/readyz] [] [get]
[/readyz] [] [get]
[/version/] [] [get]
[/version/] [] [get]
[/version] [] [get]
[/version] [] [get]

A single line comes from the Role you wrote: pods with [get list watch]. All the others come from base ClusterRoles, granted to all authenticated accounts or to all ServiceAccounts: system:basic-user (the selfsubject* entries, which auth can-i relies on), system:discovery, system:public-info-viewer, system:service-account-issuer-discovery and system:cluster-trust-bundle-discovery. They concern neither your Pods nor your Secrets: there is nothing to remember about them, except that they exist. A URL that appears in two of these roles is listed twice.

Without the -n option, kubectl queries the namespace of the current context, default if the context sets none. The line of your Role then disappears, because it only applies in equipe. To check it, create a second namespace equipe2 with a lecteur that only has get and list on Pods, then compare the output with -n equipe2 and the output without -n.

Terminal window
kubectl create namespace equipe2
kubectl create serviceaccount lecteur -n equipe2
kubectl create role lire-pods --verb=get,list --resource=pods -n equipe2
kubectl create rolebinding lecteur-lire-pods --role=lire-pods --serviceaccount=equipe2:lecteur -n equipe2

The comparison is done with diff, which reads both outputs:

Terminal window
diff <(kubectl auth can-i --list --as=system:serviceaccount:equipe2:lecteur -n equipe2) <(kubectl auth can-i --list --as=system:serviceaccount:equipe2:lecteur)
Output
6d5
< pods [] [] [get list]

The only difference is the pods ... [get list] line. So remember to pass -n to see the rights of a namespace. The exit code 1 of this command is the one of diff, which signals a difference.

Step 8: Reuse a built-in ClusterRole with a RoleBinding

The auditeur account must read everything in equipe, and write nothing. Rather than writing a long Role, we reuse the view ClusterRole. The three built-in ClusterRoles exist as soon as the cluster is created:

Terminal window
kubectl get clusterrole view edit admin
Output
NAME CREATED AT
view 2026-11-12T19:20:17Z
edit 2026-11-12T19:20:17Z
admin 2026-11-12T19:20:17Z

The RoleBinding targets this ClusterRole with --clusterrole, and remains an object of the equipe namespace:

Terminal window
kubectl create rolebinding auditeur-view --clusterrole=view --serviceaccount=equipe:auditeur -n equipe
Output
rolebinding.rbac.authorization.k8s.io/auditeur-view created

The YAML of the binding shows the point to remember: roleRef.kind is ClusterRole, but the object remains a RoleBinding of the equipe namespace.

Terminal window
kubectl get rolebinding auditeur-view -n equipe -o yaml
Output
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
creationTimestamp: "2026-11-12T19:24:54Z"
name: auditeur-view
namespace: equipe
resourceVersion: "1265"
uid: e804ed6a-a126-4c62-a6b9-2714a39ef3a7
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: view
subjects:
- kind: ServiceAccount
name: auditeur
namespace: equipe

The diagram shows the two ways to grant this same ClusterRole. Look at the roleRef arrows: both bindings target the same ClusterRole, and only the nature of the binding changes the scope of the rights obtained.

The view ClusterRole, written once, is referenced by a RoleBinding in the equipe namespace, which gives read access to the resources of equipe only, and by a ClusterRoleBinding, which would give read access to the resources of all namespaces.roleRefroleRefClusterRole viewread-only, written onceRoleBindingin the equipe namespaceClusterRoleBindingRead access to resourcesof the equipe namespace onlyRead access to resourcesof every namespace
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

Two checks confirm it: view does not give Secrets, and its rights stop at the namespace of the binding.

Terminal window
kubectl auth can-i get secrets --as system:serviceaccount:equipe:auditeur -n equipe
kubectl auth can-i list deployments --as system:serviceaccount:equipe:auditeur -n equipe
kubectl auth can-i list deployments --as system:serviceaccount:equipe:auditeur -n default
Output, one line per command
no
yes
no

Secrets stay denied: view excludes them on purpose, because reading them amounts to reading passwords. Deployments can be listed in equipe and not in default: the ClusterRole is reused, but its scope is the one of the RoleBinding.

Give access to logs and name a user

The logs of a Pod are not the Pod: they form a subresource, pods/log, which is named separately in the Role. Reading a Pod does not give the right to read its logs. To grant both, the command lists both resources:

Terminal window
kubectl create role pod-reader --verb=get,list,watch --resource=pods,pods/log -n equipe

The subject of a RoleBinding is not always a ServiceAccount. For a user, kubectl create rolebinding takes --user=<name> instead of --serviceaccount:

Terminal window
kubectl create rolebinding read-pods-binding --role=pod-reader --user=dev-user -n equipe

This command succeeds even if dev-user exists nowhere: Kubernetes has no User object, and the binding only contains a name. The authentication of this user, that is, the proof that they really carry this name, is covered at level 4 of the course.

To check the right on the subresource, kubectl auth can-i takes the --subresource option. Ask the question for dev-user, then check that they cannot delete:

Terminal window
kubectl auth can-i get pods --subresource=log --as=dev-user -n equipe
kubectl auth can-i delete pods --as=dev-user -n equipe
Output
yes
no

The first answer comes from the pod-reader Role, the second shows that reading opens nothing else. Note that --as does not check that the identity exists: a misspelled name, if it appears in a binding, would answer yes in the same way. The lecteur account of the previous steps, for its part, would answer no on logs, since its lire-pods Role does not cover pods/log.

Step 9: Clean up

Deleting the namespace takes the ServiceAccounts, the Role and the RoleBindings with it. No file or kubeconfig was created, so there is nothing else to remove.

Terminal window
kubectl delete namespace equipe equipe2 --ignore-not-found

The command also names the equipe2 namespace from step 7. With --ignore-not-found, it succeeds whether you created it or not.

Put it into practice

You do not reread RBAC, you query it. The first lab binds a Role to the dev-user user to grant read-only access to Pods and their logs, and has you prove both sides with kubectl auth can-i: what passes and what stays denied. The second takes a cluster-admin granted for convenience to a ServiceAccount and brings it down to the strict minimum, the proof being what the API server answers, not what the manifest claims.

Key takeaways

  1. Kubernetes has no User object: a human identity is a CN in a certificate signed by the cluster CA, or an account from an external provider. Applications use ServiceAccounts.
  2. Role and ClusterRole define permissions, RoleBinding and ClusterRoleBinding grant them. These are two separate actions.
  3. The Cluster prefix tells the scope of the object, not the scope of the rights: a ClusterRole granted by a RoleBinding only applies in one namespace.
  4. RBAC denies by default: anything not explicitly granted is forbidden, and there is no deny rule.
  5. kubectl create role and kubectl create rolebinding write the YAML for you, and -o yaml shows it.
  6. Reuse view, edit or admin with a RoleBinding rather than writing long lists of rules.
  7. kubectl auth can-i, with --as and --list, proves rights without guesswork. Pass -n to see the rights of a namespace.
  8. A Forbidden message carries its diagnosis: the subject, the verb, the resource and the namespace appear in it, or at the cluster scope for a resource without a namespace.

Knowledge check

Seven questions on two distinctions to master: the real scope of a ClusterRole depending on the binding that goes with it, and what a Forbidden error message designates exactly.

Knowledge check

Check what you have learned with this interactive quiz

7 questions
5 min
80% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer