
What is a Role?
A Role is a namespace-scoped RBAC object that defines a set of permissions (rules) for resources inside a specific namespace. Roles are composed of one or more rules that specify which API groups, resources, and verbs (actions) are allowed. Optionally, a rule can restrict access to specific resource names. Key fields in a Role rule:Example: create a namespaced Role
developer-role.yamlGranting the Role: RoleBinding
To grant the permissions defined in a Role to a user, group, or service account within the same namespace, create a RoleBinding. RoleBindings are namespace-scoped and must live in the same namespace as the Role (unless you reference a ClusterRole). Example RoleBinding manifest:ClusterRole and bind it with a ClusterRoleBinding.
Common kubectl commands to view RBAC objects
Examples:
Testing permissions with kubectl auth can-i
Usekubectl auth can-i to test whether a user has a given permission.
As the current user:
--as to test their permissions:
dev-user only has pod creation rights in the default namespace, the previous command for the test namespace will return no.
Restricting access to specific named resources
To limit a rule so it only applies to specific named resources, useresourceNames in the rule. Example: allow get on only two pods named blue and orange:
Remember: Roles and RoleBindings are namespace-scoped. If you need cluster-wide permissions (across all namespaces), use
ClusterRole and ClusterRoleBinding instead.Further reading and references
- Kubernetes RBAC documentation: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
- Kubernetes Authorization concepts: https://kubernetes.io/docs/reference/access-authn-authz/authorization/
- kubectl reference: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
kubectl auth can-i (with --as if needed) to validate permissions.