kubectl auth can-i.

staging namespace was deleted — 32 Deployments, 15 Services, 8 StatefulSets, and all PVCs. Because the PVCs had reclaimPolicy: Delete, data was lost permanently. The outage lasted six hours and cost significant revenue and recovery effort.
The root cause: everyone had cluster-admin. No separation of environments. No restrictions on destructive actions.


Role + RoleBinding. Only go cluster-wide when you genuinely need cluster scope or access to non-namespaced resources.
Authoring a Role (example)
Create a read-only Role that permits listing pods and reading pod logs in the payments namespace:
apiGroups:""(empty string) is the core API group (pods, services, configmaps). Other groups include"apps"(Deployments) and"batch"(Jobs).resources: resource types for the rule. Subresources likepods/logare valid.verbs: operations allowed. Read-only typically usesget,list,watch. Addcreate,update,patchfor write, anddeletefor destructive actions. Avoid wildcard*unless absolutely necessary.resourceNames(optional): restricts the rule to specific named resources for precise permissions.
RoleBinding connects subjects (who) to a Role or ClusterRole (what). Examples:
Bind the pod-reader Role to user alice in payments:
ClusterRole (e.g., edit) to a service account in a different namespace:
User— human identity from your ID provider.Group— group from your ID provider.ServiceAccount— Pod identities (system:serviceaccount:<namespace>:<name>).
Use
cluster-admin only for emergency / break-glass scenarios. Day-to-day operations should use least-privilege, namespace-scoped roles wherever possible.
kubectl auth can-i is the primary tool to test permissions. Common usage patterns:
- Can the current identity create Deployments in
payments?
- Can user
alicedelete Secrets inproduction?
- What can the
deploy-botservice account do inpayments?
- Impersonate a user:
--as=<user> - Impersonate a service account:
--as=system:serviceaccount:<namespace>:<name> - Enumerate all permissions:
--list - After applying Roles/Bindings, run
kubectl auth can-ito confirm expected behavior.
Wrap-up: core concepts
- Role and ClusterRole define permissions (rules built from
apiGroups,resources, optionalresourceNames, andverbs). - RoleBinding and ClusterRoleBinding grant those permissions to subjects.
- Prefer namespace-scoped
Role+RoleBindingfor day-to-day access. - Use
kubectl auth can-ito validate and debug RBAC policies.

- Kubernetes RBAC documentation: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
- Best practices: favor least-privilege, prefer namespace-scoped roles, and regularly audit bindings.