RBAC
RBAC (Role-Based Access Control) is Kubernetes' built-in authorization system that decides which users, groups, and ServiceAccounts can perform which actions on which resources. It is implemented with four objects: Role, ClusterRole, RoleBinding, and ClusterRoleBinding.
The four objects
- Role: permissions within one namespace.
- ClusterRole: permissions cluster-wide, or on cluster-scoped resources like nodes.
- RoleBinding: grants a Role or ClusterRole to subjects within one namespace.
- ClusterRoleBinding: grants a ClusterRole to subjects across the whole cluster.
RBAC is additive. There are no deny rules, so a subject can do only what some binding allows.
user, group, ServiceAccount"] --> B["Binding"] B --> R["Role or ClusterRole
verbs on resources"] R --> S{"Scope"} S -->|"RoleBinding"| N["One namespace"] S -->|"ClusterRoleBinding"| C["Whole cluster"] classDef who fill:#475569,color:#ffffff classDef bind fill:#FFEDD5,color:#1E293B classDef role fill:#DBEAFE,color:#1E293B classDef scope fill:#D1FAE5,color:#1E293B class W who class B bind class R role class S,N,C scope
Example: namespace developer and CI bot
apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: developer namespace: payments-prodrules: - apiGroups: ["", "apps"] resources: ["pods", "services", "deployments", "configmaps"] verbs: ["get", "list", "watch", "create", "update", "patch"] - apiGroups: [""] resources: ["secrets"] verbs: ["get", "list"]apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: payments-developer-binding namespace: payments-prodsubjects: - kind: User name: rahul@devops.in apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: developer apiGroup: rbac.authorization.k8s.ioapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: ci-deployer namespace: payments-prodrules: - apiGroups: ["apps"] resources: ["deployments"] verbs: ["get", "patch", "update"]Test permissions, do not guess
kubectl auth can-i update deployments \ --as=system:serviceaccount:payments-prod:github-actions-bot -n payments-prodkubectl auth can-i --list -n payments-prodkubectl get clusterrolebindings -o json | \ jq '.items[] | select(.roleRef.name=="cluster-admin") | .subjects'Mistakes and fixes
| Mistake | Consequence | Fix |
|---|---|---|
| ClusterRoleBinding for team access | Cluster-wide access | Use a RoleBinding per namespace |
| Wildcard verbs and resources | Admin by accident | List exact verbs |
| Wrong apiGroups | Rule silently does not apply | Core is "", Deployments are "apps" |
| cluster-admin for CI | Pipeline compromise owns cluster | Minimal ci-deployer Role |
RememberA RoleBinding can reference a ClusterRole. Define a standard set of permissions once, then bind it per namespace.
SecurityGranting
getorliston Secrets reveals their values. Treat it as a high-privilege permission and avoid wildcards on Secrets.
Common MistakeGiving CI tools cluster-admin for convenience. A leaked token then controls the whole cluster.
Frequently Asked Questions
What is the difference between a Role and a ClusterRole?
A Role applies inside one namespace. A ClusterRole is defined cluster-wide and can cover cluster-scoped resources, and can also be bound into a single namespace with a RoleBinding to reuse a permission set.
How do I find out why I get 403 Forbidden?
Run kubectl auth can-i with the subject and verb, and list bindings in the namespace. The missing verb, resource, or apiGroup is usually obvious.
Does RBAC authenticate users?
No. Authentication, such as certificates or OIDC, identifies who is calling. RBAC only authorizes what that identity may do.