Skip to main content

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.

flowchart TD W["Subject
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

YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: developer
namespace: payments-prod
rules:
- apiGroups: ["", "apps"]
resources: ["pods", "services", "deployments", "configmaps"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list"]
YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: payments-developer-binding
namespace: payments-prod
subjects:
- kind: User
name: rahul@devops.in
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: developer
apiGroup: rbac.authorization.k8s.io
YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: ci-deployer
namespace: payments-prod
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "patch", "update"]

Test permissions, do not guess

Bash
kubectl auth can-i update deployments \
--as=system:serviceaccount:payments-prod:github-actions-bot -n payments-prod
kubectl auth can-i --list -n payments-prod
kubectl 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
Remember

A RoleBinding can reference a ClusterRole. Define a standard set of permissions once, then bind it per namespace.

Security

Granting get or list on Secrets reveals their values. Treat it as a high-privilege permission and avoid wildcards on Secrets.

Common Mistake

Giving 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.

Found a mistake, an outdated step or a broken link?