Difficulty: Advanced
What is RBAC in Kubernetes, and how do you use Roles, ClusterRoles, bindings and ServiceAccounts to secure a cluster?
RBAC (Role-Based Access Control) is the Kubernetes authorizer that decides whether an authenticated subject may perform a verb on a resource. You define permissions in a Role (namespaced) or ClusterRole (cluster-wide), then grant them to users, groups or ServiceAccounts with a RoleBinding or ClusterRoleBinding. Permissions are purely additive and there are no deny rules, so security comes from granting the least privilege needed.
| Object | Scope | Purpose |
|---|---|---|
Role | One namespace | List of rules: apiGroups, resources, verbs |
ClusterRole | Cluster | Same rules, usable cluster-wide or for cluster-scoped resources like nodes |
RoleBinding | One namespace | Grants a Role or ClusterRole to subjects within that namespace |
ClusterRoleBinding | Cluster | Grants a ClusterRole across all namespaces |
A useful pattern: write one ClusterRole like deployment-manager and reuse it with RoleBindings in many namespaces. The permission stays limited to each binding's namespace.
Verbs map to HTTP actions: get, list, watch, create, update, patch, delete, deletecollection. Subresources are separate, for example pods/log and pods/exec.
automountServiceAccountToken: false on Pods that never call the API.system:serviceaccount:<ns>:<name>).resources: [""], verbs: [""]) and avoid handing out cluster-admin.list/get on secrets, create on pods/exec, create on pods in privileged namespaces, and the escalate, bind and impersonate verbs.default.view, edit and admin ClusterRoles for humans, bound per namespace.kubectl auth can-i --list --as=... and tools like rbac-lookup or kubectl-who-can.In practice, a team running a UPI service might give developers view in production, edit in staging, and let only the CD system's ServiceAccount (Argo CD, for example) change production Deployments.
apiVersion: v1
kind: ServiceAccount
metadata:
name: log-reader
namespace: payments
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-log-reader
namespace: payments
rules:
- apiGroups: [""] # core API group
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: log-reader-binding
namespace: payments
subjects:
- kind: ServiceAccount
name: log-reader
namespace: payments
roleRef:
kind: Role
name: pod-log-reader
apiGroup: rbac.authorization.k8s.io
The Role lists exactly what is allowed and the RoleBinding attaches it to one ServiceAccount in one namespace. roleRef cannot be changed after creation.
kubectl auth can-i list pods -n payments \
--as=system:serviceaccount:payments:log-reader
# yes
kubectl auth can-i delete pods -n payments \
--as=system:serviceaccount:payments:log-reader
# no
kubectl auth can-i --list -n payments \
--as=system:serviceaccount:payments:log-reader
Impersonation with --as lets you test a policy before handing out credentials (your own user needs the impersonate permission).
RBAC, Role, ClusterRole, RoleBinding, ServiceAccount, Least Privilege