RBAC follows the principle of least privilege: grant only the permissions that an identity needs. If a permission isn’t explicitly granted, it’s denied.
- Inspecting namespaces and service accounts
- Creating a namespaced Role and RoleBinding
- Creating a ClusterRole and ClusterRoleBinding
- Binding roles to service accounts
- Verifying permissions with impersonation and best practices
Inspect namespaces and service accounts
List namespaces and then list service accounts in each team namespace:Namespaced Role: developer
Create a Role scoped to theteam-alpha namespace that grants CRUD-like permissions on core resources and the apps group.
Bind the Role to a user (RoleBinding)
Bind thedeveloper Role to the user alice in team-alpha:
Verify Alice’s permissions (impersonation)
Usekubectl auth can-i with --as to impersonate alice and validate permissions:
team-alpha, Alice can create pods there but not in team-beta. This enforces least privilege by design.
When granting or revoking RBAC permissions, exercise caution. Impersonation (using
--as) helps validate changes before committing them.ClusterRole: viewer
For cluster-wide view-only access, use a ClusterRole (not namespaced). Theviewer ClusterRole below grants get, list, and watch on selected core and apps resources across the cluster.
bob:
Binding a Role to a ServiceAccount
Service accounts are identities used by in-cluster workloads (CI/CD pipelines, controllers, automation). You bind Roles the same way you bind users. Create a RoleBinding to give theteam-alpha:deploy-bot service account the developer role:
team-alpha by impersonating its system name:
readonly-sa in team-beta, which has no bindings — it should not be able to get pods:
Role vs ClusterRole and Bindings — quick reference
Debugging and best practices
- Validate permissions with
kubectl auth can-iand--asto impersonate users or service accounts. - Prefer namespaced Roles and RoleBindings whenever possible; use ClusterRoles sparingly.
- Follow least privilege: grant only the verbs/resources required and scope them to the appropriate namespace.
- Test RBAC changes in a staging or non-production cluster before rolling them out to production.
- Keep Role and ClusterRole manifests in version control and review changes (code review/PR) before applying.