- Roles (or ClusterRoles) define permissions (verbs on resources).
- RoleBindings (or ClusterRoleBindings) attach those permissions to identities: users, groups, or service accounts.
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 theteam-alpha namespace. It’s scoped to the namespace so it won’t affect other teams.
alice in the team-alpha namespace:
kubectl auth can-i with impersonation via --as:
Cluster-wide Viewer Role (ClusterRole)
To provide read-only access across the entire cluster use a ClusterRole and ClusterRoleBinding. The following ClusterRole allowsget, list, and watch for common cluster resources:
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.developer Role to the deploy-bot service account in team-alpha:
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-ito check whether an identity can perform an action. - Use
--asto 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-iwith--asto 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.
Links and references
Practice these principles in a safe lab environment by creating Roles, ClusterRoles, RoleBindings, and ClusterRoleBindings and validating access withkubectl auth can-i.