Kubernetes RBAC in practice: Roles, ClusterRoles, RoleBindings, and the dangerous permissions
Learn to authorize with RBAC: Role vs ClusterRole, RoleBinding vs ClusterRoleBinding, subresources like pods/log and pods/exec, aggregated ClusterRoles, built-in roles, and how to audit, with real kubectl auth can-i output.
Kubernetes$ kubectl auth can-i create pods --subresource=exec▍Authentication says who is asking; authorization says what that identity may do. In Kubernetes, the answer to the second question almost always comes from RBAC. It’s a simple model, purely additive permissions with no “deny” rule, but full of details that turn into incidents: a forgotten *, edit handed to someone who only needed to read logs, a misunderstood subresource. In this guide, we build everything on a kind cluster with Kubernetes 1.37 and show the real answers from kubectl auth can-i.
The four pieces
A rule combines API groups, resources, and verbs. Rules live in a role (Role or ClusterRole), and a binding (RoleBinding or ClusterRoleBinding) grants the role to users, groups, or ServiceAccounts.
| Object | Scope | What it’s for |
|---|---|---|
| Role | One namespace | Permissions inside that namespace |
| ClusterRole | Cluster | Cluster-scoped resources (nodes, namespaces), URLs like /metrics, or a reusable template |
| RoleBinding | One namespace | Grants a Role or a ClusterRole, but only applies in that namespace |
| ClusterRoleBinding | Cluster | Grants a ClusterRole in every namespace |
The most confusing combination, and the most useful one, is the third row: a RoleBinding that points to a ClusterRole. We’ll come back to it below. For the identity side, see Kubernetes authentication.
Verbs, resources, and subresources
Verbs map to HTTP operations: get, list, watch, create, update, patch, delete, and deletecollection. There are special verbs, such as use, bind, escalate, and impersonate, which show up in the dangerous permissions section.
Some resources have subresources with their own permissions: pods/log, pods/exec, pods/portforward, deployments/scale, serviceaccounts/token. Reading a pod’s logs is get on pods/log; opening a shell is create on pods/exec. That’s how you separate who reads logs from who gets into the container.
A detail we caught in the lab: kubectl auth can-i get pods/exec doesn’t test the subresource. kubectl reads pods/exec as “the pod named exec” and, in our case, answered yes, because the ServiceAccount could read any pod. The right way is --subresource=exec.
Demo: logs yes, exec no
A support ServiceAccount that reads pods and logs, and nothing else:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: e-read-logs
namespace: e-rbac-dev
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: e-support-read-logs
namespace: e-rbac-dev
subjects:
- kind: ServiceAccount
name: e-support
namespace: e-rbac-dev
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: e-read-logs
Testing with --as, which asks the API server to evaluate the request as another identity:
SA=system:serviceaccount:e-rbac-dev:e-support
kubectl auth can-i list pods --as=$SA -n e-rbac-dev
kubectl auth can-i get pods --subresource=log --as=$SA -n e-rbac-dev
kubectl auth can-i create pods --subresource=exec --as=$SA -n e-rbac-dev
kubectl auth can-i delete pods --as=$SA -n e-rbac-dev
kubectl auth can-i list secrets --as=$SA -n e-rbac-dev
kubectl auth can-i list pods --as=$SA -n e-rbac-prod
Each command answers just yes or no; putting the answers together:
list pods yes
get pods --subresource=log yes
create pods --subresource=exec no
delete pods no
list secrets no
list pods (e-rbac-prod) no
And the real test, against an nginx running in the namespace:
$ kubectl logs e-web -n e-rbac-dev --as=$SA --tail=1
2026/11/25 04:38:17 [notice] 1#1: start worker process 43
$ kubectl exec e-web -n e-rbac-dev --as=$SA -- id
error: unable to upgrade connection: pods "e-web" is forbidden: User "system:serviceaccount:e-rbac-dev:e-support"
cannot create resource "pods/exec" in API group "" in the namespace "e-rbac-dev"
One ClusterRole, many namespaces
Instead of copying the same Role into ten namespaces, define a ClusterRole once and bind it with a RoleBinding wherever you need it:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: e-read-only
rules:
- apiGroups: [""]
resources: ["pods", "services", "configmaps", "events"]
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: e-support-read-prod
namespace: e-rbac-prod
subjects:
- kind: ServiceAccount
name: e-support
namespace: e-rbac-dev
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: e-read-only
Asking with kubectl auth can-i list deployments.apps --as=$SA -n <namespace>:
list deployments -n e-rbac-prod yes
list deployments -n e-rbac-dev no
list deployments -n default no
list deployments -A no
list secrets -n e-rbac-prod no
The permission only applies in the RoleBinding’s namespace. If the binding were a ClusterRoleBinding, the same ClusterRole would apply across the whole cluster.
Aggregated ClusterRoles
A ClusterRole with an aggregationRule has no rules of its own: the controller copies into it the rules of every ClusterRole carrying the given label. That’s how an operator adds its CRDs to the built-in roles, using labels such as rbac.authorization.k8s.io/aggregate-to-view: "true".
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: e-monitoring
aggregationRule:
clusterRoleSelectors:
- matchLabels:
rbac.example.com/aggregate-to-e-monitoring: "true"
rules: [] # filled in by the controller
In the lab, creating two ClusterRoles with that label (one for pods and nodes, another for the /metrics URL) was enough for kubectl get clusterrole e-monitoring -o yaml to show both rules combined, seconds later.
Built-in roles: view, edit, admin, and cluster-admin
Kubernetes ships four user-facing ClusterRoles. We bound each one to a ServiceAccount with a RoleBinding in the same namespace and asked:
Question (can-i) |
view | edit | admin |
|---|---|---|---|
get secrets |
no | yes | yes |
create deployments.apps |
no | yes | yes |
create pods --subresource=exec |
no | yes | yes |
create rolebindings |
no | no | yes |
create serviceaccounts --subresource=token |
no | yes | yes |
impersonate serviceaccounts |
no | yes | yes |
update resourcequotas |
no | no | no |
The two second-to-last rows are surprising: anyone with edit in a namespace can mint tokens for and impersonate any ServiceAccount in that namespace. In practice, edit equals the sum of the permissions of every ServiceAccount in the namespace. cluster-admin, meanwhile, is everything, everywhere; keep it for emergency access.
Dangerous permissions
escalateon roles: lets you create or edit roles with permissions you don’t have.bindon roles: lets you bind roles you don’t have.impersonateon users, groups, or serviceaccounts: you become another identity.*in verbs or resources: includes things that don’t exist yet, such as future CRDs.listorwatchon secrets:listreturns the contents of every Secret, not just the names.nodes/proxy: gives access to the kubelet API and, through it, to running commands in pods.createon pods or workloads: whoever creates a pod can mount any Secret and use any ServiceAccount in the namespace.
RBAC has a protection against escalation that’s worth seeing in action. We gave an e-manager ServiceAccount permission to create RoleBindings and nothing else, and it tried to grant itself the admin role:
$ kubectl -n e-rbac-dev create rolebinding e-self-admin --clusterrole=admin \
--serviceaccount=e-rbac-dev:e-manager --as=system:serviceaccount:e-rbac-dev:e-manager
error: failed to create rolebinding: ... is forbidden: user "system:serviceaccount:e-rbac-dev:e-manager"
... is attempting to grant RBAC permissions not currently held:
{APIGroups:[""], Resources:["configmaps"], Verbs:["create" "delete" ...]}
...
Blocked. Then we added a Role with the bind verb on the admin ClusterRole (--verb=bind --resource=clusterroles --resource-name=admin). The same command went through, and kubectl auth can-i get secrets as e-manager answered yes. That’s why bind and escalate deserve the same care as cluster-admin.
Auditing RBAC
kubectl auth can-i --list --as=system:serviceaccount:e-rbac-dev:e-support -n e-rbac-dev
kubectl get clusterrolebindings -o wide | grep -E 'cluster-admin|system:masters'
--list shows everything an identity can do in a namespace; in the lab, it returned pods [get list watch] and pods/log [get], exactly what we granted. For the reverse question (“who can read Secrets here?”), the kubectl who-can plugin, installed through krew, helps. To know who used each permission, only the audit log can tell you; see audit logging for security. The big picture is in the Kubernetes attack surface.
Checklist
- Per-namespace Roles and reusable ClusterRoles bound with RoleBindings
- No
*in verbs or resources outside emergency roles -
pods/logseparated frompods/execfor support teams -
edittreated as access to every ServiceAccount in the namespace -
escalate,bind,impersonate, andnodes/proxyreviewed and justified -
cluster-adminonly for emergency access, with ClusterRoleBindings reviewed periodically -
kubectl auth can-i --listand the audit log used in access reviews
RBAC has no “deny”: every permission you didn’t grant is the only protection you have.