Creating a Role
To define a role, create a YAML file that sets the API version torbac.authorization.k8s.io/v1 and the kind to Role. In this example, we create a role named developer to grant developers specific permissions. The role includes a list of rules where each rule specifies the API groups, resources, and allowed verbs. For resources in the core API group, provide an empty string ("") for the apiGroups field.
For instance, the following YAML definition grants developers permissions on pods (with various actions) and allows them to create ConfigMaps:
Both roles and role bindings are namespace-scoped. This example assumes usage within the default namespace. To manage access in a different namespace, update the YAML metadata accordingly.
Creating a Role Binding
After defining a role, you need to bind it to a user. A role binding links a user to a role within a specific namespace. In this example, we create a role binding named devuser-developer-binding that grants the userdev-user the developer role.
Below is the combined YAML definition for both creating the role and its corresponding binding:
Verifying Roles and Role Bindings
After applying your configurations, it’s important to verify that the roles and role bindings have been created correctly. To list all roles in the current namespace, execute:Testing Permissions with kubectl auth
You can test whether you have the necessary permissions to perform specific actions by using thekubectl auth can-i command. For example, to check if you can create deployments, run:
--as flag. Although the developer role does not permit creating deployments, it does allow creating pods:
Limiting Access to Specific Resources
In some scenarios, you may want to restrict user access to a select group of resources. For example, if you have multiple pods in a namespace but only intend to provide access to pods named “blue” and “orange,” you can utilize theresourceNames field in the role rule.
Start with a basic role definition without any resource-specific restrictions: