Skip to main content
Everything in Kubernetes starts with one question: who is allowed to do what and where? Role-Based Access Control (RBAC) answers that question. It’s a cornerstone of Kubernetes security: misconfigured RBAC can leave your cluster unusable or dangerously over-permissive. The model is simple and powerful:
  • Roles (or ClusterRoles) define permissions (verbs on resources).
  • RoleBindings (or ClusterRoleBindings) attach those permissions to identities: users, groups, or service accounts.
In this guide we’ll cover namespaced Roles and cluster-scoped ClusterRoles, show how to bind them to users and service accounts, and demonstrate how to test and debug permissions.

Check namespaces and service accounts

Start by listing your namespaces and the service accounts in team namespaces:

Developer Role (namespaced)

This Role grants CRUD + watch/list/get permissions for several common resources in the team-alpha namespace. It’s scoped to the namespace so it won’t affect other teams.
Apply the Role, then create a RoleBinding that grants this Role to a user named alice in the team-alpha namespace:
Test Alice’s permissions using kubectl auth can-i with impersonation via --as:
This enforces least privilege: Alice can create resources only in the namespace where the RoleBinding exists.

Cluster-wide Viewer Role (ClusterRole)

To provide read-only access across the entire cluster use a ClusterRole and ClusterRoleBinding. The following ClusterRole allows get, list, and watch for common cluster resources:
Apply and bind cluster-wide to a user bob:
Verify Bob’s permissions (impersonating Bob):

Binding Roles to Service Accounts

Service accounts are identities used by automation, controllers, and CI/CD systems. Bind Roles to service accounts using the --serviceaccount flag; the format is namespace:serviceaccount-name.
When impersonating a service account with kubectl auth can-i, use the username format: system:serviceaccount:<namespace>:<serviceaccount-name> — for example: system:serviceaccount:team-alpha:deploy-bot.
Bind the developer Role to the deploy-bot service account in team-alpha:
Test the permissions as that service account:

Default deny and RBAC debugging

Kubernetes RBAC is deny-by-default. If no binding grants a permission, it is denied — you do not need explicit deny rules. To debug RBAC:
  • Use kubectl auth can-i to check whether an identity can perform an action.
  • Use --as to impersonate users or service accounts.
  • For deeper debugging, increase verbosity on API calls (e.g., kubectl --v=8) or inspect Role/ClusterRole and RoleBinding/ClusterRoleBinding objects.
Always review Role and ClusterRole changes carefully. Test with kubectl auth can-i --as=... before applying changes to production to avoid accidentally granting too much access.

Quick comparison

Summary / Best practices

  • Roles and RoleBindings are namespaced; ClusterRoles and ClusterRoleBindings are cluster-scoped.
  • Prefer namespaced Roles when access only needs to be limited to a team or project.
  • Use kubectl auth can-i with --as to test permissions for users and service accounts before and after changes.
  • Follow the principle of least privilege: grant only the permissions required.
  • Remember: by default, Kubernetes denies actions that aren’t explicitly granted.
Practice these principles in a safe lab environment by creating Roles, ClusterRoles, RoleBindings, and ClusterRoleBindings and validating access with kubectl auth can-i.

Watch Video

Practice Lab