Role-Based Access Control (RBAC)

Difficulty: Advanced

Question

What is RBAC in Kubernetes, and how do you use Roles, ClusterRoles, bindings and ServiceAccounts to secure a cluster?

Answer

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.

The four objects

ObjectScopePurpose
RoleOne namespaceList of rules: apiGroups, resources, verbs
ClusterRoleClusterSame rules, usable cluster-wide or for cluster-scoped resources like nodes
RoleBindingOne namespaceGrants a Role or ClusterRole to subjects within that namespace
ClusterRoleBindingClusterGrants 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.

Who the subjects are

  • Users and groups are not Kubernetes objects. They come from the authentication layer: client certificates, OIDC (Google, Okta, Azure AD), or cloud IAM mappings such as EKS access entries.
  • ServiceAccounts are namespaced identities for Pods. Since Kubernetes 1.24, Pods get short-lived, auto-rotated projected tokens instead of long-lived Secret tokens being created automatically. Set automountServiceAccountToken: false on Pods that never call the API.

How a request is checked

  1. Authentication identifies the caller (user, group, or system:serviceaccount:<ns>:<name>).
  2. RBAC looks for any binding that grants this verb on this resource in this namespace. If none matches, the request is denied with 403.
  3. Admission controllers (for example Pod Security Admission, OPA Gatekeeper, Kyverno) can still reject or mutate it.

Hardening checklist

  • Avoid wildcards (resources: [""], verbs: [""]) and avoid handing out cluster-admin.
  • Treat these as near-admin: list/get on secrets, create on pods/exec, create on pods in privileged namespaces, and the escalate, bind and impersonate verbs.
  • Give each workload its own ServiceAccount rather than using default.
  • Use the built-in view, edit and admin ClusterRoles for humans, bound per namespace.
  • Audit regularly with 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.

How an API request is authorized. RBAC sits between authentication and admission and allows only what a binding explicitly grants.
  1. kubectl or Pod calls API: For example: list pods in namespace payments.
  2. Authentication: Cert, OIDC token or ServiceAccount token identifies the caller.
  3. RBAC authorization: Any matching binding allows; otherwise 403 Forbidden.
  4. Admission control: Policies can still reject or mutate the request.
  5. Persisted in etcd: Only fully approved requests change cluster state.

Code examples

Read-only access to Pods and logs for a ServiceAccount

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.

Verifying permissions

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

Key points

  • Role/RoleBinding are namespaced; ClusterRole/ClusterRoleBinding are cluster-wide.
  • A RoleBinding can reference a ClusterRole to reuse rules within one namespace.
  • RBAC is additive only; there are no deny rules.
  • Users and groups come from authentication; ServiceAccounts are Kubernetes objects for Pods.
  • Secrets read access, pods/exec, escalate, bind and impersonate are effectively privilege escalation paths.

Common follow-up questions

  • What is the difference between authentication and authorization in Kubernetes?
  • How would you give a CI pipeline permission to deploy to only one namespace?
  • Why is granting list on secrets dangerous?
  • How do ServiceAccount tokens work since Kubernetes 1.24?
  • How does EKS or GKE map cloud IAM identities to Kubernetes RBAC?

Interview tips

  • Draw the 2x2: namespaced vs cluster, permission vs binding; it organises your answer instantly.
  • Mention that RBAC has no deny rules and that least privilege is the only defence.
  • Show practical skill with kubectl auth can-i --as; it is how real teams debug 403s.

Concepts covered

RBAC, Role, ClusterRole, RoleBinding, ServiceAccount, Least Privilege

More Kubernetes & Orchestration questions